adamgeo1
adamgeo1

Reputation: 53

Why is my function not affecting a button in tkinter?

I'm trying to make Tic Tac Toe in python using tkinter, but I'm running into an issue. I'm trying to make it so when you click on one of the squares, it displays whichever player's symbol on that square. However, with my current function playerCheck, nothing happens when I press them. I can't figure out why this is happening, so I would appreciate any help.

Keep in mind I am in no way finished.

import tkinter as tk

Player1 = "X"
Player2 = "O"
turn = None
playerNumber = None

def playerCheck(function):
    if turn == Player1:
        playerNumber = Player1
        function("white", playerNumber)

    elif turn == Player2:
        playerNumber = Player2
        function("white", playerNumber)

def topLeft(color, player):
    topL = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(topL)).grid(row=0,column=0)

def topMiddle(color, player):
    topM = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(topMiddle)).grid(row=0,column=1)

def topRight(color, player):
    topR = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(topRight)).grid(row=0,column=2)

def middleLeft(color, player):
    midL = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(middleLeft)).grid(row=1,column=0)

def middleMiddle(color, player):
    midM = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(middleMiddle)).grid(row=1,column=1)

def middleRight(color, player):
    midR = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(middleRight)).grid(row=1,column=2)

def bottomLeft(color, player):
    botL = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(bottomLeft)).grid(row=2,column=0)

def bottomMiddle(color, player):
    botM = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(bottomMiddle)).grid(row=2,column=1)

def bottomRight(color, player):
    botR = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(bottomRight)).grid(row=2,column=2)

def gameStart():
    topLeft("white", "")
    topMiddle("white", "")
    topRight("white", "")
    middleLeft("white", "")
    middleMiddle("white", "")
    middleRight("white", "")
    bottomLeft("white", "")
    bottomMiddle("white", "")
    bottomRight("white", "")
    turn = Player1

def Main():
    a = tk.Tk()
    a.title("Tick Tack Toe")
    a.geometry("250x250")
    gameStart()

    a.mainloop()
Main()

Upvotes: 0

Views: 239

Answers (1)

Barmar
Barmar

Reputation: 780909

The argument to playerCheck() is a button, not a function. You should change the button's text and color, not try to call it.

def playerCheck(button):
    global turn
    button['text'] = turn
    button['fg'] = "white"
    if turn == Player1:
        turn = Player2
    else:
        turn = Player1

Upvotes: 2

Related Questions