I\'m doing some very simple PySide (and PyQt) tutorials in IPython. One tutorial just creates a window with some sliders to demonstrate slots and signals.
When I clo
According to https://github.com/spyder-ide/spyder/issues/4639,
First, check if the instance is already present, and either assign that, or make a new one:
if not QApplication.instance():
app = QApplication(sys.argv)
else:
app = QApplication.instance()
(This snippet is taken from the answer by @NotSoBrainy.)
Then, after the show()
method:
QtWidgets.QApplication.setQuitOnLastWindowClosed(True)
app.exec_()
app.quit()
There’s no need for sys.exit()
.
This solved my issue, which was exactly the same.
sys.exit
just raises SystemExit
to terminate the interperter.
ipython catches SysExit
when it executes a script in interactive mode, so this isn't acutally an error but a feature of ipython do avoid the interactive interpreter from being shutdown when a script is executed, as that's not what you usually want in an interactive session.
What you need to do is to cause the QApplication to be deleted later as in:
app = QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
Using this code you can rerun the application as many times as you want in IPython, or anywhere else and every time you close your qt application, the object will be deleted in python.
Check if already an instance of QApplication is present or not as the error occurs when an instance is already running and you are trying to create a new one.
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
This answer is thanks to Matthias BUSSONNIER from the ipython-users mailing list.
When I close the window of the running demo application, I see this error: An exception has occurred, use %tb to see the full traceback. SystemExit: 0
Just don't use sys.exit(0) as you are not exiting python, but still running IPython.
Add it back if you wish to run your app from a (real) command line and have a return status.
If I try to execute my code again, I get this:
RuntimeError: A QApplication instance already exists.
This is a PySide Bug that they "won't fix" as they don't consider it a bug.
See https://github.com/ipython/ipython/issues/1124)
and http://bugs.pyside.org/show_bug.cgi?id=855
QApplication can only have one instance and quitting an app is apparently not considered a reason sufficient enough do delete the object...
You can use this code from above issues :
app=QtGui.QApplication.instance() # checks if QApplication already exists
if not app: # create QApplication if it doesnt exist
app = QtGui.QApplication(sys.argv)
This was a sufficient solution for my present needs.