问题描述
我在 Mac 中使用 PyQt5 创建 Qt 菜单栏时遇到问题.
我遇到的问题是菜单栏会出现,但直到我取消对应用程序的关注(通过单击其他应用程序),然后再次重新聚焦 Qt 应用程序才会做出反应.
这是我的环境:
操作系统:Sierra 10.12
Python:来自 conda 的 Python 3.6
PyQt5:conda 默认(v5.3.1)
这是我的代码(主要来自 我搜索了很多网站,都没有找到解决方案.最接近的是这个(https://github.com/robotology/yarp/issues/457) 但它似乎没有帮助.
使用 pythonw 而不是 python3 运行您的代码.例如,如果您的文件名为 test-gui.py,请在命令行终端中输入:
pythonw test-gui.py
这为我修复了错误.它将代码视为可执行文件,因此操作系统不会混淆正在运行的内容.
I have problems in creating the Qt menubar using PyQt5 in Mac.
The problem I have is the menubar would show up, but won't react until I unfocus the app (by clicking the other app), then refocus the Qt app again.
Here's my environment:
OS: Sierra 10.12
Python: Python 3.6 from conda
PyQt5: conda default(v5.3.1)
Here's my code (mostly from 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_())
The picture below shows the menu bar is present, but it just won't respond to my click until I select other app and return to my Qt app again.
I've searched many site and found no solution. The closest one is this (https://github.com/robotology/yarp/issues/457) but it doesn't seem to help.
Run your code using pythonw rather than python3. So for example, if your file is called test-gui.py, in the command line terminal, type in:
pythonw test-gui.py
This fixed the error for me. It treats the code as an executable, so the OS isn't confused as to what is being run.
这篇关于Mac PyQt5 菜单栏在取消聚焦 - 重新聚焦应用程序之前不处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!