2013年12月30日 星期一

PyQt4 教學3 Regexp 測試器

 
Step 1.
先建立基本 視窗 
# -*- coding: UTF8 -*-
import os, sys, re
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class RegexTest(QWidget):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setWindowTitle("RegexExpress Test")

        #self.setupUI()
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = RegexTest()
    main.show()
    sys.exit(app.exec_())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Step 2.
我們只需要簡單 添加 三個元件
QTextEdit * 2
QLineEdit * 1
QHBoxLayout *1
class RegexTest 添加函數 setupUI
 
 
   def setupUI(self):
        layout = QHBoxLayout()
        rightLayout = QVBoxLayout()

        self.inputTextEdit = QTextEdit()
        self.outputTextEdit = QTextEdit()
        self.lineEdit = QLineEdit("Type an expression and press Enter")

        rightLayout.addWidget(self.inputTextEdit)
        rightLayout.addWidget(self.outputTextEdit)
        rightLayout.addWidget(self.lineEdit)

        layout.addLayout(rightLayout)

        self.setLayout(layout)
 
 
 
 
 這樣程式的基本雛形,就已經有了
再來我們可以寫個函數,取得
self.inputTextEdit的文字,來做正則語法匹配

   def setupUI(self):
        .
        .
        self.lineEdit.returnPressed.connect(self.update)

    def update(self):
        self.outputTextEdit.clear()
        try:
            text = str(self.inputTextEdit.toPlainText())
            rule = unicode(self.lineEdit.text())
            result = re.findall(rule, text)
            self.outputTextEdit.append("%s" % result)

        except Exception, e:
            self.outputTextEdit.append(
                "%s is invalid!" % unicode(
                                                self.lineEdit.text()))
            self.outputTextEdit.append(unicode(e))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = RegexTest()
    main.show()
    testText = """
    Beautiful is better than ugly.
    translate by noname.tam@gmail.com
    16 minutes  37.57.166.156
    22 minutes  201.211.193.192
    """
    main.inputTextEdit.setText(testText)
    sys.exit(app.exec_())
 
 

 
 
 Step:
基本功能有了後,剩再做一些美化、加強功能的事情
標籤: 
styleString = "%s"
inputLabel = QLabel(styleString % "Input Text:")
rightLayout.addWidget(inputLabel)
 
控制元件
controlLayout = QHBoxLayout()
self.addBtn = QPushButton("Add")
self.removeBtn = QPushButton("Remove")
optionLabel = QLabel("Option:")
self.option = QComboBox()
self.option.addItem("findall")
self.option.addItem("match")
controlLayout.addWidget(self.addBtn)
controlLayout.addWidget(self.removeBtn)
controlLayout.addWidget(optionLabel)
controlLayout.addWidget(self.option)
controlLayout.addStretch()

基本上都還很陽春,其它功能,留待後面有時間再做補充…
 
現在完整代碼如下: 

# -*- coding: UTF8 -*-
import os, sys, re
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class RegexTest(QWidget):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setWindowTitle("RegexExpress Test")

        self.setupUI()

    def setupUI(self):
        styleString = "%s"
        layout = QHBoxLayout()
        rightLayout = QVBoxLayout()
        inputLabel = QLabel(styleString % "Input Text:")
        self.inputTextEdit = QTextEdit()
        outputLabel = QLabel(styleString % "Result:")
        self.outputTextEdit = QTextEdit()
        self.lineEdit = QLineEdit("Type an expression and press Enter")
        self.lineEdit.selectAll()

        controlLayout = QHBoxLayout()
        self.addBtn = QPushButton("Add")
        self.removeBtn = QPushButton("Remove")
        optionLabel = QLabel("Option:")
        self.option = QComboBox()
        self.option.addItem("findall")
        self.option.addItem("match")
        controlLayout.addWidget(self.addBtn)
        controlLayout.addWidget(self.removeBtn)
        controlLayout.addWidget(optionLabel)
        controlLayout.addWidget(self.option)
        controlLayout.addStretch()

        rightLayout.addWidget(inputLabel)
        rightLayout.addWidget(self.inputTextEdit)
        rightLayout.addWidget(outputLabel)
        rightLayout.addWidget(self.outputTextEdit)
        rightLayout.addLayout(controlLayout)
        rightLayout.addWidget(self.lineEdit)
        layout.addLayout(rightLayout)
        self.setLayout(layout)
        self.lineEdit.setFocus()
        self.lineEdit.returnPressed.connect(self.update)

    def update(self):
        print 'test'
        self.outputTextEdit.clear()
        try:
            text = self.inputTextEdit.toPlainText()
            rule = unicode(self.lineEdit.text())
            result = re.findall(rule, text)
            print "test", result
            self.outputTextEdit.append(str(result))
        except Exception, e:
            self.outputTextEdit.append(
                "%s is invalid!" % unicode(
                                                self.lineEdit.text()))
            self.outputTextEdit.append(unicode(e))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = RegexTest()
    main.show()
    testText = """
    Beautiful is better than ugly.
    translate by noname.tam@gmail.com
    16 minutes  37.57.166.156
    22 minutes  201.211.193.192
    """
    main.inputTextEdit.setText(testText)
    sys.exit(app.exec_())
 
 
 
 
 
 
 
 
 
 
 

沒有留言:

張貼留言