Python:RuntimeError:从未调用过 %S 的父类(super class) __init__()

标签 python pyqt runtime-error superclass deep-copy

我尝试对 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_())

问题是什么?

最佳答案

如果你想继承QObject(或QWidget),你必须总是调用父类(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属性直到你这样做了。


编辑: 在您的情况下,您正在尝试深度复制 QWidget,但这是不可能的。 Python 可能能够复制 QWidgetwrapper,但是 QWidget 本身是一个 C++ 对象,python 无法使用copy.deepcopy,因此每当您调用复制实例的方法时,您都会收到 RuntimeError,因为底层 C++ 对象未正确初始化。

酸洗这些对象也是如此。 Python 能够 pickle wrapper,而不是 C++ 对象本身,因此当 unpickling 实例时,结果是一个损坏的实例。

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

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

关于Python:RuntimeError:从未调用过 %S 的父类(super class) __init__(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12280371/

相关文章:

python - 在保持图像纵横比的同时,在水平布局内调整像素图大小行为的标签不一致

python - PyQt5动态添加矩形到QML网格

for-loop - Go中for循环内的错误处理可能会导致下一次迭代

c++ - 如何修改 C++ runtime_error 的 what 字符串?

python - 算法题: Finding the cheapest flight

python - 使用 Doxygen 记录 UTF-8 Python 代码

python - 在循环(或推导式)中创建函数(或 lambda)

python - pyqt4 QTabWidget addtab不成功

c - Ferror 是否会进行多次写入?

php - Microsoft Word 自动化 - 以编程方式删除标题(及其子 indo)