PySide Qt script doesn't launch from Spyder but works from shell

前端 未结 4 1227
旧巷少年郎
旧巷少年郎 2020-12-20 07:26

I have a weird bug in my project that uses PySide for its Qt GUI, and in response I\'m trying to test with simpler code that sets up the environment.

Here is the cod

相关标签:
4条回答
  • 2020-12-20 07:30

    The official Spyder wiki has a page on the subject: https://github.com/spyder-ide/spyder/wiki/How-to-run-PyQt-applications-within-Spyder. The gist of it is:

    Important Note: Before running a PyQt application in Spyder, you need to change your Graphics backend to Automatic. You can do that by going to

    Tools > Preferences > IPython Console > Graphics
    

    After that, please restart your console kernels or Spyder itself for this change to take effect.

    There's an explantion at the end:

    The most common problem when running a PyQt application multiple times inside Spyder is that a QApplication instance remains in the namespace of the IPython console kernel after the first run. In other words, when you try to re-run your application, you already have a QApplication instance initialized.

    Trying to remove that instance will probably cause your program to get stuck in a blocking while-loop, as suggested here, and using sys.exit() doesn't help since it's the same as trying to exit Python (and hence the IPython console).

    A suggested solution is doing something like what @mata suggsets.

    0 讨论(0)
  • 2020-12-20 07:34

    I have the same problem, and somewhere on stackoverflow was a solution.

    Instead of

    qApp = QtGui.QApplication(sys.argv)
    

    Use

    qApp = QtGui.QApplication.instance()
    if qApp is None:
            qApp = QtGui.QApplication(sys.argv)
    
    0 讨论(0)
  • 2020-12-20 07:35

    It won't work in Spyder if you try to launch the application into an interactive console because that console is specially configured to import several scientific libraries, automatically show() matplotlib figures, and a few other details. Type scientific at the Spyder console prompt for more details. The result is effectively that a Qt application event loop is already running there.

    To get your application to run inside of Spyder:

    1. Make sure Spyder is configured to open external consoles with PySide and not PyQt. This can be set from Tools>Preferences>Console>External modules>Qt-Python bindings library selection.
    2. With your script active in the editor, hit F6 to open the Run Settings dialog. Select the "Execute in a new dedicated Python interpreter" radio button instead of executing in the current interactive interpreter. Click OK. Now run the script by hitting F5. Debug the script by hitting Ctrl+F5.
    0 讨论(0)
  • 2020-12-20 07:42

    As Spyder also is a Qt application, it starts it's own QApplication. In the same process only one QApplication can exist, that's why you get the first error.

    Sypder also uses matplotlib, and probably, therfore it already will have imported some of the mentioned modules, so that's why you get the second error.

    So when usin it like that, you can't create your own QApplication or call matplotlib.use(). Or maybe it will work if you wrap these calls in try/except.

    0 讨论(0)
提交回复
热议问题