Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在子类python中调用父方法_Python_Oop_Tkinter_Nameerror - Fatal编程技术网

在子类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。。。