|
| 1 | +importsys |
| 2 | + |
| 3 | +fromPyQt5.QtWidgetsimport ( |
| 4 | +QApplication, |
| 5 | +QHBoxLayout, |
| 6 | +QMainWindow, |
| 7 | +QPushButton, |
| 8 | +QStackedLayout, |
| 9 | +QVBoxLayout, |
| 10 | +QWidget, |
| 11 | +) |
| 12 | + |
| 13 | +fromlayout_colorwidgetimportColor |
| 14 | + |
| 15 | + |
| 16 | +classMainWindow(QMainWindow): |
| 17 | +def__init__(self): |
| 18 | +super().__init__() |
| 19 | +self.setWindowTitle("My App") |
| 20 | + |
| 21 | +pagelayout=QVBoxLayout() |
| 22 | +button_layout=QHBoxLayout() |
| 23 | +self.stacklayout=QStackedLayout() |
| 24 | + |
| 25 | +pagelayout.addLayout(button_layout) |
| 26 | +pagelayout.addLayout(self.stacklayout) |
| 27 | + |
| 28 | +btn=QPushButton("red") |
| 29 | +btn.pressed.connect(self.activate_tab_1) |
| 30 | +button_layout.addWidget(btn) |
| 31 | +self.stacklayout.addWidget(Color("red")) |
| 32 | + |
| 33 | +btn=QPushButton("green") |
| 34 | +btn.pressed.connect(self.activate_tab_2) |
| 35 | +button_layout.addWidget(btn) |
| 36 | +self.stacklayout.addWidget(Color("green")) |
| 37 | + |
| 38 | +btn=QPushButton("yellow") |
| 39 | +btn.pressed.connect(self.activate_tab_3) |
| 40 | +button_layout.addWidget(btn) |
| 41 | +self.stacklayout.addWidget(Color("yellow")) |
| 42 | + |
| 43 | +widget=QWidget() |
| 44 | +widget.setLayout(pagelayout) |
| 45 | +self.setCentralWidget(widget) |
| 46 | + |
| 47 | +defactivate_tab_1(self): |
| 48 | +self.stacklayout.setCurrentIndex(0) |
| 49 | + |
| 50 | +defactivate_tab_2(self): |
| 51 | +self.stacklayout.setCurrentIndex(1) |
| 52 | + |
| 53 | +defactivate_tab_3(self): |
| 54 | +self.stacklayout.setCurrentIndex(2) |
| 55 | + |
| 56 | + |
| 57 | +app=QApplication(sys.argv) |
| 58 | +window=MainWindow() |
| 59 | +window.show() |
| 60 | +app.exec() |