Python 蟒蛇3。类、继承
单击“取消”按钮时,仅接受对话框关闭,没错, 但是,单击“接受”按钮时,应关闭“接受”对话框和对话框窗口Python 蟒蛇3。类、继承,python,python-3.x,pyqt4,Python,Python 3.x,Pyqt4,单击“取消”按钮时,仅接受对话框关闭,没错, 但是,单击“接受”按钮时,应关闭“接受”对话框和对话框窗口 ... from PyQt4.QtGui import * from PyQt4.QtCore import * class UserInfoModalWindow(QDialog): def init(self): super(UserInfoModalWindow, self).init()
...
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()
问题是什么?我做错了什么?这里:
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()
super()
第一个参数应该是当前类(至少对于大多数用例)。实际上super(cls,self).method()
的意思是:
- 获取
self的mro
- 在mro中找到类
cls
- 下一节课(cls后面的那节课)
- 从这个类执行这个方法
AcceptDialogWindow
中的super(UserInfoModalWindow,self)
解析为UserInfoModalWindow
的父级,即QDialog
请注意,在Python3.x中,您不必将任何参数传递给
super()
——它将自动执行正确的操作(tm)。您可能应该提到纯无参数super()
,非常感谢,我明白了。您在这里使用的是Python2的操作方式。
return super(AcceptDialogWindow, self).quit()