I'm trying to adapt a code i made in python to a program using tkinter. I made a list for each char in the board (X and O) and a list of buttons. each button when pressed should update its text from " " to "X" or "O" depending on the situation. The rest of the code wasn't made yet, but thats not the problem.
I'm using marcar(i,j,texto)
as the function to update the button text, using as reference the button position in the buttons list. but in line 25, the error of "list index out of range" shows up. I use the same command in the line 31. I suppose that it is because in the line 29 where I create the buttons, one of the things used to create them is the method marcar()
, and as it hasn't been created any other in the list, I cant reference it.
I can't find any solution to making a button that when pressed it uses a method that references the same button, because to create it, I need the function.
from tkinter import *
lista = [[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]]
buttons = [[], [], []]
vitx = 0
vito = 0
root = Tk()
turno = Label(root, text="Turno de X",font=("Arial",15))
turno.grid(row = 1,column = 1)
vitoriasx = Label(root, text="Vitórias de X:"+str(vitx), font=("Arial",8))
vitoriasx.grid(row = 1,column = 0)
vitoriaso = Label(root, text="Vitórias de O:"+str(vito), font=("Arial",8))
vitoriaso.grid(row = 1,column = 2)
def marcar(i,j,texto):
lista[i][j] = texto
buttons[i][j].config(text= texto)
for i in range(0,3):
for j in range(0,3):
buttons[i].append(Button(root,text=lista[i][j],command = marcar(i,j,"x")))
buttons[i][j].grid(row = i+2,column = j)
buttons[i][j].config(height=6, width=13)
root.mainloop()