Python3。类,继承

问题描述 投票:1回答:1
...
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()

当点击'cancel_button'时 - 只有accept_dialog关闭,这是正确的,但是当点击'accept_button'时 - 应该关闭accept_dialog AND 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'

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

python python-3.x pyqt4
1个回答
1
投票

这里:

return super(UserInfoModalWindow, self).quit()

你要:

return super(AcceptDialogWindow, self).quit()

super()第一个参数应该是当前的类(至少对于大多数用例)。实际上super(cls, self).method()意味着:

  • 得到self的mro
  • 在mro找到班级cls
  • 上下课(cls之后)
  • 从这个类执行方法

所以在super(UserInfoModalWindow, self)AcceptDialogWindow解决了UserInfoModalWindow的父母,这是QDialog

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

© www.soinside.com 2019 - 2024. All rights reserved.