Python3。 类, 继承
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()
单击时 'cancel_button' - 只有 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'
这是什么问题?我做错了什么?
这里:
return super(UserInfoModalWindow, self).quit()
你想要:
return super(AcceptDialogWindow, self).quit()
super()
第一个参数应该是当前的 class (至少对于大多数用例)。实际上 super(cls, self).method()
的意思是:
- 获取
self
的 mro
- 在 mro
中找到 class cls
- 走下一个class(
cls
之后的那个)
- 从这个class
执行方法
因此 AcceptDialogWindow
中的 super(UserInfoModalWindow, self)
解析为 UserInfoModalWindow
的父级,即 QDialog
。
请注意,在 Python 3.x 中,您不必将任何参数传递给 super()
- 它会自动执行 RightThing(tm)。
...
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 和 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'
这是什么问题?我做错了什么?
这里:
return super(UserInfoModalWindow, self).quit()
你想要:
return super(AcceptDialogWindow, self).quit()
super()
第一个参数应该是当前的 class (至少对于大多数用例)。实际上 super(cls, self).method()
的意思是:
- 获取
self
的 mro
- 在 mro 中找到 class
- 走下一个class(
cls
之后的那个) - 从这个class 执行方法
cls
因此 AcceptDialogWindow
中的 super(UserInfoModalWindow, self)
解析为 UserInfoModalWindow
的父级,即 QDialog
。
请注意,在 Python 3.x 中,您不必将任何参数传递给 super()
- 它会自动执行 RightThing(tm)。