Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Some environment basics Python Version: 3.4.2 OS: Windows 8.1

Searching so far, I suspect this other question is related to my issue at hand, but I'm not sure how I'm replicating enough of the same conditions--probably my lack of in-depth python knowledge.

Simplified code to reproduce issue:

Base 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)

Inheriting 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)

Something to kick it all off

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_())

I run this all inside one module, interface.py, and when I run it...

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.

The part that confuses me is how the print statements in the base class, Intefrace, are obviously being called as they are printing--but it still raises a RuntimeError saying that it hasn't been initialized, and of course fails to get so far as to create the app window. Most of the related messages I've found on stackoverflow are related to people initializing things incorrectly with the super() method--but I have quintuple-checked my super inits, and everything I see tells me it should be working, with the exception of what I linked above.

If I could understand more about why this is happening I'm hoping I can find a way to work around it. Any assistance is much appreciated--thanks in advance!

In the meantime I'm going to try to find how I might be unintentionally deepcopy-ing a C++ object...

EDIT: included the url in the link to other stack overflow post.

like image 361
LastTigerEyes Avatar asked May 08 '15 01:05

LastTigerEyes


1 Answers

Adding a super call to the Interface class constructor is required:

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

Also, you're calling self.show(), where you probably mean qmw.show().

like image 97
101 Avatar answered Nov 03 '22 05:11

101