複数のウィンドウを備えた PyQt アプリケーションを作成しています。今のところ、一度に 2 つのウィンドウのうちの 1 つを開くことに興味があります (つまり、1 つのウィンドウでボタンをクリックすると、別のウィンドウに切り替わります)。PyQt アプリケーションで複数のウィンドウを追跡する合理的な方法は何ですか? 私の最初の試みは、以下に示すように、単純なクラスのグローバルQtGui.QWidget
インスタンスのin data メンバーのインスタンスを基本的に格納します。
私は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()