繁体   English   中英

PyQt5 menuBar出了点问题

[英]Something wrong with PyQt5 menuBar

当我使用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

Qt网站说以下

注意:不要调用QMainWindow :: menuBar()创建共享菜单栏,因为该菜单栏将以QMainWindow作为其父菜单栏。 该菜单栏仅针对父QMainWindow显示。

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

见参考文献的形式在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM