Python 3.x 在嵌套类内调用函数
我想从嵌套类(2)(类1中定义的类2)调用类(1)中定义的函数。类似于下面的代码:Python 3.x 在嵌套类内调用函数,python-3.x,class,nested,self-reference,Python 3.x,Class,Nested,Self Reference,我想从嵌套类(2)(类1中定义的类2)调用类(1)中定义的函数。类似于下面的代码: class firstclass(object): def __init__(self, *args, **kwargs): #some code and functions... def afunction(self): #some code class nestedclass(tkinter.Frame): def __init__(se
class firstclass(object):
def __init__(self, *args, **kwargs):
#some code and functions...
def afunction(self):
#some code
class nestedclass(tkinter.Frame):
def __init__(self, parent):
#some code
def anotherfunction(self):
#here I would like to call a function from the class firstclass from a button
self.abutton = tkinter.Button(self.parent, text = "blabla", command = firstclass.afunction)
self.abutton.grid(row = 0, column = 1,columnspan=1)
但是,我不断得到一个错误,缺少必需的位置参数self。如何在命令选项中指定此参数
或者有什么建议如何解决这个问题?
非常感谢您的所有输入确切地说,为什么要嵌套类?尽管如此,不管是否嵌套,
affunction
是类firstclass
的一种方法,因此您应该通过该类的实例调用它firstclass().affunction
。将firstclass.affunction替换为parent.affunction