【问题标题】:PyQt4 program doesn't workPyQt4 程序不工作
【发布时间】:2017-12-20 14:33:07
【问题描述】:

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

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

但我会出现运行窗口,但是当我输入表达式并点击返回时,会发生以下情况:

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

请帮忙。

【问题讨论】:

    标签: python python-3.x pyqt pyqt4


    【解决方案1】:

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

    【讨论】:

    • 太棒了...非常感谢。我实际上也有同样的想法,但对 unicode 与 v.2 和 v.3 的兼容性一无所知。至于文本的事情我真的很困惑,因为我对pyqt4一无所知。这个例子来自 Mark Summerfield 的一本书。所以,如果像他这样的人弄错了,像我这样的新手是不可能知道任何事情的......非常感谢......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2016-05-14
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    相关资源
    最近更新 更多