PyQt: eg2

#coding:utf-8

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


class Form(QtGui.QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.browser = QtGui.QTextBrowser()
        self.lineedit = QtGui.QLineEdit("Type an expression and press Enter")
        self.lineedit.selectAll()  #设置文本全选
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.lineedit)
        self.setLayout(layout)
        self.lineedit.setFocus()  #设置光标定位
        self.connect(self.lineedit, QtCore.SIGNAL("returnPressed()"), self.updateUi)
        self.setWindowTitle("Calculate")

    def updateUi(self):
        try:
            text = unicode(self.lineedit.text())
            self.browser.append("{}={}".format(text,eval(text)))
        except:
            self.browser.append(u"{} is invalid".format(text))

app = QtGui.QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
原文地址:https://www.cnblogs.com/lypy/p/6394194.html