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

Posted

技术标签:

【中文标题】简单的 PyQt5 教程不显示菜单栏【英文标题】:Simple PyQt5 tutorial does not show menu bar 【发布时间】:2019-05-08 09:49:25 【问题描述】:

窗口出现,但菜单栏只显示“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 菜单栏和窗口左上部分的屏幕截图:

更新:我发现了这个问题:I need help making a menu bar in PyQt5 关于同样的问题。我运行了适用于 OP 的代码并且它有效!我将尝试找出该代码与我的代码之间的差异并再次发布。

【问题讨论】:

我记得Mac OS中的菜单栏不是出现在窗口的上部而是出现在屏幕的上部。见pngkey.com/png/detail/… 对,这正是我所期望的。我刚刚添加了一个部分屏幕转储来显示我得到了什么。 你是如何启动应用程序的? 无论是从命令行启动还是从 PyCharm 的“运行”菜单启动,我都会得到相同的结果。 对我来说,我不得不使用 mainmenu = self.menuBar(); mainmenu.setNativeMenuBar(False) 【参考方案1】:

这是 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_())

【讨论】:

以上是关于简单的 PyQt5 教程不显示菜单栏的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5不同方法创建菜单栏工具栏和状态栏

PyQt5——DAY3

在PyQt5中的菜单栏和工具栏

PyQt5菜单添加+事件+状态栏-7

如何使用按钮在 PyQt5 中的两个菜单栏之间切换?菜单栏消失

Python Tkinter 菜单栏不显示