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 = %s' %(line_text,eval(line_text)))
except:
self.browser.append('%s is invalid!' %(line_text))
self.line_edit.selectAll()
if __name__ == "__main__":
app = QApplication(sys.argv)
cal = Calculate()
cal.show()
sys.exit(app.exec_())
标签:__,控件,Calculate,edit,text,self,Pyqt,html,line
来源: https://www.cnblogs.com/hester/p/10442903.html