当我关闭窗口时,PyQt 应用程序不会退出

问题描述:

每当我执行代码并关闭窗口时,它就会关闭,但是 IDE 中的 python 控制台不会返回退出代码,当我尝试再次运行它时,我会收到一个警告对话框,内容类似于

Whenever i execute the code and close the window, it closes,but the python console in the IDE doesn't return the exit code,when i try to run it again i get a warning dialog that says something like

No python console is selected to run main.py

所以我必须关闭 IDE python 控制台,并打开一个新的,然后在新的 python 控制台中运行程序

So i have to close the IDE python console, and open a new one, then run the program in the new python console

我在 Windows 上使用 spyder IDE 64 位

I'm using spyder IDE 64 bits on windows

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = QMainWindow()       
    win.show()
    sys.exit(app.exec_())

如果在运行中的 (i)python 控制台中执行代码,则不需要以通常的方式启动 qapplication,两行

If you execute the code in a running (i)python console, you do not need to start a qapplication in the usual way, the two lines

win = QMainWindow()       
win.show()

足以让你跑步.这是因为控制台已经为您准备了一个(线程化的)qapplication.

will be enough to get you running. This is because the console already has a (threaded) qapplication prepared for you.

当没有控制台具有焦点时可能会导致错误消息(即,您使用的控制台可能因为 sys.exit() 而退出,或者您单击离开等).您只需在 (i)python 控制台中单击以使其被选中",然后运行按钮应该会再次起作用.

The error message can be caused when no console has focus (i.e., perhaps the one you were using quit because of sys.exit(), or you clicked away etc). You need to simply click in an (i)python console to get it to be 'selected', and then the run button should work again.