Skip to content

Qt Embedding w/ Spyder #13400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gigo318 opened this issue Feb 10, 2019 · 15 comments · Fixed by #15487
Closed

Qt Embedding w/ Spyder #13400

gigo318 opened this issue Feb 10, 2019 · 15 comments · Fixed by #15487
Labels
Documentation Good first issue Open a pull request against these issues if there are no active ones! GUI: Qt
Milestone

Comments

@gigo318
Copy link

gigo318 commented Feb 10, 2019

I have found a solution to a bug when using the script on:
https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html
with the Spyder IDE (v3.3.2)

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()

Can this be added to the example script?

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.0.2
  • Python version: 3.7
@ImportanceOfBeingErnest
Copy link
Member

All examples are meant to be run as script.
In Spyder you can check

image

to not have your GUI interfere with the build-in IPython console.

@hmaarrfk
Copy link
Contributor

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
Copy link
Author

gigo318 commented Feb 10, 2019

Which part of the workaround, Issue 2?

@hmaarrfk
Copy link
Contributor

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
Copy link
Author

gigo318 commented 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
Copy link
Member

jklymak commented 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!

@jklymak jklymak closed this as completed Feb 10, 2019
@tacaswell
Copy link
Member

@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
Copy link
Contributor

anntzer commented 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
Copy link
Contributor

anntzer commented 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
Copy link
Member

@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:

  • Copy code from https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html in a file in Spyder.
  • 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.

@anntzer
Copy link
Contributor

anntzer commented 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

app.activateWindow()
app.raise_()

before the call to qapp.exec_() -- see also https://github.com/matplotlib/matplotlib/pull/6384/files; this at least works for me locally.

@timhoffm
Copy link
Member

timhoffm commented Oct 17, 2019

After the second closing the commandline does not come back for me:


image

matplotlib 3.1.1
spyder 3.3.6
ipython 7.8.0
Qt 5.9.6
PyQt5 5.9.2

@anntzer
Copy link
Contributor

anntzer commented Oct 17, 2019

what about matplotlib master? what desktop environment?

@timhoffm
Copy link
Member

Indeed, works on master 😄.

So after fixing the raise, this can be closed.

@anntzer anntzer added the Good first issue Open a pull request against these issues if there are no active ones! label Oct 22, 2019
@timhoffm
Copy link
Member

@anntzer Sorry, didn't see the label.

@QuLogic QuLogic modified the milestones: v3.1-doc, v3.2.0 Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Good first issue Open a pull request against these issues if there are no active ones! GUI: Qt
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants