简体   繁体   English

蟒蛇3。 类,继承

[英]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.当点击“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'

What is the issue?问题是什么? What did I do wrong?我做错了什么?

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). super()第一个参数应该是当前类(至少对于大多数用例)。 Actually super(cls, self).method() means:实际上super(cls, self).method()意思是:

  • get the mro of self得到的MRO self
  • locate class cls in the mro在 mro 中找到类cls
  • take the next class (the one after cls )参加下一堂课(在cls之后的那堂课)
  • execute the method from this class执行这个类的方法

So super(UserInfoModalWindow, self) in AcceptDialogWindow resolves to the parent of UserInfoModalWindow , which is QDialog .因此, AcceptDialogWindow super(UserInfoModalWindow, self)解析为UserInfoModalWindow的父级,即QDialog

Note that in Python 3.x, you don't have to pass any argument to super() - it will automagically do the RightThing(tm).请注意,在 Python 3.x 中,您不必将任何参数传递给super() - 它会自动执行 RightThing(tm)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM