PyQt5 daughter window



  • Help me create a subsidiary window that will close with him when the main window is closed, the bottom window itself must be different.

    Unfortunately, I only know how to create a regular window.

    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
    
        self.setWindowTitle("Lab 2-3")
    
        self.centralWidget = QWidget(self)
        self.setCentralWidget(self.centralWidget)
        self.grid_layout = QGridLayout(self.centralWidget)
    
        self.setGeometry(590, 300, 480, 215)
        self.setStyleSheet("background : white;")
    



  • Try this:

    import sys
    from PyQt5.Qt import *
    

    class Window_2(QMainWindow): # !!! QMainWindow
    def init(self, parent=None):
    super().init(parent) # !!! parent
    self.setWindowTitle("Window_2")
    self.resize(200, 215)

    class MainWindow(QMainWindow):
    def init(self):
    super(MainWindow, self).init()
    self.setWindowTitle("Lab 2-3")
    self.setGeometry(590, 300, 480, 215)
    self.setStyleSheet("background : white;")

        self.centralWidget = QWidget(self)
        self.setCentralWidget(self.centralWidget)
    
        self.button = QPushButton('Создать второе окно')
        self.button.clicked.connect(self.create_window) 
        
        self.grid_layout = QGridLayout(self.centralWidget)
        self.grid_layout.addWidget(self.button)
        
    def create_window(self):
        self.window = Window_2(self)                        # !!! self
        self.window.show()
    

    if name == "main":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

    введите сюда описание изображения



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2