PyQt4 QTextBrowser.append 不显示任何输出
PyQt4 QTextBrowser.append Does'nt show any output
我刚开始学习 PyQt 和 GUI 编程并且
我从 "Rapid GUI Programming with Python and Qt:The Definitive Guide to PyQt Programming" 一书中完全复制了这段代码,它应该显示一个计算表达式的计算器。
当我 运行 应用程序 main window 出现但没有做任何事情时,因为我从一本著名的 pyqt 书中复制了代码,所以这很奇怪。
我正在使用 python 3.4.4 和 pyqt4。这是我从书中复制的代码:
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("retrunPressed()"),
self.updateUi)
self.setWindowTitle("Calculate")
def updateUi(self):
try:
text= unicode(self.lineedit.text())
self.browser.append("{0} = <b>{1}</b>".format(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_()
这些是我得到的错误:
Traceback (most recent call last):
文件 "calculator.pyw",第 25 行,在 updateUi 中
文本 = unicode(self.lineedit.text())
NameError:名称 'unicode' 未定义
在处理上述异常的过程中,又发生了异常:
回溯(最后一次调用):
文件 "calculator.pyw",第 29 行,在 updateUi 中
“%s 无效!” % 文本)
UnboundLocalError:赋值前引用的局部变量'text'
我知道让别人调试我的代码不是个好主意,但我已尽我所能,但没有任何结果。
谢谢
你的信号名称有错字,应该是
self.connect(self.lineedit, SIGNAL("returnPressed()"),
没有
self.connect(self.lineedit, 信号("retrunPressed()"),
很明显你没抄好,而且这本书似乎是用python2写的所以你不需要python3中的unicode函数字符串默认已经是unicode
我刚开始学习 PyQt 和 GUI 编程并且 我从 "Rapid GUI Programming with Python and Qt:The Definitive Guide to PyQt Programming" 一书中完全复制了这段代码,它应该显示一个计算表达式的计算器。 当我 运行 应用程序 main window 出现但没有做任何事情时,因为我从一本著名的 pyqt 书中复制了代码,所以这很奇怪。 我正在使用 python 3.4.4 和 pyqt4。这是我从书中复制的代码:
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("retrunPressed()"),
self.updateUi)
self.setWindowTitle("Calculate")
def updateUi(self):
try:
text= unicode(self.lineedit.text())
self.browser.append("{0} = <b>{1}</b>".format(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_()
这些是我得到的错误:
Traceback (most recent call last):
文件 "calculator.pyw",第 25 行,在 updateUi 中 文本 = unicode(self.lineedit.text()) NameError:名称 'unicode' 未定义
在处理上述异常的过程中,又发生了异常:
回溯(最后一次调用): 文件 "calculator.pyw",第 29 行,在 updateUi 中 “%s 无效!” % 文本) UnboundLocalError:赋值前引用的局部变量'text'
我知道让别人调试我的代码不是个好主意,但我已尽我所能,但没有任何结果。 谢谢
你的信号名称有错字,应该是
self.connect(self.lineedit, SIGNAL("returnPressed()"),
没有
self.connect(self.lineedit, 信号("retrunPressed()"),
很明显你没抄好,而且这本书似乎是用python2写的所以你不需要python3中的unicode函数字符串默认已经是unicode