【问题标题】:Python: RuntimeError: super-class __init__() of %S was never calledPython:RuntimeError:%S 的超类 __init__() 从未被调用
【发布时间】:2012-08-30 02:38:20
【问题描述】:

我尝试对 Python 中的一个对象(继承自不同类的类的实例 - 具体而言,QtGui.QLabel)执行一些操作 (setParent),但在运行时引发了上述错误。该对象本身有一些具有实际内容的字段(在调试时验证),但由于某种原因我不能“使用”它。错误是什么意思,我该如何解决?对于一些附加信息,我会说该对象是在我尝试对其执行此操作之前从静态方法返回的。

子类有自己的__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) 行有同样的错误。

#!/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_())

出了什么问题?

【问题讨论】:

  • 如果您的派生类MyDerived 有自己的__init__,请尝试添加super(MyDerived, self).__init__() 这一行。
  • 确实如此。该对象实际上是之前创建的并且使用得很好。我也可以在初始阶段毫无问题地使用 setParent()。然后它被插入到一个列表中,我上面描述的静态方法在列表中“找到”了它,然后将它返回给另一个静态方法。当后者尝试对其使用 setParnet() 时,返回了错误。反正子类的 __init__() 自定义函数是:
  • def __init__(self, image, Father): super(AtomicFactory.Image, self).__init__(father) self.raw_attributes = image.attributes self.attributes = {} Father.addChild(self) self.update()
  • @Martijn Pieters 谢谢,完成
  • 你的子类是什么?是AtomicFactory.Image,还是别的什么?

标签: python pyqt runtime-error superclass deep-copy


【解决方案1】:

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

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

你也可以在一些指令之后调用父类的__init__,但在你这样做之前你不能调用QObject方法或使用QObject属性。


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

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

为了支持deepcopy()QWidget 类应该实现__deepcopy__ 方法,但它没有这样做。

如果你想复制小部件,你必须自己手动实现所有机制。

【讨论】:

  • 请参阅上面我写给 Martijn Pieters 的答案。我不知道如何以更清晰的方式分享我的代码......
猜你喜欢
  • 2014-03-07
  • 1970-01-01
  • 2017-12-13
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 2018-12-02
  • 2011-04-16
相关资源
最近更新 更多