Even when doing trial and error with a GUI program such as PyQt or PyQtGraph I often use jupyter etc. because it is convenient, There is one of the most annoying points.
When I run a PyQt GUI program several times,
** The kernel will definitely die. ** **
I finally found a workaround.
This → sys.exit (app.exec_ ())
I thought it was because sys.exit ended strangely, but it was innocent. The presence or absence of sys.exit () at exit was irrelevant (at least in jupyter). (Spyder is unconfirmed)
app = 0 # Add this line app = QtGui.QApplication(sys.argv)
With this alone, even if you run the same code with jupyter many times, The annoying kernel restart is gone! Yay!
reference https://www.reddit.com/r/learnpython/comments/45h05k/solved_kernel_crashing_when_closing_gui_spyder/
2016/09/01 postscript
I could do it with mojaie's method in the comment. No matter how you think about it, this method is right.
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(())
Also, while PyQt4 runs silently and the kernel dies, I was shocked that PySide prevented the creation of multiple instances of QApplication in advance and returned a runtime error. It's the difference between PyQt4 and PySide. .. ..
Also, PySide causes a runtime error, so app = 0 cannot be used. app = 0 is a magic for PyQt4 ...
Recommended Posts