Hello, I understand with PyQt5, for some reason, I don’t create a menu ...

although a similar, in my opinion, toolbar is being created, tell the noob what the catch is!)

import sys from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): exitAction = QAction(QIcon('exit.png'), 'Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit application') exitAction.triggered.connect(self.close) self.statusBar() # ! тут ничего не происходит menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(exitAction) # ! а тут работает toolbar = self.addToolBar('Exit') toolbar.addAction(exitAction) self.setGeometry(300, 300, 350, 250) self.setWindowTitle('Main window') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) 

    2 answers 2

    setNativeMenuBar

    as an example:

     import random, string from PyQt5 import QtWidgets, QtCore class MainWidget(QtWidgets.QWidget): '''центральный виджет''' def __init__(self): super().__init__() layout = QtWidgets.QFormLayout() self.text = QtWidgets.QTextBrowser() layout.addWidget(self.text) self.setLayout(layout) def cmd(self): [self.text.append(str(a)) for a in range(20)] class MainWindow(QtWidgets.QMainWindow): def __init__(self): QtWidgets.QMainWindow.__init__(self) self.central = MainWidget() self.setCentralWidget(self.central) self.menu_bar() self.tool_bar() self.status_bar() self.show() def menu_bar(self): self.menuBar().setNativeMenuBar(False) exit_menu = self.menuBar().addMenu('&Exit') exit_ = QtWidgets.QAction('&Exit', self) exit_.setShortcut(self.tr("Ctrl+Q")) exit_.setStatusTip(self.tr("Exit the application")) exit_.triggered.connect(self.close) exit_menu.addAction(exit_) def tool_bar(self): toolbar = QtWidgets.QToolBar(self) self.addToolBar(QtCore.Qt.BottomToolBarArea, toolbar) action = QtWidgets.QAction('Toolbar text', toolbar) action.triggered.connect(self.central.cmd) toolbar.addAction(action) def status_bar(self): sb = QtWidgets.QStatusBar() self.setStatusBar(sb) ed = QtWidgets.QPushButton('StatusBar text') self.statusBar().addPermanentWidget(ed) ed.clicked.connect(lambda: self.statusBar().showMessage(random.choice(string.ascii_letters))) q = QtWidgets.QApplication([]) m = MainWindow() q.exec_() 
    • the menu is not displayed; screen - MoRDa
    • 2
      try adding self.menuBar (). setNativeMenuBar (False) - vadim vaduxa
    • one
      it worked, thanks, but could you explain what the problem was and how this line resolves it? - MoRDa
    • one

    You have it all right. Just the program does not find the file exit.png. I just threw the file into the working directory and it all worked.