简单的PyQt5教程不显示菜单栏

时间:2019-05-08 09:49:25

标签: pyqt5 python-3.7

出现窗口,但菜单栏仅显示“ Python3.7”,即 它显示了我是否不创建菜单栏。这几乎完全来自教程here。 MacOS High Sierra。

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

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Menu Bar'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('File')
        editMenu = mainMenu.addMenu('Edit')
        viewMenu = mainMenu.addMenu('View')
        searchMenu = mainMenu.addMenu('Search')
        toolsMenu = mainMenu.addMenu('Tools')
        helpMenu = mainMenu.addMenu('Help')
        self.show()   

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

这是Mac菜单栏和窗口左上方的屏幕截图: enter image description here

更新:我发现了这个问题:I need help making a menu bar in PyQt5关于同一问题。我运行了适用于OP的代码,它起作用了!我将尝试找出该代码与我的代码之间的区别,然后再次发布。

1 个答案:

答案 0 :(得分:0)

这是MacOS问题。我尚未对此问题进行深入研究,但似乎有两件事很明确:空菜单将不会显示,菜单项的名称开头 “关于”,“退出”,“退出”将放置在程序菜单下。

在OS X下玩此游戏

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication

class Window(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 menu bar test'
        self.initUI()

    def print_change(self):
        print('change')

    def print_byebye(self):
        print('bye bye')

    def initUI(self):
        self.setWindowTitle(self.title)

        menubar = self.menuBar()

        filemenu = menubar.addMenu('File')

        """ Play with these options in Mac OS X:

            confuse_me = False, False

            You see a File menu with two actions "Change" and "Bye Bye" respectively 
            printing "change" and "bye bye". The program menu contains "Services...", 
            "Hide ...", "Hide Others", and "Quit ..." which exits the program.

            confuse_me = True, False

            The File menu contains only "Bye Bye". The "About" item appears in the program 
            menu and prints "change". The "Quit" item still quits. 

            confuse_me = False, True

            The File menu containd only "Change". There is no "About" in the program menu,
            and the "Quit" item prints "bye bye"

            confuse_me = True, True

            The program menu has "About" and "Quit" respectively printing "change" and "bye 
            bye". These is no File menu.
        """

        confuse_me = False, False

        text = "About a Boy" if confuse_me[0] else "Change"
        pathaction = filemenu.addAction(text)
        pathaction.triggered.connect(self.print_change)

        text = "Quitting is not an Option" if confuse_me[1] else "Bye Bye"
        exitaction = filemenu.addAction(text)
        exitaction.triggered.connect(self.print_byebye)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    print(sys.argv)
    w = Window()
    w.setGeometry(500, 300, 300, 300)
    w.show()

    sys.exit(app.exec_())