简体   繁体   English

无法在Spyder中看到由PySide2创建的应用程序窗口

[英]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 . 我正在尝试使用Spyder 3.2.8 PySide2macOS 10.13.4中的Anaconda中的Python 3.6.4运行应用程序。

attempt N°1 尝试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 ): 在看过这个stackoveflow页面和这个github页面后,我在Python > Preferences > IPython Console > Graphics中将我的图形后端从Inline更改为Automatic ,我尝试运行以下脚本( 脚本N°1 ):

script N°1 脚本编号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). 这里有类似的matplotlib报告问题, 这里有ipython,但它没有帮助我(或者我无法正确实现)。 Then I tried to implement the content of this page about qtpy by changing the script N°1 in the following way: 然后我尝试通过以下方式更改脚本N°1来实现此页面关于qtpy的内容:

script N°2 脚本编号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 尝试N°2

With Inline selected in Python > Preferences > IPython Console > Graphics . Python > Preferences > IPython Console > Graphics选择Inline When I ran the script N°2 , the app launches and I got print something printed in to the console. 当我运行N°2脚本时 ,应用程序启动,我将print something打印到控制台。 When closing the app, I got Out[1]: 0 in the console. 关闭应用程序时,我在控制台中Out[1]: 0 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 尝试N°3

This time with Automatic selected in Python > Preferences > IPython Console > Graphics . 这次在Python > Preferences > IPython Console > Graphics选择了Automatic When I ran the script N°2 the first time, the app didn't launch and I got the following error message 当我第一次运行N°2脚本时,应用程序没有启动,我收到以下错误消息

/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 尝试N°4

With Automatic selected in Python > Preferences > IPython Console > Graphics . Python > Preferences > IPython Console > Graphics选择Automatic 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 当我从PySide2.QtWidgets import *from PyQt5.QtWidgets import *之后我运行脚本N°1 from PyQt5.QtWidgets import * :应用程序没有启动,我收到以下错误消息

Out[1]: -1

attempt N°5 尝试N°5

With Inline selected in Python > Preferences > IPython Console > Graphics . Python > Preferences > IPython Console > Graphics选择Inline 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. 当我从PySide2.QtWidgets import *from PyQt5.QtWidgets import *之后我运行了脚本N°1 from PyQt5.QtWidgets import * :应用程序启动并且我print something打印到控制台的内容。 I closed the app and got Out[1]: 0 in the console. 我关闭了应用程序并在控制台中Out[1]: 0 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 NB这个问题的延续了这个问题

( 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 . 这里Spyder的维护者 )由于ipykernel包(用于通过Spyder的运行在控制台代码)不具有PySide2事件循环支持,截至5月/ 2018(可以看出的在这里 ),你将无法无论你尝试什么 ,在Spyder中运行PySide2代码。

Notes : 备注

  1. The Automatic backend tries to select a suitable event loop for you, in this order: Qt5, Qt4, Tk and Inline. Automatic后端尝试按此顺序为您选择合适的事件循环:Qt5,Qt4,Tk和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). 每次在Spyder中更改Graphics后端时,都需要重新启动要运行代码的控制台内核。这是因为每个控制台会话只能使用一个后端(这是ipykernel强加的限制,而不是我们)。 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. 我们将尝试在我们的下一个主要版本(Spyder 4)中解决这个问题,将于2019年发布。
  4. If you already know about qtpy , please use it to develop your apps instead of using PySide2 directly. 如果您已经了解qtpy ,请使用它来开发您的应用,而不是直接使用PySide2。 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. 这样你可以使用PyQt5在Spyder中进行开发,但是可以使用PySide2进行部署,因为qtpy负责与任何可用的绑定无缝协作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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