To run the PyQt application again in Spyder, the running application must be deleted/destroyed but we can't use sys.exit() because it will try to close Python. One solution that works for me (Python 3.4.1, Spyder 2.3.5.2, PyQt 4.10.4) is to use QtCore.QCoreApplication.instance().quit()
and deleteLater
as shown in this example:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
"""PyQt app that closes successfully in Spyder.
"""
def __init__(self):
super().__init__()
self.setGeometry(200, 100, 400, 300)
self.button()
def button(self):
btn = QtGui.QPushButton('Quit', self)
btn.setGeometry(150, 125, 100, 50)
btn.clicked.connect(self.quitApp)
def quitApp(self):
QtCore.QCoreApplication.instance().quit()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
win = Window()
win.show()
app.exec_()