PyQt5中缺少menuBar [英] Missing menuBar in PyQt5

查看:434
本文介绍了PyQt5中缺少menuBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用PyQt5开发GUI,并希望包含一个菜单栏.但是,当我编写此功能的代码时,将不会出现我的菜单.弄清我对如何在PyQt5中实现菜单栏的理解之后,我在网上寻找了一个预先存在的示例.通过一些调整,我开发了以下测试用例:

I've been developing a GUI using PyQt5 and wanted to include a menu bar. When I went to code this feature, however, my menu wouldn't appear. Figuring my understanding on how to implement menu bars in PyQt5 was off, I looked for a pre-existing example online. With some tweaking I developed the following test case:

import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenuBar, QAction, qApp

class Example(QMainWindow):

    def __init__(self):
        super().__init__()

        exitAction = QAction(QIcon('exit.png'), '&Exit', self)
        exitAction.triggered.connect(qApp.quit)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&Testmenu')
        fileMenu.addAction(exitAction)

        self.show()


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

但是,运行此命令时,找不到Testmenu.

When I run this, however, Testmenu is nowhere to be found.

在使用pyuic5将.ui文件转换为可导入的.py之前,我还尝试了在QTCreator中创建菜单栏(以及我的GUI布局的其余部分).我以为这样可以消除一些编程错误,但是菜单栏仍然不会显示.有什么想法吗?

I have also tried creating the menu bar (and the rest of my GUI layout) in QTCreator before converting the .ui file to an importable .py using pyuic5. I thought this would eliminate some programming mistake on my end, but the menubar still won't show. Any thoughts?

我从Jupyter笔记本4.1版中使用Python 3.5(Anaconda 4.1)运行此代码.我还使用运行OS 10.1l,PyQt 5.7和Qt版本5.7.0的Macbook.

Im running this code using Python 3.5 (Anaconda 4.1) from within a Jupyter notebook, version 4.1. I'm also using a Macbook running os 10.1l, PyQt 5.7 and Qt version 5.7.0.

我已经意识到,如果我单击应用程序窗口,然后单击回到窗口,菜单栏将变得敏感-有效地使应用程序失去焦点并集中精力.有了这些信息,我意识到我不是第一个注意到此问题的人(请参阅 https://github .com/robotology/yarp/issues/457 ).不幸的是,我仍然不确定如何解决该问题.

I've realized that the menu bar will become responsive if I click off the application window and then click back onto the window - effectively unfocusing and the focusing the application. Armed with this information I realized that I am not the first to notice this problem (see https://github.com/robotology/yarp/issues/457). Unfortunately, I'm still not sure how to resolve the issue.

推荐答案

它不是Qt和PyQt5错误.

It's not a Qt and PyQt5 Bug.

我认为您的代码是 zetcode pyqt5菜单栏教程.我在Mac OS上遇到了完全相同的问题.

I think your code is zetcode pyqt5 menubar tutorial. I experienced the exact same problem on Mac OS.

第一个解决方案是一个把戏.使用' &Exit'代替'&Exit'.像这样在'&Exit'的开头插入一个空格:

First solution is a trick. Use ' &Exit' instead of '&Exit'. Insert a space at the beginning of '&Exit' like this:

...
# exitAction = QAction(QIcon('exit.png'), '&Exit', self) # Not shown
exitAction = QAction(QIcon('exit.png'), ' &Exit', self)
...

macOS的系统级菜单栏保留诸如"Exit""Quit"等之类的关键字.出于相同的原因, yurisnm的示例代码仅显示Mac OS上除"Quit"以外的菜单项.实际上,退出"具有 TextHeuristicRole ,因此覆盖了退出"行为在应用程序菜单中.当您在"Python"菜单中单击退出python"时,它不会退出,而仅显示退出触发".

The system-wide menubar of macOS reserves keywords such as "Exit", "Quit", and etc. For the same reason, yurisnm's example code shows only the menu items except "Quit" on Mac OS. Actually "Quit" has TextHeuristicRole, so overrides "Quit " behavior in the Application menu. When you click "Quit python" in "Python" menu, it does not quit and just print "quit triggered".

如果必须在其他菜单中使用该名称(例如,文件,编辑),则需要像上面一样更改操作名称,或使用QAction::setMenuRole(...)这样:

If you must use that name in other menu(e.g. File, Edit), you need to change the action name like above or use QAction::setMenuRole(...) like this:

...
exitAction = QAction(QIcon('exit.png'), '&Exit', self)
print(exitAction.menuRole()) # It prints "1". QAction::TextHeuristicRole
exitAction.setMenuRole(QAction.NoRole)
...

请阅读以下内容,它将为您提供帮助.

Please read the following, it will be help you.

http://doc.qt.io/qt-5/qmenubar.html#qmenubar-as-a-global-menu-bar

这篇关于PyQt5中缺少menuBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆