我正在尝试从“使用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继承...我在语法上缺少精妙之处吗?

最佳答案

创建QBoxLayout时,您需要指定方向(例如QBoxLayout.LeftToRight)和可选的父级(在这种情况下,self应该作为父级)。
这些应该添加到您的layout = QBoxLayout()行中。

10-06 14:25