PySide.QtGui RuntimeError: '__init__' method of object's base class not called ...but it was

PySide.QtGui RuntimeError: '__init__' method of object's base class not called ...but it was

一些环境基础知识 Python版本:3.4.2 OS: Windows 8.1

到目前为止的搜索,我怀疑 this other question 与我手头的问题有关,但我不确定我是如何复制足够多的相同条件的——可能是我缺乏深入 python知识。

重现问题的简化代码:

基础Class

from PySide.QtGui import *

class Interface(QWidget):
    '''
    Wrapper base class for GUI input QWidgets:
    - buttons
    - text fields
    - checkboxes
    - line edit
    - dropdown menu (combo box)
    '''

    def __init__(self, parent, name, title_txt=None, qt_obj=None,
        update_log_method=None):
        print('Interface base class constructor has been called.') #DEBUG
        self._parent = parent
        self.title = None
        self.name = name #also text that appears on component
        self.qt_obj = qt_obj
        self.inheritted_log_method = update_log_method

        # don't want to create an empty text QLabel, or one with
        # the text reading "None".
        if title_txt:
            self.title = QLabel(text=title_txt, parent=parent)
        print('Interface base class constructor has been completed.') #DEBUG

    def get_name(self):
        return self.name

    def update_log(self, message, level="INFO"):
        ''' '''
        self.inheritted_log_method(message, level)

继承Class

class IFPushButton(Interface):
    ''' '''

    def __init__(self, name, parent, icon=None, update_log_method=None):
        ''' '''
        # print('\n\nCHECKPOINT: pre IFPushButton super()\n\n') #DEBUG
        super(IFPushButton, self).__init__(
            parent=parent,
            name=name,
            qt_obj=QPushButton(icon, name, parent),
            update_log_method=update_log_method)
        self.behaviors = {}
        self.qt_obj.clicked.connect(self.activate)

开始一切的东西

if __name__ == '__main__':
    # setup
    import sys
    app = QApplication(sys.argv)
    qmw = QMainWindow()
    qcw = QWidget() #central widget
    qcl = QVBoxLayout(qcw) #central layout

    # experimental
    name = 'named button'
    ifpb = IFPushButton(name=name, parent=None, icon=None, update_log_method=None)
    print("as long a I don't touch the ifpb instance, everything seems to be okay.")
    print("...but the second I do...")

    qcl.addWidget(ifpb)

    self.show()

    print("name of created push button:", ifpb.get_name())

    # proper teardown
    sys.exit(app.exec_())

我 运行 这一切都在一个模块中,interface.py,当我 运行 它...

C:\Path\To\Module> python interface.py
Interface base class constructor has been called.
Interface base class constructor has been completed.
as long a I don't touch the ifpb instance, everything seems to be okay.
...but the second I do...
Traceback (most recent call last):
  File "c_interface.py", line 167, in <module>
    qcl.addWidget(ifpb)
RuntimeError: '__init__' method of object's base class (IFPushButton) not called.

让我感到困惑的部分是基础 class、Intefrace 中的打印语句在打印时显然是如何被调用的——但它仍然引发一个 RuntimeError 说它没有' 已初始化,当然无法创建应用 window。我在 Whosebug 上发现的大多数相关消息都与人们使用 super() 方法错误地初始化事物有关——但我已经对我的超级初始化进行了五次检查,我看到的一切都告诉我它应该工作,除了我在上面 link 编辑的内容。

如果我能更多地了解为什么会发生这种情况,我希望我能找到一种解决方法。非常感谢任何帮助——提前致谢!

与此同时,我将尝试找出我可能无意中深度复制 C++ 对象的原因...

编辑: 将 url 包含在 link 到其他堆栈溢出 post.

需要添加对 Interface class 构造函数的 super 调用:

def __init__(self, parent, name, title_txt=None, qt_obj=None, update_log_method=None):
    super(Interface, self).__init__(parent)
    ...

此外,您正在呼叫 self.show(),您可能指的是 qmw.show()