Switching Screens: Utilizing the Stacked Widget --------------------------------------------------- - ``Screen Transition``: Learn how to switch between multiple screens using the Stacked Widget. - In PyQt, the ``QStackedWidget`` allows for the display of one screen (or page) at a time out of multiple available screens. - In this example, three pages are added to the ``QStackedWidget``. Each page contains a button, enabling a switch to a different page. .. code-block:: python :caption: scripts/PYQT2/03_toggle_screens/main.py :linenos: # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QStackedWidget, QLabel class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() # Create the stacked widget self.stack = QStackedWidget() # Create some sample pages self.page1 = QWidget() self.page2 = QWidget() self.page3 = QWidget() # Add pages to the stacked widget self.stack.addWidget(self.page1) self.stack.addWidget(self.page2) self.stack.addWidget(self.page3) # Sample layout for page 1 layout1 = QVBoxLayout(self.page1) layout1.addWidget(QLabel("여긴 1 페이지입니다.")) button1 = QPushButton("2 페이지로 가자") layout1.addWidget(button1) button1.clicked.connect(self.go_to_page2) # Sample layout for page 2 layout2 = QVBoxLayout(self.page2) layout2.addWidget(QLabel("여긴 2 페이지라고라")) button2 = QPushButton("3 페이지로 가기") layout2.addWidget(button2) button2.clicked.connect(self.go_to_page3) # Sample layout for page 3 layout3 = QVBoxLayout(self.page3) layout3.addWidget(QLabel("여긴 3 페이지라고하지")) button3 = QPushButton("1 페이지로 가볼까") layout3.addWidget(button3) button3.clicked.connect(self.go_to_page1) # Set the current widget to be page 1 self.stack.setCurrentWidget(self.page1) # Set the stacked widget as the central widget of the window self.setCentralWidget(self.stack) def go_to_page1(self): self.stack.setCurrentWidget(self.page1) def go_to_page2(self): self.stack.setCurrentWidget(self.page2) def go_to_page3(self): self.stack.setCurrentWidget(self.page3) if __name__ == '__main__': app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() sys.exit(app.exec_()) - By clicking the button, the ``clicked.connect()`` method is utilized to execute the connected slot function. Within that function, the ``setCurrentWidget()`` method is used to change the current screen. - ``QStackedWidget``: A widget that contains multiple pages and displays only one at a time. - ``setCurrentWidget()``: A method in ``QStackedWidget`` to set the currently displayed page. Detailed Analysis ~~~~~~~~~~~~~~~~~~~ 1. Creating the stacked widget: .. code-block:: python self.stack = QStackedWidget() 2. Creating and adding sample pages: .. code-block:: python self.page1 = QWidget() ... self.stack.addWidget(self.page1) 3. Setting content for each page: Each page contains a label and a button. Clicking the button will switch to a different page. .. code-block:: python layout1 = QVBoxLayout(self.page1) ... button1 = QPushButton("Go to page 2") 4. Connecting button click events: Use methods like ``go_to_page2`` to connect each button to the logic that switches to the respective page. .. code-block:: python button1.clicked.connect(self.go_to_page2) 5. Setting the initial page: This sets which page will be displayed by default when the app runs. .. code-block:: python self.stack.setCurrentWidget(self.page1)