首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PySide.QtGui RuntimeError:对象基类的“__init__”方法(不称为...but )

PySide.QtGui RuntimeError:对象基类的“__init__”方法(不称为...but )
EN

Stack Overflow用户
提问于 2015-05-08 01:40:38
回答 1查看 6.6K关注 0票数 6

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

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

复制问题的简化代码:

基类

代码语言:javascript
复制
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)

继承类

代码语言:javascript
复制
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)

什么的把它从身上踢出来

代码语言:javascript
复制
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,当我运行它.

代码语言:javascript
复制
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.

让我困惑的部分是基类Intefrace中的print语句在打印时是如何被调用的--但是它仍然引发了一个RuntimeError,它说它还没有初始化,当然也没有创建应用程序窗口。我在堆栈溢出中找到的大部分相关消息都与使用super()方法初始化错误有关--但我已经检查了我的超级inits,我看到的所有信息都应该正常工作,除了上面链接的内容。

如果我能更好地理解为什么会发生这种情况,我希望我能找到一个解决这个问题的方法。任何帮助都是非常感谢的-谢谢!

在此期间,我将试图找到我可能会无意中深入复制一个C++对象.

编辑:将url包含在指向其他堆栈溢出帖子的链接中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-08 02:23:20

需要向super类构造函数添加一个Interface调用:

代码语言:javascript
复制
def __init__(self, parent, name, title_txt=None, qt_obj=None, update_log_method=None):
    super(Interface, self).__init__(parent)
    ...

此外,您还调用了self.show(),您可能是指qmw.show()

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30114436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档