How to create a menu with mitil-pages by Pyside2



  • I would like to create a meun with several pages, just like the menu of many PC game.

    For example, here is my trial. In first page, there are two button, "Start game" and "Exit". If Start game button is clicked, it would go to next page, with a button "back". So when it be clicked, I deleted all two buttons in first page, and create a new button "back" that belong to 2nd page.

    However, it doesn't work as my expect. two buttons of first page were deleted, while "back" button not appeared.

    here is my code:

    import sys
    from PySide2 import QtCore, QtGui, QtWidgets
    
    class Main_meun(object):
        def __init__(self):
            
            self.start_button = QtWidgets.QPushButton(MainWindow)
            self.start_button.setGeometry(QtCore.QRect(90, 150, 221, 80))
            self.start_button.setIconSize(QtCore.QSize(250, 80))
            self.start_button.setText("Start a game")
            self.start_button.clicked.connect(self.start)
            
            self.ex_button = QtWidgets.QPushButton(MainWindow)
            self.ex_button.setGeometry(QtCore.QRect(90, 250, 221, 80))
            self.ex_button.setIconSize(QtCore.QSize(250, 80))
            self.ex_button.setText("Exit")
            self.ex_button.clicked.connect(app.quit)
    
        def start(self):
            print("Game Start")
            global ui
            ui = Game()      
            for widget in [self.start_button, self.ex_button]:
                widget.deleteLater()
    
    class Game(object):
        def __init__(self):
    
            self.back_button = QtWidgets.QPushButton(MainWindow)
            self.back_button.setGeometry(QtCore.QRect(90, 150, 221, 80))
            self.back_button.setIconSize(QtCore.QSize(250, 80))
            self.back_button.clicked.connect(self.back)
    
        def back(self):
            print("back")
    
    app = QtWidgets.QApplication(sys.argv)
    
    MainWindow = QtWidgets.QMainWindow()
    MainWindow.resize(640, 480)
    
    ui = Main_meun()
     
    MainWindow.show()
    sys.exit(app.exec_())
    

    I hope experts can help me in this problem, thank you:)



  • My god... I thought I have a answer...

    just use this for each widgets I want to create...

    new_widget.show()
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.