如何在Spyder中运行时防止从QTapplication错误退出

时间:2015-07-27 11:05:18

标签: python pyqt spyder

此问题与显示here

的代码有关

更为直接的代码可能就是这样:

import sys
import os
from PyQt4 import QtGui


class Window(QtGui.QDialog):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        # Just some extra button to mess around
        self.button= QtGui.QPushButton('Push Me')

        # set the layout
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.button)
        self.setLayout(layout)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = Window()
    main.show()

    sys.exit(app.exec_())

当我运行时,会出现一个非常愚蠢的一键式窗口,它什么都不做:-) 但是,当我关闭它时,我从IPython控制台收到以下投诉:

An exception has occurred, use %tb to see the full traceback.

To exit: use 'exit', 'quit', or Ctrl-D.
An exception has occurred, use %tb to see the full traceback.

SystemExit: 0


In [3]: %tb
Traceback (most recent call last):

  File "<ipython-input-2-0a8f6cad0df5>", line 1, in <module>
    runfile('C:/Users/my_username/AppData/Local/Continuum/Anaconda3/Lib/site-packages/PyQt4/SE2.py', wdir='C:/Users/mtk10531/AppData/Local/Continuum/Anaconda3/Lib/site-packages/PyQt4')

  File "C:\Users\my_username\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)

  File "C:\Users\my_username\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "C:/Users/my_username/AppData/Local/Continuum/Anaconda3/Lib/site-packages/PyQt4/SE2.py", line 39, in <module>
    sys.exit(app.exec_())

SystemExit: 0

然后,再次运行会产生以下结果:

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

我发现其他帖子抱怨类似的内容,herehere

我试图从答案中实现我理解的(不多),即:

  • 将我的脚本名称传递给QApplicationapp = QtGui.QApplication(['my_file.py'])然而这没有效果

  • 从我的app = QtGui.QApplication(['my_file.py'])中删除main(),但在这种情况下,内核在执行任何操作之前就已经死了......

任何提示?

干杯, 米歇尔

P.S。 - 只是看着追溯中\/的波浪图案让我感到恶心!

0 个答案:

没有答案