哪一个是子类ChildWidget(PySide)的父类?

时间:2015-08-17 20:12:42

标签: python class parent

你能帮助我并解释为什么print(str(self.parent()))返回MainWindow并且self.print_base()返回QWidget? parent()方法在哪里定义?在super(ChildWidget, self).__init__(parent)父进入MainWindow init或QWidget init?

import sys
from PySide import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.do_something() #sanity check
        self.cw = ChildWidget(self)
        self.setCentralWidget(self.cw)
        self.show()

    def do_something(self):

        print 'doing something!'

class ChildWidget(QtGui.QWidget):
    def print_base(self):
        for base in self.__class__.__bases__:
            print base.__name__

    def __init__(self, parent):
        super(ChildWidget, self).__init__(parent)

        print(str(self.parent()))
        self.print_base()

        self.button1 = QtGui.QPushButton()
        self.button1.clicked.connect(self.do_something_else)

        self.button2 = QtGui.QPushButton()
        self.button2.clicked.connect(self.parent().do_something)

        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.button1)
        self.layout.addWidget(self.button2)
        self.setLayout(self.layout)
        self.show()

    def do_something_else(self):
        print 'doing something else!'

1 个答案:

答案 0 :(得分:1)

您正在处理两种类型的层次结构:1)小部件的层次结构; 2)python类hiearchy。方法" print_base"列出了python POV中的所有基类,而#34; parent"返回附加子窗口小部件的窗口小部件实例。