代码改变世界

PyQt: eg2

2017-02-13 16:04  一方书斋  阅读(129)  评论(0编辑  收藏  举报
#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_()