Python 内核因 PyQt5 GUI 的第二次运行而死机
问题描述
- 在 Python 3.5.2 |Anaconda 4.2.0(64 位)Windows 软件包中使用 Spyder.qt:5.6.0
- 首次运行时,GUI 窗口会按预期打开
- 第二次运行时,没有打开任何东西,并且收到 Kernel dead, restarting 日志消息.
gui1.py:
<块引用>import sys from PyQt5.QtWidgets import QApplication, QWidget应用程序 = QApplication(sys.argv)w = QWidget()w.resize(250,150) w.show()#sys.exit(app.exec_())app.exec_()
IPhython 日志:
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')内核死机,重启内核死机,重启内核死机,重启
为什么内核在第二次运行时死机以及如何解决?
<块引用>(即使在最后一行使用 #sys.exit(app.exec_()) 也是如此.)
解决方案此代码解决了问题,感谢提示.
app = QtCore.QCoreApplication.instance()如果应用程序是无:应用程序 = QtWidgets.QApplication(sys.argv)
- 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?
(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)
相关文章