PyQt5 中缺少 menuBar
Missing menuBar in PyQt5
我一直在使用 PyQt5 开发 GUI,并希望包含一个菜单栏。但是,当我编写此功能的代码时,我的菜单不会出现。弄清楚我对如何在 PyQt5 中实现菜单栏的理解是错误的,我在网上查找了一个预先存在的示例。通过一些调整,我开发了以下测试用例:
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
却找不到了。
在使用 pyuic5 将 .ui 文件转换为可导入的 .py 之前,我还尝试在 QTCreator 中创建菜单栏(以及我的 GUI 布局的其余部分)。我认为这会消除我这边的一些编程错误,但菜单栏仍然不会显示。有什么想法吗?
编辑:
Im 运行使用 Python 3.5 (Anaconda 4.1) 从 Jupyter notebook 4.1 版中编写此代码。我也在使用 Macbook 运行ning os 10.1l、PyQt 5.7 和 Qt 5.7.0 版。
我已经意识到,如果我单击关闭应用程序 window 然后再单击回到 window,菜单栏将变得响应 - 有效地取消焦点和聚焦应用程序。有了这些信息,我意识到我不是第一个注意到这个问题的人(参见 https://github.com/robotology/yarp/issues/457)。不幸的是,我仍然不确定如何解决这个问题。
试试这个:
menubar = QMenuBar()
self.setMenuBar(menubar)
而不是menubar = self.menuBar()
下面这个最简单的例子你试过了吗linktutorialspoint.
这是最简单的例子。
import sys
from PyQt5.QtWidgets import QHBoxLayout, QAction, QApplication, QMainWindow
class menudemo(QMainWindow):
def __init__(self, parent = None):
super(menudemo, self).__init__(parent)
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
save = QAction("Save",self)
save.setShortcut("Ctrl+S")
file.addAction(save)
edit = file.addMenu("Edit")
edit.addAction("copy")
edit.addAction("paste")
quit = QAction("Quit",self)
file.addAction(quit)
file.triggered[QAction].connect(self.processtrigger)
self.setWindowTitle("menu demo")
def processtrigger(self, q):
print(q.text()+" is triggered")
def main():
app = QApplication(sys.argv)
ex = menudemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
这不是 Qt 和 PyQt5 的错误。
我认为你的代码是 zetcode pyqt5 menubar tutorial。我在 Mac OS.
上遇到了完全相同的问题
第一个解决方案是一个技巧。使用 ' &Exit'
而不是 '&Exit'
。在'&Exit'
开头插入一个space,像这样:
...
# exitAction = QAction(QIcon('exit.png'), '&Exit', self) # Not shown
exitAction = QAction(QIcon('exit.png'), ' &Exit', self)
...
mac系统范围的菜单栏OS保留了"Exit"
、"Quit"
等关键词,同理 shows only the menu items except "Quit"
on Mac OS. Actually "Quit" has TextHeuristicRole,所以覆盖了"Quit " 应用程序菜单中的行为。当你点击 "Python" 菜单中的 "Quit python" 时,它不会退出,只是打印 "quit triggered".
如果您必须在其他菜单中使用该名称(例如文件、编辑),您需要像上面那样更改操作名称或使用 QAction::setMenuRole(...)
像这样:
...
exitAction = QAction(QIcon('exit.png'), '&Exit', self)
print(exitAction.menuRole()) # It prints "1". QAction::TextHeuristicRole
exitAction.setMenuRole(QAction.NoRole)
...
请阅读以下内容,对您有所帮助。
如果您的程序在 Ubuntu 上 运行,您可能会在屏幕顶部找到菜单栏。
如果要将菜单栏移动到 window 的标题栏,可以在 "System setting / Appearance / Behavior / Show the menus for a window / In the window's title bar" 中切换设置。
菜单栏在 PyQt5 中不可见
bar = self.menuBar()
bar.setNativeMenuBar(False)
file = bar.addMenu("File")
file.addAction("New")
NativeMenuBar 属性 指定菜单栏是否应在支持它的平台上用作本机菜单栏。如果此 属性 为真,则菜单栏将在本机菜单栏中使用,而不是在其父级的 window 中使用,如果为假,则菜单栏将保留在 window 中。
示例程序
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
exitAct = QAction(QIcon('exit.png'), ' &Quit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')
exitAct.triggered.connect(qApp.quit)
self.statusBar()
menubar = self.menuBar()
menubar.setNativeMenuBar(False)
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
bar = self.menuBar()
file = bar.addMenu("Edit")
file.addAction("New")
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Simple menu')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Menu()
sys.exit(app.exec_())
在 MacOS 下,应用程序菜单出现在屏幕顶部。通过在字符串 "Quit" 前加上一个空字符,我设法让“退出”菜单选项出现在上面的示例中,如下所示:
close = QAction("[=10=]Quit",self)
close.setShortcut("Ctrl+Q")
file.addAction(close)
macOS 似乎以某种方式拦截了名为 "Quit" 或 "Exit" 的菜单项。
您可以愉快地使用 "Close" 或 "Leave" 而无需解决方法。
我一直在使用 PyQt5 开发 GUI,并希望包含一个菜单栏。但是,当我编写此功能的代码时,我的菜单不会出现。弄清楚我对如何在 PyQt5 中实现菜单栏的理解是错误的,我在网上查找了一个预先存在的示例。通过一些调整,我开发了以下测试用例:
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
却找不到了。
在使用 pyuic5 将 .ui 文件转换为可导入的 .py 之前,我还尝试在 QTCreator 中创建菜单栏(以及我的 GUI 布局的其余部分)。我认为这会消除我这边的一些编程错误,但菜单栏仍然不会显示。有什么想法吗?
编辑:
Im 运行使用 Python 3.5 (Anaconda 4.1) 从 Jupyter notebook 4.1 版中编写此代码。我也在使用 Macbook 运行ning os 10.1l、PyQt 5.7 和 Qt 5.7.0 版。
我已经意识到,如果我单击关闭应用程序 window 然后再单击回到 window,菜单栏将变得响应 - 有效地取消焦点和聚焦应用程序。有了这些信息,我意识到我不是第一个注意到这个问题的人(参见 https://github.com/robotology/yarp/issues/457)。不幸的是,我仍然不确定如何解决这个问题。
试试这个:
menubar = QMenuBar()
self.setMenuBar(menubar)
而不是menubar = self.menuBar()
下面这个最简单的例子你试过了吗linktutorialspoint.
这是最简单的例子。
import sys
from PyQt5.QtWidgets import QHBoxLayout, QAction, QApplication, QMainWindow
class menudemo(QMainWindow):
def __init__(self, parent = None):
super(menudemo, self).__init__(parent)
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
save = QAction("Save",self)
save.setShortcut("Ctrl+S")
file.addAction(save)
edit = file.addMenu("Edit")
edit.addAction("copy")
edit.addAction("paste")
quit = QAction("Quit",self)
file.addAction(quit)
file.triggered[QAction].connect(self.processtrigger)
self.setWindowTitle("menu demo")
def processtrigger(self, q):
print(q.text()+" is triggered")
def main():
app = QApplication(sys.argv)
ex = menudemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
这不是 Qt 和 PyQt5 的错误。
我认为你的代码是 zetcode pyqt5 menubar tutorial。我在 Mac OS.
上遇到了完全相同的问题第一个解决方案是一个技巧。使用 ' &Exit'
而不是 '&Exit'
。在'&Exit'
开头插入一个space,像这样:
...
# exitAction = QAction(QIcon('exit.png'), '&Exit', self) # Not shown
exitAction = QAction(QIcon('exit.png'), ' &Exit', self)
...
mac系统范围的菜单栏OS保留了"Exit"
、"Quit"
等关键词,同理"Quit"
on Mac OS. Actually "Quit" has TextHeuristicRole,所以覆盖了"Quit " 应用程序菜单中的行为。当你点击 "Python" 菜单中的 "Quit python" 时,它不会退出,只是打印 "quit triggered".
如果您必须在其他菜单中使用该名称(例如文件、编辑),您需要像上面那样更改操作名称或使用 QAction::setMenuRole(...)
像这样:
...
exitAction = QAction(QIcon('exit.png'), '&Exit', self)
print(exitAction.menuRole()) # It prints "1". QAction::TextHeuristicRole
exitAction.setMenuRole(QAction.NoRole)
...
请阅读以下内容,对您有所帮助。
如果您的程序在 Ubuntu 上 运行,您可能会在屏幕顶部找到菜单栏。
如果要将菜单栏移动到 window 的标题栏,可以在 "System setting / Appearance / Behavior / Show the menus for a window / In the window's title bar" 中切换设置。
菜单栏在 PyQt5 中不可见
bar = self.menuBar()
bar.setNativeMenuBar(False)
file = bar.addMenu("File")
file.addAction("New")
NativeMenuBar 属性 指定菜单栏是否应在支持它的平台上用作本机菜单栏。如果此 属性 为真,则菜单栏将在本机菜单栏中使用,而不是在其父级的 window 中使用,如果为假,则菜单栏将保留在 window 中。
示例程序
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
exitAct = QAction(QIcon('exit.png'), ' &Quit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')
exitAct.triggered.connect(qApp.quit)
self.statusBar()
menubar = self.menuBar()
menubar.setNativeMenuBar(False)
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
bar = self.menuBar()
file = bar.addMenu("Edit")
file.addAction("New")
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Simple menu')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Menu()
sys.exit(app.exec_())
在 MacOS 下,应用程序菜单出现在屏幕顶部。通过在字符串 "Quit" 前加上一个空字符,我设法让“退出”菜单选项出现在上面的示例中,如下所示:
close = QAction("[=10=]Quit",self)
close.setShortcut("Ctrl+Q")
file.addAction(close)
macOS 似乎以某种方式拦截了名为 "Quit" 或 "Exit" 的菜单项。 您可以愉快地使用 "Close" 或 "Leave" 而无需解决方法。