我正在尝试运行“使用 Python 和 QT 进行快速 GUI 编程”一书中的示例,但收到一条错误消息。
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 = QBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineedit)
self.setLayout(layout)
self.lineedit.setFocus()
self.connect(self.lineedit, SIGNAL("returnPressed()"),self.UpdateGUI)
self.setWindowTitle("Ryans App")
def UpdateGUI(self):
try
text = 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_()
我得到的痕迹是:
Traceback (most recent call last):
File "C:\Users\MyName\workspaces\LearningProject\src\LearningModule.py", line 33, in <module>
form = Form()
File "C:\Users\MyName\workspaces\LearningProject\src\LearningModule.py", line 16, in __init__
layout = QBoxLayout()
TypeError: QBoxLayout(QBoxLayout.Direction, QWidget parent=None): not enough arguments
我很困惑为什么它需要一个参数来创建 Form 对象,因为我只是想从 QDialog 继承......我错过了语法中的微妙之处吗?