I have a question that sounds perhaps strange, but I want to call a function of my parent class inside my children.
I use tkinter and MyChild
allready inherrit from a frame.
ex:
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
here I want that when in MyChild
class the my_call
function is called, it calls the call_me
function of the MyParent
class.
I just want to know if and how it is possible to call call_me
in the my_call
function like Myparent.call_me()
答案 0 :(得分:0)
根据您问题中的代码,必须告诉您的孩子父母是谁。通常在创建子代时完成。子级可以保留一个引用,以便在需要调用父级上的方法时可以使用它。
示例:
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()