Pyqt使用html语法格式化输出控件内容

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class Calculate(QDialog):
    def __init__(self,parent=None):
        super(Calculate,self).__init__(parent)

        self.browser = QTextBrowser()
        self.line_edit = QLineEdit('Type an expression an press Enter')
        self.line_edit.setFocusPolicy(Qt.StrongFocus)
        self.line_edit.selectAll()

        layout = QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.line_edit)
        self.setLayout(layout)
        self.setWindowTitle('Calculate')
        self.connect(self.line_edit,SIGNAL('returnPressed ()'),self.update_ui)

    def update_ui(self):
        try:
            line_text = unicode(self.line_edit.text())
            self.browser.append('%s = <b>%s</b>' %(line_text,eval(line_text)))
        except:
            self.browser.append('<font color=red>%s is invalid!</font>' %(line_text))
        self.line_edit.selectAll()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    cal = Calculate()
    cal.show()
    sys.exit(app.exec_())

  

猜你喜欢

转载自www.cnblogs.com/hester/p/10442903.html