Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt中的继承困难_Python_Qt - Fatal编程技术网

Python PyQt中的继承困难

Python PyQt中的继承困难,python,qt,Python,Qt,我试图运行“使用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.brows

我试图运行“使用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_()    
导入系统 从数学导入* 从PyQt4.QtCore导入* 从PyQt4.QtGui导入* 课堂形式(QDialog): def uuu init uuu(self,parent=None): 超级(形式,自我)。\uuuu初始\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu self.browser=QTextBrowser() self.lineedit=QLineEdit(“键入表达式并按enter键”) self.lineedit.selectAll() 布局=QBoxLayout() layout.addWidget(self.browser) layout.addWidget(self.lineedit) self.setLayout(布局) self.lineedit.setFocus() self.connect(self.lineedit,SIGNAL(“returnPressed()”),self.UpdateGUI) self.setWindowTitle(“Ryans应用程序”) def UpdateGUI(自): 尝试 text=self.lineedit.text() self.browser.append(“%s=%s”%(文本,eval(文本))) 除: self.browser.append(“%s无效!”%text) app=QApplication(sys.argv) 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
回溯(最近一次呼叫最后一次):
文件“C:\Users\MyName\workspace\LearningProject\src\LearningModule.py”,第33行,在
form=form()
文件“C:\Users\MyName\workspaces\LearningProject\src\LearningModule.py”,第16行,在\uu init中__
布局=QBoxLayout()
TypeError:QBoxLayout(QBoxLayout.Direction,QWidget parent=None):参数不足

我不明白为什么它需要一个参数来创建表单对象,因为我只是试图从QDialog继承。。。我是否缺少语法中的微妙之处?

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

我的版本使用的是QVBoxLayout:

...
self.lineedit.selectAll()
layout = QVBoxLayout()
layout.addWidget(self.browser)
...
我的理解是,由于它将小部件垂直排列,因此.LeftToRight和parent并不是严格必需的


我正在使用本书网站上最新的python 2.6代码存档。

您需要在
layout=QBoxLayout()
行上指定一个方向(例如
QBoxLayout.LeftToRight
)和可选的父级(在本例中,
self
应该作为父级)。这很有效!谢谢你介意把你的评论作为回答,这样我就可以接受了吗?谢谢你提供的信息,仔细看后我发现我的版本也使用了QVBoxLayout,当它像你上面所说的那样工作时就可以了。最好避免直接使用
QBoxLayout
,而改用
QVBoxLayout
QHBoxLayout
,除非在运行时确实需要选择布局方向。