Advertisement
robertvari

Dialog Test

Mar 16th, 2021
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. from PySide2.QtWidgets import QMainWindow, QDialog, QVBoxLayout, QPushButton, QApplication, QWidget, QLabel
  2. from PySide2.QtCore import Qt
  3. import sys
  4.  
  5.  
  6. class MainWindow(QMainWindow):
  7.     def __init__(self):
  8.         super(MainWindow, self).__init__()
  9.         self.resize(800, 600)
  10.         self.setWindowTitle("Test Window")
  11.  
  12.         central_widget = QWidget()
  13.         main_layout = QVBoxLayout(central_widget)
  14.         self.setCentralWidget(central_widget)
  15.  
  16.         button = QPushButton("New Game")
  17.         main_layout.addWidget(button)
  18.         main_layout.setAlignment(button, Qt.AlignTop)
  19.  
  20.         self.game_window = GameWindow(self)
  21.         self.start_game_dialog = StartGameWindow(self)
  22.         self.start_game_dialog.accepted.connect(self._new_game_started)
  23.         self.start_game_dialog.rejected.connect(self._new_game_rejected)
  24.  
  25.         button.clicked.connect(self.start_game_dialog.show)
  26.         self.start_game_dialog.show()
  27.         self.game_window.show()
  28.  
  29.     def _new_game_started(self):
  30.         print("New game started")
  31.         self.game_window.show()
  32.  
  33.     def _new_game_rejected(self):
  34.         print("New game rejected")
  35.         self.game_window.close()
  36.  
  37.  
  38. class GameWindow(QDialog):
  39.     def __init__(self, parent):
  40.         super().__init__(parent)
  41.         main_layout = QVBoxLayout(self)
  42.         self.resize(300, 200)
  43.         self.setWindowTitle("Game Window")
  44.  
  45.         main_layout.addWidget(QLabel("Game Window"))
  46.  
  47.  
  48. class StartGameWindow(QDialog):
  49.     def __init__(self, parent):
  50.         super().__init__(parent)
  51.         self.setWindowTitle("New Game")
  52.         main_layout = QVBoxLayout(self)
  53.         self.setModal(True)
  54.  
  55.         main_layout.addWidget(QLabel("Start New Game"))
  56.  
  57.         ok_button = QPushButton("Ok")
  58.         cancel_button = QPushButton("Cancel")
  59.  
  60.         main_layout.addWidget(ok_button)
  61.         main_layout.addWidget(cancel_button)
  62.  
  63.         ok_button.clicked.connect(self.accept)
  64.         cancel_button.clicked.connect(self.reject)
  65.  
  66.  
  67. if __name__ == '__main__':
  68.     app = QApplication(sys.argv)
  69.     win = MainWindow()
  70.     win.show()
  71.     app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement