PySide.QtGui RuntimeError:对象基类的'__init__'方法未被调用...但它是

Las*_*yes 4 python runtime-error initialization qtgui

一些环境基础Python版本:3.4.2操作系统:Windows 8.1

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

用于重现问题的简化代码:

基类

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)
Run Code Online (Sandbox Code Playgroud)

继承类

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)
Run Code Online (Sandbox Code Playgroud)

什么可以全力以赴

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_())
Run Code Online (Sandbox Code Playgroud)

我在一个模块里面运行这个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.
Run Code Online (Sandbox Code Playgroud)

令我困惑的部分是基类中的print语句在打印时Intefrace显然是如何被调用的 - 但它仍然会引发一个RuntimeError,说它还没有被初始化,当然也无法达到目的.创建应用程序窗口.我在stackoverflow上发现的大多数相关消息都与使用super()方法错误地初始化事物的人有关 - 但是我有五倍检查我的超级内容,我看到的一切告诉我它应该工作,除了异常我上面链接的内容.

如果我能更好地理解为什么会这样,我希望我能找到一种方法来解决它.非常感谢任何帮助 - 提前感谢!

与此同时,我将尝试找出如何无意中深入复制C++对象...

编辑: 在其他堆栈溢出帖子的链接中包含url.

101*_*101 7

添加superInterface课程使它适合我:

def __init__(self, parent, name, title_txt=None, qt_obj=None, update_log_method=None):
    super(Interface, self).__init__(parent)
    ...
Run Code Online (Sandbox Code Playgroud)

而且,你打电话self.show(),你可能意味着qmw.show().