Calling parent method inside child class python
本问题已经有最佳答案,请猛点这里访问。
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | class GUI(playGame): def __init__(self): import tkinter as tk home=tk.Tk() home.title("Tic Tac Toe") home.geometry("160x180") w,h=6,3 self.c1r1=tk.Button(text='',width=w, height=h, command=lambda: userTurn(self.c1r1)) self.c1r1.grid(column=1,row=1) home.mainloop() |
所以,userturn已经在父类playgame中定义,但是当我运行它并单击按钮c1r1时,我得到名称错误:未定义名称"userturn"
您需要向函数调用中添加一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import tkinter as tk class playGame(): def userTurn(self,foo): pass class GUI(playGame): def __init__(self): super().__init__() home=tk.Tk() home.title("Tic Tac Toe") home.geometry("160x180") w,h=6,3 self.c1r1=tk.Button(text='',width=w, height=h, command=lambda: self.userTurn(self.c1r1)) self.c1r1.grid(column=1,row=1) home.mainloop() |