python-3.x - Mac PyQt5 菜单栏在取消聚焦 - 重新聚焦应用程序之前不处于事件状态

标签 python-3.x macos pyqt5

我在 Mac 中使用 PyQt5 创建 Qt 菜单栏时遇到问题。

我的问题是菜单栏会出现,但在我取消关注应用程序(通过单击其他应用程序),然后再次重新聚焦 Qt 应用程序之前,不会使用react。

这是我的环境:

操作系统:塞拉利昂 10.12

Python:来自 conda 的 Python 3.6

PyQt5: conda 默认(v5.3.1)

这是我的代码(主要来自 http://zetcode.com/gui/pyqt5/menustoolbars/ ):

import sys

from PyQt5.QtWidgets import QMainWindow, QAction, QDesktopWidget, QApplication, qApp, QMenuBar

class Example(QMainWindow):

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

    def initUI(self):

        self.resize(800, 600)
        self.center()
        self.setWindowTitle('Menubar')

        exitAction = QAction(' &Exit', self)
        exitAction.setShortcut('Ctrl-Q')
        exitAction.setToolTip('Exit application')
        exitAction.triggered.connect(qApp.quit)

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

        self.show()

    def center(self):

        center_point = QDesktopWidget().availableGeometry().center()
        frame = self.frameGeometry()
        frame.moveCenter(center_point)
        self.move(frame.topLeft())


if __name__ == '__main__':

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

下图显示菜单栏存在,但它不会响应我的点击,直到我选择其他应用程序并再次返回到我的 Qt 应用程序。

enter image description here
我搜索了很多网站,都没有找到解决方案。最接近的是这个( https://github.com/robotology/yarp/issues/457 ),但它似乎没有帮助。

最佳答案

使用 运行您的代码pythonw 而不是 python3 .例如,如果您的文件名为 test-gui.py,请在命令行终端中输入:
pythonw test-gui.py
这为我修复了错误。它将代码视为可执行文件,因此操作系统不会混淆正在运行的内容。

关于python-3.x - Mac PyQt5 菜单栏在取消聚焦 - 重新聚焦应用程序之前不处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48738805/

相关文章:

objective-c - 如何从 Objective-C 中的其他类访问 IBOutlets?

macos - 在MacOSX/PPC上,有关如何在0x0000000000000000处捕获KERN_PROTECTION_FAILURE的建议

python - 集成 Qt Designer 和 PyCharm

python - MATLAB 生成的 Python 包与 Ubuntu 上的 PyQt5 冲突——可能是库问题

python - 如何解释python3的帮助输出?

python-3.x - 函数参数错误 - 缺少 1 个必需的位置参数

python - 无法运行播放通过 cx_Freeze 编译的 OGG 文件的 Pygame 脚本

python - 如何将列表中的行插入到QSqlTableModel中?

macos - 在 Mac OS X 应用程序中调整屏幕亮度

python - PyQt5:派生 QWidget 类中带有元类的 Mixin 的问题