简体   繁体   中英

How can I switch to another screen (without opening a new window) in Pyqt5

I know this question has been asked multiple times but I am having a hard time to understand examples, so all I have this first screen and I want, when I click on this Go to page 2 button , It should switch to the second screen without opening a different window.

step -1 Click on Go to page 2 button

点击“转到第 2 页”按钮

step -2 Switch to 2nd screen

切换到第二个屏幕

step -3 Switch Back to 1st screen

This is the code I have for both screens, I don't want to loadUI file

################ File1.py ##############

class Ui_PageFirst(object):
    def setupUi(self, PageFirst):
        PageFirst.setObjectName("PageFirst")
        PageFirst.resize(123, 132)
        self.centralwidget = QtWidgets.QWidget(PageFirst)
        self.centralwidget.setObjectName("centralwidget")
        self.FirstPageLable = QtWidgets.QLabel(self.centralwidget)
        self.FirstPageLable.setGeometry(QtCore.QRect(0, 0, 121, 71))
        self.FirstPageLable.setAlignment(QtCore.Qt.AlignCenter)
        self.FirstPageLable.setObjectName("FirstPageLable")
        self.btn1 = QtWidgets.QPushButton(self.centralwidget)
        self.btn1.setGeometry(QtCore.QRect(20, 80, 75, 23))
        self.btn1.setObjectName("btn1")
        PageFirst.setCentralWidget(self.centralwidget)

        self.retranslateUi(PageFirst)
        QtCore.QMetaObject.connectSlotsByName(PageFirst)

    def retranslateUi(self, PageFirst):
        _translate = QtCore.QCoreApplication.translate
        PageFirst.setWindowTitle(_translate("PageFirst", "MainWindow"))
        self.FirstPageLable.setText(_translate("PageFirst", "This is page First."))
        self.btn1.setText(_translate("PageFirst", "Go to page 2"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    PageFirst = QtWidgets.QMainWindow()
    ui = Ui_PageFirst()
    ui.setupUi(PageFirst)
    PageFirst.show()
    sys.exit(app.exec_())

################ File2.py ##############

class Ui_PageSecond(object):
    def setupUi(self, PageSecond):
        PageSecond.setObjectName("PageSecond")
        PageSecond.resize(123, 132)
        self.centralwidget = QtWidgets.QWidget(PageSecond)
        self.centralwidget.setObjectName("centralwidget")
        self.secondPageLable = QtWidgets.QLabel(self.centralwidget)
        self.secondPageLable.setGeometry(QtCore.QRect(0, 0, 121, 71))
        self.secondPageLable.setAlignment(QtCore.Qt.AlignCenter)
        self.secondPageLable.setObjectName("secondPageLable")
        self.btn2 = QtWidgets.QPushButton(self.centralwidget)
        self.btn2.setGeometry(QtCore.QRect(20, 80, 75, 23))
        self.btn2.setObjectName("btn2")
        PageSecond.setCentralWidget(self.centralwidget)

        self.retranslateUi(PageSecond)
        QtCore.QMetaObject.connectSlotsByName(PageSecond)

    def retranslateUi(self, PageSecond):
        _translate = QtCore.QCoreApplication.translate
        PageSecond.setWindowTitle(_translate("PageSecond", "MainWindow"))
        self.secondPageLable.setText(_translate("PageSecond", "This is page Second."))
        self.btn2.setText(_translate("PageSecond", "Go to page 1"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    PageSecond = QtWidgets.QMainWindow()
    ui = Ui_PageSecond()
    ui.setupUi(PageSecond)
    PageSecond.show()
    sys.exit(app.exec_())

You have to use QStackedWidget .

class Ui_PageFirst(QMainWindow):    #Page1
    def changeToPage2(self):
        widget.setCurrentWidget(secondpage)

class Ui_PageSecond(QMainWindow):   #Page2
    def changeToPage1(self):
        widget.setCurrentWidget(firstpage)

app = QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
#######
firstpage = Ui_PageFirst()
widget.addWidget(firstpage)   # create an instance of the first page class and add it to stackedwidget

secondpage = Ui_PageSecond() 
widget.addWidget(secondpage)   # adding second page

widget.setCurrentWidget(firstpage)   # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)
########
widget.show()
app.exec_()

You will need 3 UI's for that. Just follow the procedure:

  1. Create a main_UI with this structure: QMainWindow > QFrame (centralWidget) > QLayout > another_q_frame (this frame will contain both of your main pages)

  2. Now create 2 other QWidget (lets call them screen_1 & screen_2 ) windows containing one button each (this button would be used to switch between the screens)

  3. Add screen_1 to another_q_frame of main_UI programmatically. For example: if you using qtdesigner, you can simply call .Setup UI(another_q_frame) . There are many other ways to do this too.

  4. Now create a function which will switch the Qwidget (screen_1 & screen_2) in another_q_frame. Ex:

def switch_fctn(self, class_instance):
    class_instance.setupUI(self.another_q_frame)
  1. Finally just connect this function to those button we created in both screen_1 and screen_2 using self.button.clicked.connect(lambda: self.switch_fctn(screen_1_class_instance)) for screen_2 button and also do the same for screen_1 button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM