PySide2 没有使用基本示例正确关闭 [英] PySide2 not closing correctly with basic example

查看:55
本文介绍了PySide2 没有使用基本示例正确关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行基本脚本时:

import sys
from PySide2.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)
label = QLabel("Hello World")
label.show()
app.exec_()

第一次一切正常.但是,如果我第二次运行它,我会得到:

forthe first time it all works fine. However, if I run it a second time I get:

File "../script.py", line 17, in <module>
app = QApplication(sys.argv)

RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.

我在 Ubuntu 机器上运行脚本.我在 python2 和 python3 中得到同样的错误.

I am running the scripts in an Ubuntu machine. I get the same error in python2 and python3.

谢谢!

推荐答案

可能你的 IDE 已经创建了一个 QApplication,所以解决方案是创建一个 QApplication,如果它不存在:

Probably your IDE has already created a QApplication, so the solution is to create a QApplication if it does not exist:

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

这篇关于PySide2 没有使用基本示例正确关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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