0
for i in range(3):
    for j in range(3):
        
        btn_xo = tk.Button(master = frame, text=" ", width=40, height=6, command=onClick)
        btn_xo.grid(column =j, row =i, padx=5, pady=5)
        btn_dict[f'{i}{j}'] = btn_xo

I've been trying to create an xo game with tkinter. I created the interface with 9 buttons (3X3). The problem is that when i am trying to access the text in a given button, I can only access the last one to be added. My question is how do i access the other buttons.

here is the method connected to the buttons:

def onClick():
    btn_key=''
    global turn_num
    global btn_dict
    global btn_xo

    btn_xo = btn_dict[f'{i}{j}']

    if turn_num%2 == 1:
        btn_xo['text'] = "X"
    else:
        btn_xo['text'] = "O"
Emilia
  • 1

0 Answers0