'How to solve this lambda self python inside class
from tkinter import *
class ticTactoe:
def __init__(self):
self.root=Tk()
self.root.title("Tic Tac Toe")
self.num=-1
for i in range(3):
for j in range(3):
self.num+=1
exec("btn"+str(self.num)+"=StringVar()")
b = "b"+str(i)+str(j)+"=Button(self.root,padx=68,pady=68,bg='#8cff1a',bd=5,relief=SUNKEN,textvariable='btn'+str(self.num),command=lambda :self.game(self,i,j)).grid(row=i,column=j)"
exec(b)
def game(self,r,c):
Label(self.root,text="X",font="40").grid(row=r,column=c)
def mainloop(self):
self.root.mainloop()
t = ticTactoe()
t.mainloop()
Running
PS F:\TKinter> python 17.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\mpank\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "<string>", line 1, in <lambda>
NameError: name 'self' is not defined
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|