Python:RuntimeError:从未调用%S的超类__init __() [英] Python: RuntimeError: super-class __init__() of %S was never called

查看:366
本文介绍了Python:RuntimeError:从未调用%S的超类__init __()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对Python中的对象(继承自不同类的类的实例,具体来说,<$ c)进行某些操作( setParent ) $ c> QtGui.QLabel ),但在运行时出现了上述错误。对象本身具有一些具有实际内容的字段(已在调试中验证),但是由于某种原因,我无法使用它。错误是什么意思,我该如何解决?有关其他信息,我将说该对象是在尝试对该对象进行操作之前从静态方法返回的。

I tried to do some operation (setParent) on an object in Python (an instance of a class which inherits from a different class - to be specific, QtGui.QLabel), but during runtime the above error was raised. The object itself has had some fields with actual content (verified on debug), but from some reason I couldn't "use" it. What does the error mean and how can I fix it? For some additional information, I shall say that the object was returned from a static method before I tried to do this operation on it.

子类的 __ init __()本身的功能:

def __init__(self, image, father):
        super(AtomicFactory.Image, self).__init__(father)
        self.raw_attributes = image.attributes
        self.attributes = {}
        father.addChild(self)
        self.update()

现在我写了一个类似的代码,一个简单的,窗口移动时,在 widget.setParent(mw)行上出现相同的错误。

Now I wrote a similar code, a simple one, that had the same error on the line widget.setParent(mw) when the window was moved.

#!/usr/bin/env python
import sys
import copy
from PyQt4 import QtCore, QtGui

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_widget=QtGui.QWidget()
    widget = QtGui.QPushButton('Test')
    widget.resize(640, 480)
    widget.setParent(main_widget)
    widget.move(0, 0)
    widget2=QtGui.QPushButton('Test2')
    widget2.i=0
    widget2.resize(600, 200)
    widget2.setParent(main_widget)
    widget2.move(640, 0)
    def onResize(event):
        print event
        mw=copy.deepcopy(main_widget)
        widget.setParent(mw)
        widget2.setParent(mw)
        widget.move(0, 0)
        widget2.move(640, 480)
        main_widget_width=main_widget.width()
        widget_width=widget.width()
        width2=main_widget_width-widget_width
        height2=widget2.height()
        widget2.resize(width2, height2)
        widget2.move(640, 0)
    main_widget.resizeEvent=onResize
    def onClick():
        size=(widget2.width(), widget2.height())
        if(widget2.i%2==0):
            widget2.resize(int(size[0]/2), int(size[1]/2))
        else:
            widget2.resize(size[0]*2, size[1]*2)
        widget2.i+=1
    QtCore.QObject.connect(widget, QtCore.SIGNAL('clicked()'), onClick)
    main_widget.show()
    sys.exit(app.exec_())

出了什么问题?

推荐答案

如果要继承 QObject (或 QWidget ),则必须始终调用超类 __ init __

If you want to inherit QObject (or QWidget), you must always call the super-class __init__:

class MyObject(QObject):
    def __init__(self, *args, **kwargs):
        super(MyObject, self).__init__(arguments to parent class)
        #other stuff here

您也可以在之后叫家长的班级的 __ init __ 一些说明,但是除非您这样做,否则您不能调用 QObject 方法或使用 QObject 属性。

You may also call the parent's class's __init__ after some instructions, but you can't call QObject methods or use QObject attributes until you did so.

编辑
在您的情况下,您尝试深度复制一个 QWidget ,但这不可能
Python也许可以复制 QWidget 包装器,但是 QWidget 本身是python无法使用默认实现 copy.deepcopy 处理的C ++对象,因此,每当调用复制实例的方法时,都会得到 RuntimeError ,因为底层C ++对象未正确初始化。

Edit: In your case you are trying to deepcopy a QWidget, but this is not possible. Python may be able to copy the wrapper of the QWidget, but the QWidget itself is a C++ object that python cannot handle with the default implementation of copy.deepcopy, hence whenever you call a method of the copied instance you get the RuntimeError because the underlying C++ object wasn't initialized properly.

对这些对象进行腌制也是如此。 Python能够腌制包装器,而不是C ++对象本身,因此取消腌制实例时,结果是损坏的实例。

The same is true for pickling these objects. Python is able to pickle the wrapper, not the C++ object itself, hence when unpickling the instance the result is a corrupted instance.

为了支持 deepcopy() QWidget 类应实现 __ deepcopy __ 方法,但不是做到这一点。

In order to support deepcopy() the QWidget class should implement the __deepcopy__ method, but it does not do that.

如果要复制小部件,则必须自己通过以下方式实现所有机制:手。

If you want to copy widgets you'll have to implement by yourself all the mechanism by hand.

这篇关于Python:RuntimeError:从未调用%S的超类__init __()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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