Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Mac PyQt5菜单栏在取消焦点重新聚焦应用程序之前不处于活动状态_Python 3.x_Macos_Pyqt5 - Fatal编程技术网

Python 3.x Mac PyQt5菜单栏在取消焦点重新聚焦应用程序之前不处于活动状态

Python 3.x Mac PyQt5菜单栏在取消焦点重新聚焦应用程序之前不处于活动状态,python-3.x,macos,pyqt5,Python 3.x,Macos,Pyqt5,我在Mac中使用PyQt5创建Qt菜单栏时遇到问题 我遇到的问题是菜单栏会出现,但在我解除应用程序焦点(通过单击另一个应用程序),然后重新聚焦Qt应用程序之前不会做出反应。 这是我的环境: OS:Sierra 10.12 Python:来自conda的Python 3.6 PyQt5:conda默认值(v5.3.1) 以下是我的代码(主要来自): 下图显示菜单栏存在,但在我选择其他应用程序并再次返回我的Qt应用程序之前,它不会响应我的单击 我搜索了很多网站,没有找到解决方案。最接近的是this

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

我遇到的问题是菜单栏会出现,但在我解除应用程序焦点(通过单击另一个应用程序),然后重新聚焦Qt应用程序之前不会做出反应。

这是我的环境:

OS:Sierra 10.12

Python:来自conda的Python 3.6

PyQt5:conda默认值(v5.3.1)

以下是我的代码(主要来自):

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


我搜索了很多网站,没有找到解决方案。最接近的是this(),但它似乎没有帮助。

使用pythonw而不是python3运行代码。例如,如果您的文件名为test-gui.py,请在命令行终端中键入:

pythonw测试gui.py


这为我修正了错误。它将代码视为可执行文件,因此操作系统不会对正在运行的内容感到困惑。

更新您的PyQt版本,当前版本为PyQt 5.10,可能在更高版本中该问题已得到解决。不幸的是,conda只有PyQt 5.6。我不想做pip安装,因为可能会有损坏东西的风险(请参阅)。似乎我被卡住了。版本5.3.1非常旧,所以我认为对于该版本没有解决方案,您应该使用LTS版本,如版本5.6或5.9
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_())