Python PyQt4程序不';行不通

Python PyQt4程序不';行不通,python,python-3.x,pyqt,pyqt4,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_

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

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_()
来自未来进口部的

导入系统
从数学导入*
从PyQt4.QtCore导入*
从PyQt4.QtGui导入*
课堂形式(QDialog):
def uuu init uuu(self,parent=None):
超级(形式,自我)。\uuuu初始\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.browser=QTextBrowser()
self.lineedit=QLineEdit(“键入表达式并按Enter键”)
self.lineedit.selectAll()
layout=QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineedit)
self.setLayout(布局)
self.lineedit.setFocus()
self.connect(self.lineedit,信号(“returnPressed()”),
自我更新)
self.setWindowTitle(“计算”)
def updateUi(自我):
尝试:
text=unicode(self.lineedit.text())
self.browser.append(“%s=%s”%(文本,eval(文本)))
除:
self.browser.append(“%s无效!”%text)
app=QApplication(sys.argv)
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
C:\Anaconda3\python.exe“F:/Programming solutions/python/pycharmpython/GuiApp/gui1.pyw”
回溯(最近一次呼叫最后一次):
文件“F:/Programming solutions/python/pycharmpython/GuiApp/gui1.pyw”,第24行,在updateUi中
text=unicode(self.lineedit.text())
名称错误:未定义名称“unicode”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“F:/Programming solutions/python/pycharmpython/GuiApp/gui1.pyw”,第27行,在updateUi中
self.browser.append(“%s无效!”%text)
UnboundLocalError:赋值前引用的局部变量“text”

请提供帮助。

问题是由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.QtCore导入*
从PyQt4.QtGui导入*
课堂形式(QDialog):
def uuu init uuu(self,parent=None):
超级(形式,自我)。\uuuu初始\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.browser=QTextBrowser()
self.lineedit=QLineEdit(“键入表达式并按Enter键”)
self.lineedit.selectAll()
layout=QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineedit)
self.setLayout(布局)
self.lineedit.setFocus()
self.connect(self.lineedit,信号(“returnPressed()”),
自我更新)
self.setWindowTitle(“计算”)
def updateUi(自我):
text=str(self.lineedit.text())
尝试:
self.browser.append(“%s=%s”%(文本,eval(文本)))
除:
self.browser.append(“%s无效!”%text)
app=QApplication(sys.argv)
form=form()
表格.show()
app.exec()

问题是由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.QtCore导入*
从PyQt4.QtGui导入*
课堂形式(QDialog):
def uuu init uuu(self,parent=None):
超级(形式,自我)。\uuuu初始\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.browser=QTextBrowser()
self.lineedit=QLineEdit(“键入表达式并按Enter键”)
self.lineedit.selectAll()
layout=QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineedit)
self.setLayout(布局)
self.lineedit.setFocus()
self.connect(self.lineedit,信号(“returnPressed()”),
自我更新)
self.setWindowTitle(“计算”)
def updateUi(自我):
text=str(self.lineedit.text())
尝试:
self.browser.append(“%s=%s”%(文本,eval(文本)))
除:
self.browser.append(“%s无效!”%text)
app=QApplication(sys.argv)
form=form()
表格.show()
app.exec()

太棒了……非常感谢。实际上,我也有同样的想法,但对unicode与v.2和v.3的兼容性一无所知。至于文本,我真的很困惑,因为我对pyqt4一无所知。这个例子来自马克·萨默菲尔德的一本书。所以,如果像他这样的人做错了,像我这样的新手不可能知道任何事情。。。。非常感谢…太棒了…非常感谢。实际上,我也有同样的想法,但对unicode与v.2和v.3的兼容性一无所知。至于文本,我真的很困惑,因为我对pyqt4一无所知。这个例子来自马克·萨默菲尔德的一本书。所以,如果像他这样的人做错了,像我这样的新手不可能知道任何事情。。。。谢谢。。。