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日志:
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
为什么内核为第二次运行而死,以及如何解决它?
(即使使用#sys.exit(app.exec_())作为最后一行也一样。)
答案 0 :(得分:9)
此代码解决了问题,感谢提示。
app = QtCore.QCoreApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
答案 1 :(得分:1)
这对于内核死机更好,重启错误。
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.QtCore import QCoreApplication
#app = QApplication(sys.argv)
app = QCoreApplication.instance()
if app is None:
app = QApplication(sys.argv)
答案 2 :(得分:0)
对我来说,上述解决方案有效但只有窗口关闭按钮(来自Window装饰)用于关闭主窗口。但是当程序从GUI信号处理程序终止时,问题仍然存在,例如,点击一个按钮。经过多次摆弄后,我了解到在这种情况下终止的安全方法如下:
def safeExit(self):
"""exit the application gently so Spyder IDE will not hang"""
self.ui.deleteLater()
self.ui.close()
self.ui.destroy()
... self.ui.Button2.clicked.connect(self.safeExit) ...