You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the script is run as presented, two issues are present:
Issue 1: The qapp.exec_() does not terminate when the window is closed.
This was resolved for me by adding: QtWidgets.QApplication.setQuitOnLastWindowClosed(True)
before qapp.exec_() is called:
if not QtWidgets.QApplication.instance():
qapp = QtWidgets.QApplication(sys.argv)
else:
qapp = QtWidgets.QApplication.instance()
QtWidgets.QApplication.setQuitOnLastWindowClosed(True)
app = ApplicationWindow()
app.show()
qapp.exec_()
Issue 2: When this script is called multiple times in the IPython console, the Kernel dies and must be restarted. Per the comment thread on issue #4349, this is solved by replacing:
qapp = QtWidgets.QApplication(sys.argv)
with
if not QtWidgets.QApplication.instance():
qapp = QtWidgets.QApplication(sys.argv)
else:
qapp = QtWidgets.QApplication.instance()
I've struggled with this alot. I found that the workaround you suggested sometimes worked, sometimes didn't. I would really like something that worked in both Spyder and in a script.
I'm not too sure. I just remember not to ask too much of opening applications from within spyder. I'm mostly expressing my support for applications supporting execcution from within spyder. Though I know it is challenging with so many different versions out there in the wild.
I see, yeah I just tried out @ImportanceOfBeingErnest 's suggestion and using an external console is a good tool to be able to fall back on if needed. Though I like and am used to running everything within Spyder. While I have only been messing around with it recently for the past day or so, and using the most recent versions of spyder and matplotlib, I haven't had any issues with Kernel crashes or qapp.exec_() hanging using the workarounds I suggested. I noticed that there were quite a few forum threads out there that talked about this issue, so that is why I thought it might be nice to add a few extra lines or commented lines that would point people in the right direction. Though as you point out, what the 'right direction is' is complicated given the number of versions people might be using.
I'm going to close this based on @ImportanceOfBeingErnest comment. Our examples are not meant to be run inside spyder or any other IDE, particularly those that spawn an application - if you ned help getting Qt apps running in spyder, that's probably better discussed as a spyder or Qt issue. Thanks!
Agreed with the change, however I think the QtWidgets.QApplication.setQuitOnLastWindowClosed(True) line should only be added when one is creating a new QApp, not when one is already present (on the basis of "if you're getting the QApp from someone else, assume that they have configured it the way they want").
@anntzer It only works with "Execute in an external console".
"Execute in current console" still freezes at the second attempt. Is this expected/accepted?
Run code, with "Execute in current console". -> ok
(window appears, after closing the window, the console gets back to the prompt.
Run code, with "Execute in current console" again.
Observation: As opposed to the previeous run, the plot window now appears behind spyder.
After closing the window, the console is still blocked.
I see the second plot appearing behind spyder, but not the blockage at the end that you report.
Looks like we can ensure the window to be raised to front even the second time by adding
Activity
ImportanceOfBeingErnest commentedon Feb 10, 2019
All examples are meant to be run as script.
In Spyder you can check
to not have your GUI interfere with the build-in IPython console.
hmaarrfk commentedon Feb 10, 2019
I've struggled with this alot. I found that the workaround you suggested sometimes worked, sometimes didn't. I would really like something that worked in both Spyder and in a script.
gigo318 commentedon Feb 10, 2019
Which part of the workaround, Issue 2?
hmaarrfk commentedon Feb 10, 2019
I'm not too sure. I just remember not to ask too much of opening applications from within spyder. I'm mostly expressing my support for applications supporting execcution from within spyder. Though I know it is challenging with so many different versions out there in the wild.
gigo318 commentedon Feb 10, 2019
I see, yeah I just tried out @ImportanceOfBeingErnest 's suggestion and using an external console is a good tool to be able to fall back on if needed. Though I like and am used to running everything within Spyder. While I have only been messing around with it recently for the past day or so, and using the most recent versions of spyder and matplotlib, I haven't had any issues with Kernel crashes or qapp.exec_() hanging using the workarounds I suggested. I noticed that there were quite a few forum threads out there that talked about this issue, so that is why I thought it might be nice to add a few extra lines or commented lines that would point people in the right direction. Though as you point out, what the 'right direction is' is complicated given the number of versions people might be using.
jklymak commentedon Feb 10, 2019
I'm going to close this based on @ImportanceOfBeingErnest comment. Our examples are not meant to be run inside spyder or any other IDE, particularly those that spawn an application - if you ned help getting Qt apps running in spyder, that's probably better discussed as a spyder or Qt issue. Thanks!
tacaswell commentedon Feb 11, 2019
@gigo318 Could you open a PR with those changes?
I think it is good practice to always do the "is there a running Qt app?" dance before creating a QApplication so we should demo that.
anntzer commentedon Feb 11, 2019
Agreed with the change, however I think the
QtWidgets.QApplication.setQuitOnLastWindowClosed(True)line should only be added when one is creating a new QApp, not when one is already present (on the basis of "if you're getting the QApp from someone else, assume that they have configured it the way they want").anntzer commentedon Jul 3, 2019
Right now closing the window (in spyder) seems to return to the ipython console just fine, so I think this can be closed?
timhoffm commentedon Oct 17, 2019
@anntzer It only works with "Execute in an external console".
"Execute in current console" still freezes at the second attempt. Is this expected/accepted?
Steps to reproduce:
(window appears, after closing the window, the console gets back to the prompt.
Observation: As opposed to the previeous run, the plot window now appears behind spyder.
After closing the window, the console is still blocked.
anntzer commentedon Oct 17, 2019
I see the second plot appearing behind spyder, but not the blockage at the end that you report.
Looks like we can ensure the window to be raised to front even the second time by adding
before the call to
qapp.exec_()-- see also https://github.com/matplotlib/matplotlib/pull/6384/files; this at least works for me locally.timhoffm commentedon Oct 17, 2019
After the second closing the commandline does not come back for me:
matplotlib 3.1.1
spyder 3.3.6
ipython 7.8.0
Qt 5.9.6
PyQt5 5.9.2
anntzer commentedon Oct 17, 2019
what about matplotlib master? what desktop environment?
timhoffm commentedon Oct 17, 2019
Indeed, works on master 😄.
So after fixing the raise, this can be closed.
timhoffm commentedon Oct 22, 2019
@anntzer Sorry, didn't see the label.