简体   繁体   中英

Unable to see app windows created by PySide2 in Spyder

I'm trying to run an app with PySide2 from Spyder 3.2.8 and Python 3.6.4 in Anaconda in a macOS 10.13.4 .

attempt N°1

After having seen this stackoveflow page and this github page I changed my graphic backend from Inline to Automatic in Python > Preferences > IPython Console > Graphics and I tried to run the following script ( script N°1 ):

script N°1

import sys
from PySide2.QtWidgets import *

# Create a Qt application
app = QApplication.instance()
if app is None: 
    print("print something")
    app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()

but got the following error message after running it:

Importing PySide2 disabled by IPython, which has
already imported an Incompatible QT Binding: pyqt5

There are similar reported issues here with matplotlib and here with ipython but it didn't help me (or I couldn't implement it properly). Then I tried to implement the content of this page about qtpy by changing the script N°1 in the following way:

script N°2

import os
os.environ['QT_API'] = 'pyside2'
from qtpy.QtWidgets import *
import sys

# Create a Qt application
app = QApplication.instance()
if app is None: 
    print("print something")
    app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()

attempt N°2

With Inline selected in Python > Preferences > IPython Console > Graphics . When I ran the script N°2 , the app launches and I got print something printed in to the console. When closing the app, I got Out[1]: 0 in the console. However when I run the script again, no error message appears in the console but the window of the app doesn't show-up

attempt N°3

This time with Automatic selected in Python > Preferences > IPython Console > Graphics . When I ran the script N°2 the first time, the app didn't launch and I got the following error message

/anaconda3/lib/python3.6/site-packages/qtpy/__init__.py:178: RuntimeWarning: Selected binding "pyside2" could not be found, using "pyqt5"
'using "{}"'.format(initial_api, API), RuntimeWarning)
Out[2]: -1

attempt N°4

With Automatic selected in Python > Preferences > IPython Console > Graphics . When I ran the script N°1 after having changed the line from PySide2.QtWidgets import * to from PyQt5.QtWidgets import * : The app didn't launch and I got the following error message

Out[1]: -1

attempt N°5

With Inline selected in Python > Preferences > IPython Console > Graphics . When I ran the script N°1 after having changed the line from PySide2.QtWidgets import * to from PyQt5.QtWidgets import * : The app launches and I got print something printed in to the console. I closed the app and got Out[1]: 0 in the console. However when I run the script again, no error message appears in the console but the window of the app doesn't show-up

NB this question is the continuation of that question

( Spyder maintainer here ) Since the ipykernel package (which is used by Spyder to run code in its consoles) doesn't have event loop support for PySide2 as of May/2018 (as can be seen here ), you won't be able to run PySide2 code inside Spyder, no matter what you try .

Notes :

  1. The Automatic backend tries to select a suitable event loop for you, in this order: Qt5, Qt4, Tk and Inline. That's why it doesn't work in your case.
  2. Every time you change a Graphics backend in Spyder, you need to restart the kernel of the console you want to run your code in. That's because you can only use one backend per console session (this is a limitation imposed by ipykernel , not by us). It's clear from your question that you're not doing that.
  3. We're aware we fail to inform users when a kernel restart is necessary. We'll try to address that in our next major version (Spyder 4), to be released in 2019.
  4. If you already know about qtpy , please use it to develop your apps instead of using PySide2 directly. That way you could work with PyQt5 for development in Spyder, but PySide2 for deployment, since qtpy takes care of working seamlessly with whatever binding is available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM