Skip to content

Qt Embedding w/ Spyder #13400

Closed
Closed

Description

@gigo318

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

Activity

  1. ImportanceOfBeingErnest commented on Feb 10, 2019

    @ImportanceOfBeingErnest
    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.

  2. hmaarrfk commented on Feb 10, 2019

    @hmaarrfk
    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.

  3. gigo318 commented on Feb 10, 2019

    @gigo318
    Author

    Which part of the workaround, Issue 2?

  4. hmaarrfk commented on Feb 10, 2019

    @hmaarrfk
    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.

  5. gigo318 commented on Feb 10, 2019

    @gigo318
    Author

    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.

  6. jklymak commented on Feb 10, 2019

    @jklymak
    Member

    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!

  7. tacaswell commented on Feb 11, 2019

    @tacaswell
    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.

  8. anntzer commented on Feb 11, 2019

    @anntzer
    Contributor

    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").

  9. added this to the v3.0-doc milestone on Feb 11, 2019
  10. modified the milestones: v3.0-doc, v3.1-doc on Jul 3, 2019
  11. anntzer commented on Jul 3, 2019

    @anntzer
    Contributor

    Right now closing the window (in spyder) seems to return to the ipython console just fine, so I think this can be closed?

  12. timhoffm commented on Oct 17, 2019

    @timhoffm
    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.
  13. anntzer commented on Oct 17, 2019

    @anntzer
    Contributor

    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.

  14. timhoffm commented on Oct 17, 2019

    @timhoffm
    Member

    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

  15. anntzer commented on Oct 17, 2019

    @anntzer
    Contributor

    what about matplotlib master? what desktop environment?

  16. timhoffm commented on Oct 17, 2019

    @timhoffm
    Member

    Indeed, works on master 😄.

    So after fixing the raise, this can be closed.

  17. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Oct 22, 2019
  18. timhoffm commented on Oct 22, 2019

    @timhoffm
    Member

    @anntzer Sorry, didn't see the label.

  19. modified the milestones: v3.1-doc, v3.2.0 on Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions