spyder: PyQt code cannot run twice

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    sys.exit(app.exec_())  

i can not run the code twice it says kernel stopped.but in other ide’s it runs smoothly again and again in web there are other ppl mention about this please fix this bug
http://stackoverflow.com/questions/19459299/running-a-pyqt-application-twice-from-one-prompt-in-spyder
http://stackoverflow.com/questions/33527108/pyqt-examples-not-running-second-time

this bug is general it covers all spyder versions i think ,i tried a few but in all , kernel crashes

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 27 (19 by maintainers)

Commits related to this issue

Most upvoted comments

based on @eendebakpt 's code, the following workaround works for me on windows/anaconda 4.4.0/python 3.6:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *


if __name__ == '__main__':
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    else:
        print('QApplication instance already exists: %s' % str(app))

    window = QtWidgets.QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    app.exec_()

can’t say anything about other platforms currently

The workarounds mentioned here did not work for me, but this seems to work for me (Windows, Anaconda, Python 3.7.2, IPython 7.2.0):

app = QtWidgets.QApplication(sys.argv)
app.lastWindowClosed.connect(app.quit)
widget = QtWidgets.QDialog()
widget.show()
app.exec_()

I get rid of it by running the program with hot key F10 (Run→Profile).

Could you try https://github.com/spyder-ide/spyder-kernels/pull/189 to see if this fixes it?

My guess is the following: when QtWidgets.QApplication([]) is called, a new python wrapper is created, but it wraps a singleton. So when the second time app = QtWidgets.QApplication([]) is called:

  • python has an old app object that is losing its name, so it must be destroyed because:
  • A new python object is taking the app name

But the new python object is really a wrapper around the same C++ object. Therefore the second wrapper ends up empty.

The workaround of @jcnossen works also on (Debian Linux / Python 3.6.8 / Qt5.9.6 / IPython 7.5.0 / JupyterLab 0.35.5):

if 'app' in locals():
    del app

app = QtGui.QApplication([])

# CODE

if __name__ == '__main__':
    QtGui.QApplication.instance().exec_()

Manually deleting the app object works for me (Windows / Python 3.6.7 / Qt5):

app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QDialog()
widget.show()
app.exec_()
del app

The following code works for me. Note the check on QApplication already running and not executing the exit statement at the end.

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

if __name__ == '__main__':
    app = QtGui.QApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)
    else:
        print('QApplication instance already exists: %s' % str(app))

    window = QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    app.exec_()