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

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

It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console.
解决运行PyQt4 Gui程序后Python内核崩溃问题_第1张图片

后来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.

你可能感兴趣的:(解决运行PyQt4 Gui程序后Python内核崩溃问题)