我有一个听起来可能很奇怪的问题,但我想在我的孩子内部调用我父类的函数。
我使用MyChild
tkinter并且已经从框架中继承了。
前任:
class MyParent:
def __init__(self):
do_things()
def myfunction(self):
child_class = MyChild()
def call_me(self):
print("I'm here!")
class MyChild:
def __init__(self):
do_things()
def my_call(self):
#here call the call_me function
在这里,我希望在MyChild
类中my_call
调用call_me
函数时,它调用类的函数MyParent
。我只是想知道是否以及如何可以调用call_me
的my_call
类似功能Myparent.call_me()
根据您问题中的代码,您的孩子必须被告知其父母是什么。这通常在您创建子项时完成。子进程可以保留一个引用,以便在需要调用父进程的方法时使用它。
例子:
class MyParent:
def myfunction(self):
child_class = MyChild(parent=self)
def call_me(self):
print("I'm here!")
class MyChild:
def __init__(self, parent):
self.parent = parent
do_things()
def my_call(self):
self.parent.call_me()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句