Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. How to create a menu with mitil-pages by Pyside2
Forum Updated to NodeBB v4.3 + New Features

How to create a menu with mitil-pages by Pyside2

Scheduled Pinned Locked Moved Solved Language Bindings
2 Posts 1 Posters 710 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    York Wu
    wrote on last edited by
    #1

    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:)

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      York Wu
      wrote on last edited by
      #2

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

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

      new_widget.show()
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved