Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 蟒蛇3。类、继承_Python_Python 3.x_Pyqt4 - Fatal编程技术网

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