本文介绍了python pyqt和父类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class testWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(testWidget, self).__init__(parent)
        self.parent = parent
        self.something()
    def something(self):
        self.parent.callme() # self.parent?.... nice?


class testClass():
    def __init__(self):
        self.widget = testWidget(parent=self)

test = testClass()

在python(pyqt)中处理父类的最干净方法是什么?
是否有比直接调用self.parent更好的方法?

What is the cleanest way of dealing with a parent class in python(pyqt)?Is there a nicer way than calling self.parent directly?

推荐答案

如果要调用此方法小部件的父项(如果已设置),请使用:

If you want to call a method of this widget's parent (if one has been set), use QObject.parent():

class TestWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(TestWidget, self).__init__(parent)
    def something(self):
        self.parent().callMe()

class TestClass(QtCore.QObject):
    def __init__(self):
        super(TestClass, self).__init__(None)
        self.widget = TestWidget(parent=self)
        ...
    def callMe(self): pass

test = TestClass()

这篇关于python pyqt和父类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:09