PyQt4程序不起作用

时间:2017-12-20 06:22:00

标签: python python-3.x pyqt pyqt4

该程序没有按预期工作。它应该接受一个表达式并将其写入上面的文本框,但它不会这样做。

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_()

enter image description here

但是我会运行窗口,但是当我输入一个表达式并点击返回以下内容时:

C:\Anaconda3\python.exe "F:/Programming solutions/python/pycharmpython/GuiApp/gui1.pyw"
Traceback (most recent call last):
  File "F:/Programming solutions/python/pycharmpython/GuiApp/gui1.pyw", line 24, in updateUi
    text = unicode(self.lineedit.text())
NameError: name 'unicode' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:/Programming solutions/python/pycharmpython/GuiApp/gui1.pyw", line 27, in updateUi
    self.browser.append("<font color=red>%s is invalid!</font>" % text)
UnboundLocalError: local variable 'text' referenced before assignment

请帮忙。

1 个答案:

答案 0 :(得分:2)

问题是由python2和python3之间的不兼容引起的,例如python3中不再出现unicode。还有一个错误的异常处理,例如,如果错误发生在行text = unicode (self.lineedit.text ())中,那么文本变量将永远不会被定义,因此在打印错误的行中生成另一个错误,考虑到上述情况我已经实现了以下与python2和python3兼容的解决方案。

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):
        text = str(self.lineedit.text())
        try:
            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_()