Search code examples
pythonpython-3.xpyqt4

Python3. Classes, Inheritance


...
from PyQt4.QtGui import * 
from PyQt4.QtCore import *

class UserInfoModalWindow(QDialog): 
    def init(self):                                
        super(UserInfoModalWindow, self).init() 
        self.dialog_window = QDialog(self) 
        ... 
        self.dialog_window.exec_() 
        ... 
    def quit(self): 
        self.dialog_window.close()

...

class AcceptDialogWindow(UserInfoModalWindow):
    def init(self):
        super(UserInfoModalWindow, self).init() 
        self.accept_dialog = QDialog()
        ...
        self.accept_button = QPushButton()
        self.cancel_button = QPushButton()
        ... 
        self.connect(self.accept_button, 
                     SIGNAL('clicked()'), 
                     lambda: self.quit()) 
        self.connect(self.cancel_button, 
                     SIGNAL('clicked()'), 
                     self.accept_dialog.close)
        ... 
        self.accept_dialog.exec_() 
    ... 
    # From this method I want to call a method from a parent class 
    def quit(self): 
        self.accept_dialog.close() 
        return super(UserInfoModalWindow, self).quit()

When clicked 'cancel_button' - only accept_dialog closes, that's right, however when clicked 'accept_button' - accept_dialog AND dialog_window should be closed.

I get this error: File "app.py", line 252, in quit
return super(UserInfoModalWindow, self).quit() 
AttributeError: 'super' object has no attribute 'quit'

What is the issue? What did I do wrong?


Solution

  • Here:

    return super(UserInfoModalWindow, self).quit()
    

    You want:

    return super(AcceptDialogWindow, self).quit()
    

    super() first argument is supposed to be the current class (for most use case at least). Actually super(cls, self).method() means:

    • get the mro of self
    • locate class cls in the mro
    • take the next class (the one after cls)
    • execute the method from this class

    So super(UserInfoModalWindow, self) in AcceptDialogWindow resolves to the parent of UserInfoModalWindow, which is QDialog.

    Note that in Python 3.x, you don't have to pass any argument to super() - it will automagically do the RightThing(tm).