Python3。类,继承

时间:2018-09-18 09:46:56

标签: python python-3.x pyqt4

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

当点击“取消按钮”时-仅accept_dialog关闭,是的, 但是,当单击“ accept_button”时,应关闭accept_dialog和dialog_window。

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

出了什么问题?我做错了什么?

1 个答案:

答案 0 :(得分:1)

这里:

return super(UserInfoModalWindow, self).quit()

您要

return super(AcceptDialogWindow, self).quit()

super()第一个参数应该是当前类(至少在大多数使用情况下)。实际上super(cls, self).method()的意思是:

  • 获取self的mro
  • 在mro中找到类cls
  • 参加 next 类(cls之后的那一类)
  • 执行此类中的方法

因此super(UserInfoModalWindow, self)中的AcceptDialogWindow解析为UserInfoModalWindow的父级,即QDialog

请注意,在Python 3.x中,您不必将任何参数传递给super()-它会自动执行RightThing(tm)。