如何在Python PyQt4中设置现有MainWidnow的中央小部件? [英] How to set the central widget of existing MainWidnow in Python PyQt4?
问题描述
我有一个由 3个类
组成的代码。类 widget1
和 widget2
继承自 QFrame
类,并包括 lineedit
和 combobox
中的一个供用户输入信息。
I have a code which consists of 3 classes
. The classes widget1
and widget2
inherit from QFrame
class and consists of several lineedit
and combobox
for the user to enter information.
所以,我想要的是:启动程序时,它将首先使用以下命令调出 QMainWindow
类 widget1
设置为中央小工具
。
So, what I want is: when the program is launched, it will firstly bring up the QMainWindow
class with widget1
set as the central widget
.
widget1
具有检查功能
,该功能已连接到一个按钮上。复选按钮将根据用户在页面上输入的数据来检查条件是否为真。
The widget1
has a Check function
which is connected to a button on it. The check button will check whether a condition is true based on the data entered by the user on the page.
如果条件为真。我希望 widget2
将其设置为 MainWindow
中央假发 >替换现有的中央小工具
,即 widget1
。
If the condition is true. I want the widget2
to set it as central wiget
of MainWindow
to replace existing central widget
, the widget1
.
我的问题是,如何将 widget2
设置为现有 MainWidnow
类实例的中央小部件?
My question is, how do I set the widget2
as the central widget of existing MainWidnow
class instance?
这是我的代码格式:
class widget1(QtGui.QFrame):
def __init__(self,parent = None):
......
......
def Check(self):
if (condition):
#set widget2 as central widget on MainWindow
class widget2(QtGui.QFrame):
def __int__(self,parent = None):
.....
.....
class MainWindow(QtGui.QMainWindow):
def __init__(self,parent = None):
QtGui.QMainWindow.__init__(self,parent)
....
mywidgetone = widget1()
self.setCentralWidget(mywidgetone)
if __name__ == '__main__':
app = QtGui.QApplicaiton(sys.argv)
main = MainWindow()
main.show()
app.exec_()
推荐答案
我将对 MainWindow
:
class MainWindow(QtGui.QMainWindow):
def __init__(self,parent = None):
QtGui.QMainWindow.__init__(self,parent)
....
self.setMyCentral(widget1)
def setMyCentral(self, widgetClass):
mywidget = widgetClass(self)
self.setCentralWidget(mywidget)
,然后在 widget1
中:
def Check(self):
if (condition):
self.parent().setMyCentral(widget2)
现在,请遵循以下约定:类以大写字母开头( Widget1
, Widget2
)和方法不要(检查
而不是检查
)
Now, please, follow the conventions: classes start in capital letters (Widget1
, Widget2
) and methods don't (check
instead of Check
)
这篇关于如何在Python PyQt4中设置现有MainWidnow的中央小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!