Opening of the window through menubar



  • I'm trying to realize the discovery. 2-го windows when applied to the button menubarbut something's going wrong.

    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication, QAction, QWidget
    

    lst = ["1", "2", "3", "4"]

    class MainWindow(QMainWindow):
    def init(self):
    super().init()
    self.initUI()

    def initUI(self):
        about = QAction('&О программе', self)
    
        menu = self.menuBar()
        cpravka = menu.addMenu('&Справка')
        cpravka.self.addAction(about)
    
        self.setGeometry(300, 300, 800, 500)
        self.setWindowTitle('MainWindow')
    
        self.init_handlers()
    
    def init_handlers(self):  # обработка открытия 2 окна
        self.about.clicked.connect(self.show_window_2)
    
    def show_window_2(self):  # открытие 2  окна
        self.w2 = Window2()
        self.w2.show()
    

    class Window2(QWidget):
    def init(self):
    super(Window2, self).init()
    self.setWindowTitle('Window2')

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



  • As an option:

    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication, QAction, QWidget
    

    class Window2(QWidget):
    def init(self):
    super(Window2, self).init()
    self.setWindowTitle('Window2')

    class MainWindow(QMainWindow):
    def init(self):
    super().init()
    self.setGeometry(300, 300, 800, 500)
    self.setWindowTitle('MainWindow')

        self.initUI()
    
    def initUI(self):
        about = QAction('&О программе', self)
    
        menu = self.menuBar()
        
        cpravka = menu.addMenu('&Справка')                     # +++
        self.newAction = cpravka.addAction("О программе")      # +++
        cpravka.triggered.connect(self.show_window_2)          # +++ triggered !!!
    
    def show_window_2(self, action):  # открытие 2  окна
        if action == self.newAction:
            self.w2 = Window2()
            self.w2.show()
    

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

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



Suggested Topics

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