【发布时间】:2014-02-24 19:25:50
【问题描述】:
我正在编写一个具有多个窗口的 PyQt 应用程序。现在,我有兴趣一次打开两个窗口中的一个(因此在一个窗口中单击一个按钮会导致切换到另一个窗口)。在 PyQt 应用程序中跟踪多个窗口的合理方法是什么?如下所示,我最初的尝试基本上是将QtGui.QWidget 的实例存储在一个简单类的全局 实例的数据成员中。
我是 PyQt 的新手。有没有更好的方法来解决这个问题?
#!/usr/bin/env python
import sys
from PyQt4 import QtGui
class Program(object):
def __init__(
self,
parent = None
):
self.interface = Interface1()
class Interface1(QtGui.QWidget):
def __init__(
self,
parent = None
):
super(Interface1, self).__init__(parent)
self.button1 = QtGui.QPushButton(self)
self.button1.setText("button")
self.button1.clicked.connect(self.clickedButton1)
self.layout = QtGui.QHBoxLayout(self)
self.layout.addWidget(self.button1)
self.setGeometry(0, 0, 350, 100)
self.setWindowTitle('interface 1')
self.show()
def clickedButton1(self):
self.close()
program.interface = Interface2()
class Interface2(QtGui.QWidget):
def __init__(
self,
parent = None
):
super(Interface2, self).__init__(parent)
self.button1 = QtGui.QPushButton(self)
self.button1.setText("button")
self.button1.clicked.connect(self.clickedButton1)
self.layout = QtGui.QHBoxLayout(self)
self.layout.addWidget(self.button1)
self.setGeometry(0, 0, 350, 100)
self.setWindowTitle('interface 2')
self.show()
def clickedButton1(self):
self.close()
program.interface = Interface1()
def main():
application = QtGui.QApplication(sys.argv)
application.setApplicationName('application')
global program
program = Program()
sys.exit(application.exec_())
if __name__ == "__main__":
main()
【问题讨论】:
标签: windows pyqt data-members