解决运行PyQt4 Gui程序后Python内核崩溃问题

运行PyQt4 Gui程序后,Python内核每次都会崩溃,如下:

It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console.

后来stackoverflow.com和reddit.com找到了解决办法,就是调用前先重置app对象。
这个问题应该是Spyder在每次程序结束后仍保留了原有app的值,导致在退出程序时执行出错。

http://stackoverflow.com/questions/24041259/python-kernel-crashes-after-closing-an-pyqt4-gui-application

The easy solution here
https://www.reddit.com/r/learnpython/comments/45h05k/solved_kernel_crashing_when_closing_gui_spyder/

if __name__ == "__main__": 
  app=0 #This is the solution 
  app = QtGui.QApplication(sys.argv) 
  MainApp = Dice_Roller() 
  MainApp.show()   
  sys.exit(app.exec_())

My assessment of the problem is that Spyder retains values for objects between runs and re-instantiating QtGui.QApplication() to "app" while the previous instance was still in memory was causing the kernel to crash. By writing over the namespace "app" and giving it an arbitrary value of 0 (the commented line), I am able to avoid crashing the kernel.
While this may be a well-known quirk with Spyder for other Python users, I wanted an answer to be available for others that encounter a similar problem.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • “这是最好的时代,这是最坏的时代,这是智慧的时代,这是愚蠢的时代,这是大数据的云时代。”大数据一词自2012...
    JKmelody阅读 4,187评论 0 0
  • 作别2017,我们踏上飞驰的列车迎接新年的到来。没有鞭炮齐鸣,到我们在欢声笑语中体会欢乐。 欢聚一堂,是我们在奔波...
    晓荍阅读 1,254评论 0 0
  • 那一天我二十一岁,在我一生的黄金时代。我有好多奢望。我想爱,想吃,还想在一瞬间变成天上半明半暗的云。后来我才知道,...
    待汝豪杰只是凡夫阅读 2,662评论 0 0
  • 焦点父母课堂洛阳一期班李萍分享第9天 孩子越来越大,越来越有自己的想法和主见了,不像小时候带她去上什么就上什...
    crystal平平阅读 2,523评论 0 1