I'm currently trying to write a GUI for Tic Tac Toe in Python. For that purpose I am using tkinter. I want to create a button for every position to be able to make my moves by clicking one of these buttons. The problem now is that I want a function with different parameters to be called depending on the button I clicked. Therefore I tried to use lambda expressions, but no matter which button I click, it results in the same call.
import tkinter as tk
class GameWindow:
def __init__(self):
win = tk.Tk()
buttons = [[tk.Button(win,text=str(i)+str(j)) for j in range(3)] for i in range(3)]
for i in range(3):
for j in range(3):
buttons[i][j]["command"] = lambda: print([i,j])
buttons[i][j].grid(row=i, column=j)
print([i,j])
win.mainloop()
test = GameWindow()
In this case, I just want the position to be printed, but no matter which button I click, [2,2] is being printed.