PySide Qt 脚本不能从 Spyder 启动,但可以从 shell 运行 [英] PySide Qt script doesn't launch from Spyder but works from shell

查看:57
本文介绍了PySide Qt 脚本不能从 Spyder 启动,但可以从 shell 运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个奇怪的错误,它使用 PySide 作为其 Qt GUI,作为回应,我尝试使用设置环境的更简单的代码进行测试.

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.

这是我正在测试的代码:https://stackoverflow.com/a/6906552/130164

Here is the code I am testing with: https://stackoverflow.com/a/6906552/130164

当我从我的 shell (python test.py) 启动它时,它工作得很好.但是,当我在 Spyder 中运行该脚本时,出现以下错误:

When I launch that from my shell (python test.py), it works perfectly. However, when I run that script in Spyder, I get the following error:

Traceback (most recent call last):
  File "/home/test/Desktop/test/test.py", line 31, in <module>
    app = QtGui.QApplication(sys.argv)
RuntimeError: A QApplication instance already exists.

如果有帮助,我还会收到以下警告:

If it helps, I also get the following warning:

/usr/lib/pymodules/python2.6/matplotlib/__init__.py:835: UserWarning:  This call to matplotlib.use() has no effect
because the the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

为什么该代码在从我的 shell 启动而不是从 Spyder 启动时有效?

更新: Mata 回答说问题发生是因为 Spyder 使用 Qt,这是有道理的.目前,我已使用在外部系统终端中执行"选项在 Spyder 中设置执行,该选项不会导致错误但也不允许调试.Spyder 是否有任何内置的解决方法?

Update: Mata answered that the problem happens because Spyder uses Qt, which makes sense. For now, I've set up execution in Spyder using the "Execute in an external system terminal" option, which doesn't cause errors but doesn't allow debugging, either. Does Spyder have any built-in workarounds to this?

推荐答案

我也有同样的问题,stackoverflow 上有一个解决方案.

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

代替

qApp = QtGui.QApplication(sys.argv)

使用

qApp = QtGui.QApplication.instance()
if qApp is None:
        qApp = QtGui.QApplication(sys.argv)

这篇关于PySide Qt 脚本不能从 Spyder 启动,但可以从 shell 运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆