简单的IPython示例在sys.exit()上引发异常 [英] simple IPython example raises exception on sys.exit()

查看:3929
本文介绍了简单的IPython示例在sys.exit()上引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IPython中做了一些很简单的PySide(和PyQt)教程。一个教程只是创建一个带有滑块的窗口来演示插槽和信号。



当我关闭运行的演示应用程序的窗口时,我看到这个错误:

 发生异常,使用%tb查看完整的追溯。 
SystemExit:0
退出:使用'退出','退出'或Ctrl-D。

所以我运行%tb并得到这个:

  SystemExit Traceback(最近的最后一次调用)
/ Workspaces / scratch /< ipython-input-1-88966dcfb499>在< module>()
33
34 if __name__ ==__main__:
---> 35 main()

/工作区/ scratch /< ipython-input-1-88966dcfb499>在main()
29 w.show()
30 app.exec_()
---> 31 sys.exit(0)
32
33

SystemExit:0

如果我再次尝试执行我的代码,我得到这个:

  RuntimeError:一个QApplication实例存在。 

如果有帮助,这里我的代码:



来自PySide.QtCore的*

从PySide.QtGui导入$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b def __init __(self):
QWidget .__ init __(self,None)

vbox = QVBoxLayout(self)

self.slider1 = QSlider(Qt。水平)
self.slider1.setRange(0,99)
self.slider1.setValue(0)
vbox.addWidget(self.slider1)

self。 slider2 = QSlider(Qt.Horizo​​ntal)
self.slider2.setRange(0,99)
self.slider2.setValue(99)
vbox.addWidget(self.slider2)

self.slider1.valueChanged.connect(self.slider2Changed)

def slider2Changed(self,position):
self.slider2.setValue(self.slider2.maximum() - 位置)

def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
app.exec_()
sys.exit(0)

如果__name__ ==__main__:
main()
/ pre>

使用python运行代码时,我没有错误:

  python myexample.py 

只有当我在IPython中运行代码(包括笔记本或qtconsole或普通ipython终端)。



更新:我的主要问题是我无法再次快速,轻松地运行应用程序。如果我再次尝试运行我的代码,我得到这个:

  RuntimeError:QApplication实例已经存在。 

杀死IPython的快速,互动性:(

解决方案

您需要做的是使QApplication稍后被删除:

  app = QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)

使用此代码,您可以按照IPython或其他任何位置重新运行应用程序多次,每次关闭qt应用程序时,该对象将在python中删除。


I'm doing some very simple PySide (and PyQt) tutorials in IPython. One tutorial just creates a window with some sliders to demonstrate slots and signals.

When I close the window of the running demo application, I see this error:

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

So I run %tb and get this:

SystemExit                                Traceback (most recent call last)
/Workspaces/scratch/<ipython-input-1-88966dcfb499> in <module>()
     33 
     34 if __name__ == "__main__":
---> 35     main()

/Workspaces/scratch/<ipython-input-1-88966dcfb499> in main()
     29         w.show()
     30         app.exec_()
---> 31         sys.exit(0)
     32 
     33 

SystemExit: 0

If I try to execute my code again, I get this:

RuntimeError: A QApplication instance already exists.

In case it helps, here my code:

from PySide.QtCore import *
from PySide.QtGui import *
import sys

class MyWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self, None)

        vbox = QVBoxLayout(self)

        self.slider1 = QSlider(Qt.Horizontal)
        self.slider1.setRange(0, 99)
        self.slider1.setValue(0)
        vbox.addWidget(self.slider1)

        self.slider2 = QSlider(Qt.Horizontal)
        self.slider2.setRange(0, 99)
        self.slider2.setValue(99)
        vbox.addWidget(self.slider2)

        self.slider1.valueChanged.connect(self.slider2Changed)

    def slider2Changed(self, position):
        self.slider2.setValue(self.slider2.maximum() - position)

def main():
        app = QApplication(sys.argv)
        w = MyWindow()
        w.show()
        app.exec_()
        sys.exit(0)

if __name__ == "__main__":
    main()

I do not have errors when running the code using python:

python myexample.py

This error only happens when I run the code in an IPython (including a notebook or the qtconsole or the regular ipython terminal).

UPDATE: My main problem is that I cannot run the application again quickly and easily. If I try to run my code again, I get this:

RuntimeError: A QApplication instance already exists.

That kills the fast, interactive nature of IPython :(

解决方案

What you need to do is to cause the QApplication to be deleted later as in:

app = QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)

Using this code you can rerun the application as many times as you want in IPython, or anywhere else and every time you close your qt application, the object will be deleted in python.

这篇关于简单的IPython示例在sys.exit()上引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆