利用QTextBrowser------实现在GUI中输出你想输出的任何内容

首先第一个脚本是输出一个建议计算器,在QLineEdit单行文本框中输入计算式,然后在QTextBrowser显示组件中输出结果

#!/ust/bin/python
from __future__ import division
import sys 
from math import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.browser = QTextBrowser()
        self.lineedit = QLineEdit("Type an expression and press Enter")
        self.lineedit.selectAll()
        layout = QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.lineedit)
        self.setLayout(layout)
        self.lineedit.setFocus()
        self.connect(self.lineedit, SIGNAL("returnPressed()"),self.updateUi)
        self.setWindowTitle("Calculate")
    def updateUi(self):
         
         try:
             text = unicode(self.lineedit.text())
             self.browser.append("%s = <b>%s</b>" % (text, eval(text)))
         except:
             self.browser.append("<font color=red>%s is invalid!</font>" % text)
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

QTextBrowser是一个显示组件

QLineEdit是一个输入组件(单行文本框)

QTextEdit也是一个输入组件,但他是多行文本框


下面是第二个脚本,这个脚本用了一个button,点击button,在QTextBrowser框中就会出现你预设的内容
#!/ust/bin/python

from __future__ import division
import sys 
from math import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.browser = QTextBrowser()
        self.click = QPushButton('OK')
        layout = QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.click)
        self.setLayout(layout)
        self.connect(self.click, SIGNAL("clicked()"),self.updateUi)
        self.setWindowTitle("Calculate")

    def updateUi(self):
        self.browser.append("%s" % 'This is a test')

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
~



你可能感兴趣的:(利用QTextBrowser------实现在GUI中输出你想输出的任何内容)