【发布时间】:2018-06-12 16:08:22
【问题描述】:
我正在尝试编写一个简单的菜单驱动的 GUI 程序。
以下是环境:
- PyQt5 ver.5.10.1
- Python 3.6.4
- Mac OS Sierra
PyQt5 是使用 pip3 安装的。
这是我正在使用的代码:
from PyQt5.QtWidgets import (QMainWindow, QApplication,
QWidget, QPushButton, QAction)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class myApp(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Some App'
self.left = 10
self.top = 10
self.width = 480
self.height = 260
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.statusBar()
pkMenuBar = self.menuBar()
mnuFile = pkMenuBar.addMenu("File")
mnuFile.addAction("Create New")
mnuQuit = QAction(QIcon("ico1.png"), " Quit", self)
mnuQuit.setShortcut("Ctrl+Q")
mnuQuit.setStatusTip("Quit Application")
mnuFile.addAction(mnuQuit)
mnuFile.triggered[QAction].connect(self.triggerAct)
self.show()
def triggerAct(self, x):
if x.text() == "Create New":
print("Trigger Create New...")
elif x.text() == " Quit":
mnuQuit.triggered.connect(self.close)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = myApp()
sys.exit(app.exec_())
在执行代码时(使用 IDLE),GUI 加载和菜单项(操作)也按预期工作。
当菜单项“退出”被调用时,应用程序以及 python 图标(来自系统托盘)关闭。但大约 5-10 秒后,我不断收到一条消息“Python 意外退出”。
我一直在尝试使用可能的解决方案来解决问题。通过跟踪网络上的线索(如 sys.exit()、app.quit() ),但每次我面临相同的结果。
我有 ABAP/4 和 VB.Net 的编码经验,但就 Python/PyQt 上的 GUI 编码而言,这对我来说还处于早期阶段。
如果能提供线索,我将不胜感激,以便我可以在我的新工作中取得进展。
谢谢
【问题讨论】:
-
这可能是因为您指定了退出代码(在
sys.exit()中)。我不知道app.exec_()返回什么,但尝试将其从exit参数中提取出来。