PyQt app doesn't exit when i close the window

时光总嘲笑我的痴心妄想 提交于 2019-12-02 10:32:02

问题


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

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

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

回答1:


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

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

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.



来源:https://stackoverflow.com/questions/25412263/pyqt-app-doesnt-exit-when-i-close-the-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!