【问题标题】:Something wrong with PyQt5 menuBarPyQt5 menuBar 有问题
【发布时间】:2018-08-16 08:30:45
【问题描述】:

我在使用 PyQt5 构建带有菜单栏的 GUI 窗口时出现了问题。

这是我的代码:

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

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        bar = self.menuBar()              

        example1 = QAction('Exit', self)        
        example1.setShortcut('Ctrl+E')
        example1.triggered.connect(self.close)

        example2 = QAction('xit', self)        
        example2.setShortcut('Ctrl+A')
        example2.triggered.connect(self.close)

        example3 = QAction('Quit', self)        
        example3.setShortcut('Ctrl+Q')
        example3.triggered.connect(self.close)

        fileMenu = bar.addMenu('File')
        fileMenu.addAction('NNN')
        fileMenu.addAction(example1)
        fileMenu.addAction(example2)
        fileMenu.addAction(example3)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Menu Example')    
        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

当我运行它时,菜单栏如下所示:

如图所示,“退出”和“退出”消失了,但快捷方式有效。

我的环境:Python 3.6.5,PyQt 5.11.1,MAC_OS 10.13.5

【问题讨论】:

  • 它显示在 Windows 上,但我不了解 Mac
  • 在 Ubuntu 16.04、Python 3.5.2 和 PyQt 5.10.0 中运行良好
  • 在 Ubuntu 18.04 中也可以使用 Python 3.6.5 和 PyQt 5.10.1。
  • 基于 cmets,这似乎是 Mac 特有的问题。您可能需要准备好在他们的问题跟踪器中提交错误报告。
  • 在 Ubuntu 16.04 上很好:framapic.org/kbUxJUA6bo1m/iOlbFwAp99hf.png

标签: python macos pyqt pyqt5 menubar


【解决方案1】:

qt 网站是这样说的

注意:不要调用 QMainWindow::menuBar() 来创建共享菜单 栏,因为该菜单栏将 QMainWindow 作为其父级。 该菜单栏只会显示给父 QMainWindow。

尝试将bar = self.menuBar()改为bar = QtGui.MenuBar()

见参考表格here

【讨论】:

  • 感谢您的回答,但在我的问题中只有一个窗口,而不是共享菜单栏。也许就像@code_dredd 说的那样,这是一个特定于 MAC 的问题。
猜你喜欢
  • 2019-08-02
  • 2017-11-30
  • 1970-01-01
  • 2011-12-19
  • 2019-04-09
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多