【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_()
原文地址:https://www.cnblogs.com/colipso/p/3304575.html