Search code examples
pythontkintertic-tac-toe

List not callable in Python Tkinter Tic Tac Toe using lambda


In my code I am required to use Lambda to create the button board however I am having errors trying to call the buttons. I have tried switching the () to [] as I saw suggested in another forum however I have not been able to make it work.

from tkinter import *
import tkinter.font as font

root = Tk()
root.geometry("500x500")

myFont = font.Font(family = "Courier", size = 80)

who = True
turn = 0

#changing turns and choosing tiles
def update(x,y):
    global who, turn
    if board(x,y)["text"] == "-" and who == True:#<-----------Error
        board(x,y)["text"] = "X"
        who = False
        turn += 1
    elif board(x,y)["text"] == "-" and who == False:
        board(x,y)["text"] = "0"
        who = True
        turn += 1
    else:
        print("That Box has already been chosen")
    
    
    
board = [[Button(root, text = "-", font = myFont, command = (lambda x = x, y = y: update(x,y))) for y in range(3)] for x in range(3)]
#^^^^^ The cause of my frustration

for x in range(3):
    for y in range(3):
        board[x][y].grid(row=x,column=y)


root.mainloop()

Solution

  • Should be

    if board[x][y]["text"] == "-"
    

    Just like in the grid loop.