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
- Fixed the "Kernel died" issue Thanks to @eendebakpt and @TheHugeManatee at https://github.com/spyder-ide/spyder/issues/2970 — committed to tentekal/backgroundSubtraction by tentekal 9 years ago
based on @eendebakpt 's code, the following workaround works for me on windows/anaconda 4.4.0/python 3.6:
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):
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 timeapp = QtWidgets.QApplication([])is called:appobject that is losing its name, so it must be destroyed because:appnameBut 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):
Manually deleting the app object works for me (Windows / Python 3.6.7 / Qt5):
The following code works for me. Note the check on QApplication already running and not executing the exit statement at the end.