为什么 PyQt 菜单在 macOS 上冻结? [英] Why does PyQt menu get frozen on macOS?

查看:106
本文介绍了为什么 PyQt 菜单在 macOS 上冻结?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 macOS Catalina 上编写了一个最小的 PyQt 5.15 应用程序.它只有一个菜单和一个项目.虽然我可以看到菜单,但单击它不会打开它.它完全没有反应.事实上,当我的应用程序处于焦点时,甚至 macOS(苹果图标)都不会打开.如果我不聚焦然后重新聚焦在应用程序上,菜单就会起作用.我在 Windows 10 上运行了相同的程序,没有出现这样的问题.

I wrote a minimal PyQt 5.15 app on macOS Catalina. It just has one menu with one item. While I can see the menu, clicking on it does not open it. It's completely unresponsive. In fact, not even the macOS (apple icon) opens when my app is in focus. If I unfocus and then re-focus on the app, the menu works. I ran the same program on Windows 10 and there was no such problem.

是什么导致菜单栏在 macOS 中无法正常工作?

What is causing the menu bar not to work properly in macOS?

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QAction


class MainWindow(QMainWindow):

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

        insert_action = QAction("&Open", self)
        insert_action.triggered.connect(self.open_file)

        menu = self.menuBar()

        file_menu = menu.addMenu("&File")
        file_menu.addAction(insert_action)

    def open_file(self):
        pass


app = QApplication(sys.argv)
w = MainWindow()
w.show()

app.exec_()

推荐答案

这不是一个完整的答案,而是一个解决方法:

This is not a complete answer, but a workaround:

menu.setNativeMenuBar(False)

使用窗口上的非原生 macOS 菜单,与 Windows 和 Linux 上的菜单类似,并且有效.

uses an on-window, non-native macOS menu, similar to those on Windows and Linux, which works.

还有在线讨论提到将应用打包解决问题,但我还没有尝试过.

There are also online discussions mentioning that making the app a bundle solves the issue, but I have not tried that.

这篇关于为什么 PyQt 菜单在 macOS 上冻结?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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