在子类python中调用父方法
这是我的密码:在子类python中调用父方法,python,oop,tkinter,nameerror,Python,Oop,Tkinter,Nameerror,这是我的密码: 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=''
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时,我得到
NameError:未定义名称“userTurn”您需要在函数调用中添加一个self。您可能应该在init中调用super:
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()
打字错误:更改用户转身。。。到self.userTurn。。。