Python内核为第二轮PyQt5 GUI而死 [英] Python kernel dies for second run of PyQt5 GUI

查看:420
本文介绍了Python内核为第二轮PyQt5 GUI而死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 在Python 3.5.2中使用Spyder | Anaconda 4.2.0(64位)Windows软件包。 qt:5.6.0

  • 首次运行时,GUI窗口按预期打开

  • 第二次运行时,没有任何内容打开,接收内核死亡,重新启动日志消息。

  • Using Spyder in Python 3.5.2 |Anaconda 4.2.0 (64-bit) Windows package. qt: 5.6.0
  • For first run, GUI window opens as expected
  • For 2nd run, nothing opens, and receiving Kernel died, restarting log message.

gui1.py:


import sys from PyQt5.QtWidgets import QApplication, QWidget

app = QApplication(sys.argv)

w = QWidget()

w.resize(250,150) w.show()

#sys.exit(app.exec_()) 
app.exec_()


IPhython log:

runfile('F:/work/ws_python/TestProj1/gui1/gui1.py', wdir='F:/work/ws_python/TestProj1/gui1')

runfile('F:/work/ws_python/TestProj1/gui1/gui1.py', wdir='F:/work/ws_python/TestProj1/gui1')

Kernel died, restarting

Kernel died, restarting

Kernel died, restarting

为什么内核为第二次运行而死并且如何解决是吗?

Why kernel dies for 2nd run and how to solve it?


(即使使用#sys.exit(app.exec_())作为最后一行也一样。)

(Doing the same even using #sys.exit(app.exec_()) as last line.)


推荐答案

此代码解决了问题,感谢提示。

This code fixed the problem, thanks for the hint.

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

这篇关于Python内核为第二轮PyQt5 GUI而死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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