PyQt4程序不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:Anaconda3python.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

请帮忙。

答案

问题是由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_()

以上是关于PyQt4程序不起作用的主要内容,如果未能解决你的问题,请参考以下文章

PyQt4 Windows - 工具栏操作不起作用。我该如何修复它们?

PyQt4程序不起作用

睡眠在pyqt4上不起作用

PyQt4 QTabWidget TAB CHANGE....currentChange(int) 不起作用

java代码在片段活动中不起作用

片段不起作用并且有错误