简体   繁体   English

Python在关闭GUI应用程序时意外退出

[英]Python exiting unexpectedly while closing GUI application

I am trying to write a simple menu driven GUI program. 我正在尝试编写一个简单的菜单驱动的GUI程序。

Following are the environment: 以下是环境:

  1. PyQt5 ver.5.10.1 PyQt5版本5.10.1
  2. Python 3.6.4 的Python 3.6.4
  3. Mac OS Sierra Mac OS Sierra

PyQt5 was installed using pip3. 使用pip3安装了PyQt5。

Here is the code I am using: 这是我正在使用的代码:

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_())

On executing the code (using IDLE) the GUI loads and the menu items (Actions) are also working as expected. 在执行代码(使用IDLE)时,GUI加载和菜单项(操作)也按预期工作。

When the menu item "Quit" is called the app closes as well as the python icon (from the system tray). 调用菜单项“退出”时,应用程序以及python图标(从系统托盘中)关闭。 But after about 5-10 seconds I keep getting a message saying "Python quit unexpectedly". 但是大约5到10秒钟后,我不断收到一条消息,说“ Python意外退出”。

I have been trying to figure out the problem using possible solns. 我一直在尝试使用可能的解决方案来解决问题。 by following leads on the net (like sys.exit(), app.quit() ) but every time I face the same result. 通过跟踪网络上的线索(例如sys.exit(),app.quit()),但是每次我遇到相同的结果时。

I have coding experience on ABAP/4 and VB.Net but this is very early stages for me so far as GUI coding on Python/PyQt is concerned. 我在ABAP / 4和VB.Net上有编码经验,但是就我在Python / PyQt上进行GUI编码而言,这对我来说还处于初期。

Will appreciate if a lead is provided so that I may progress in this new endeavor of mine. 如果能提供线索,我将在我的这项新工作中取得进展,将不胜感激。

Thanks 谢谢

The problem is really simple: mnuQuit is a variable not a member of the class so it can not be accessed by other methods of the class, plus I see it unnecessary to use that line of code, just call close() : 这个问题真的很简单: mnuQuit是一个变量,不是类的成员,因此该类的其他方法无法访问它,而且我认为不必使用该行代码,只需调用close()

def triggerAct(self, x):
    if x.text() == "Create New":
        print("Trigger Create New...")
    elif x.text() == " Quit":
        self.close()
        # mnuQuit.triggered.connect(self.close)

It is not appropriate to call exit() within the GUI because the GUI has to free memory and if you call exit() it does not give you the time to do it. 在GUI中调用exit()是不合适的,因为GUI必须释放内存,并且如果您调用exit()它不会给您时间。 with close() the GUI closes correctly. 使用close() ,GUI可以正确关闭。

 def triggerAct(self, x):
    if x.text() == "Create New":
        print("Trigger Create New...")
    elif x.text() == " Quit":
        exit()

I think exit() will work fine in your case. 我认为exit()在您的情况下可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM