【发布时间】:2018-07-22 04:05:13
【问题描述】:
我在 Mac 中使用 PyQt5 创建 Qt 菜单栏时遇到问题。
我遇到的问题是菜单栏会显示,但直到我取消应用程序焦点(通过单击另一个应用程序),然后再次重新调整 Qt 应用程序时才会做出反应。
这是我的环境:
操作系统:Sierra 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 应用程序。
我搜索了很多网站,但没有找到解决方案。最接近的是这个 (https://github.com/robotology/yarp/issues/457),但它似乎没有帮助。
【问题讨论】:
-
更新你的PyQt版本,当前版本是PyQt 5.10,可能在以后的版本中问题已经解决了。 riverbankcomputing.com/software/pyqt/download5
-
不幸的是,conda 只有 PyQt 5.6。由于潜在的破坏风险,我不想进行 pip install (请参阅github.com/ContinuumIO/anaconda-issues/issues/1970)。看来我被困住了。
-
5.3.1版本太老了,所以我认为那个版本没有解决方案,你应该使用5.6或5.9等LTS版本
标签: python-3.x macos pyqt5