Python Project : TIC TAC TOE Game GUI using Tkinter.

from tkinter import *
window = Tk()
window.title('TIC TAC TOE GAME')

def display_x_or_o(button):
    current_text=button["text"]
    if current_text==" " and not winner:
        if player.get()=="X":
            button["text"]='O'
            button.config(fg="blue")
            player.set("O")
        else:
            button["text"]='X'
            button.config(fg="red")
            player.set("X")
        check_winner()

def clear_board():
    global winner
    for button in [button1, button2, button3, button4, button5, button6, button7, button8, button9]:
        button["text"] = " "
        button.config(bg="SystemButtonFace",state=NORMAL)
    player.set("X")
    winner = False
    winner_label.config(text="")

def check_winner():
    global winner
    if (button1["text"] == button2["text"] == button3["text"] != " ") or (button4["text"] == button5["text"] == button6["text"] != " ") or (button7["text"] == button8["text"] == button9["text"] != " ") or (button1["text"] == button4["text"] == button7["text"] != " ") or (button2["text"] == button5["text"] == button8["text"] != " ") or (button3["text"] == button6["text"] == button9["text"] != " ") or (button1["text"] == button5["text"] == button9["text"] != " ") or (button3["text"] == button5["text"] == button7["text"] != " "):
        winner_label.config(text=f"Player {player.get()} wins!")
        winner=True
        for button in [button1, button2, button3, button4, button5, button6, button7, button8, button9]:
            button.config(state=DISABLED)

player=StringVar()
player.set("X")
winner=False

button1=Button(window, text=" ",font=("Arial", 20),width=5,height=2,command=lambda: display_x_or_o(button1))
button1.grid(row=0, column=0)
button2=Button(window, text=" ",font=("Arial", 20),width=5,height=2,command=lambda: display_x_or_o(button2))
button2.grid(row=0, column=1)
button3=Button(window, text=" ",font=("Arial", 20),width=5,height=2,command=lambda: display_x_or_o(button3))
button3.grid(row=0, column=2)
button4=Button(window, text=" ",font=("Arial", 20),width=5,height=2,command=lambda: display_x_or_o(button4))
button4.grid(row=1, column=0)
button5=Button(window, text=" ",font=("Arial", 20),width=5,height=2,command=lambda: display_x_or_o(button5))
button5.grid(row=1, column=1)
button6=Button(window, text=" ",font=("Arial", 20),width=5,height=2,command=lambda: display_x_or_o(button6))
button6.grid(row=1, column=2)
button7=Button(window, text=" ",font=("Arial", 20),width=5,height=2,command=lambda: display_x_or_o(button7))
button7.grid(row=2, column=0)
button8=Button(window, text=" ",font=("Arial", 20),width=5,height=2,command=lambda: display_x_or_o(button8))
button8.grid(row=2, column=1)
button9=Button(window, text=" ",font=("Arial", 20),width=5,height=2,command=lambda: display_x_or_o(button9))
button9.grid(row=2, column=2)

winner_label = Label(window, text="", font=("Arial", 16))
winner_label.grid(row=3, column=0, columnspan=3)
clear_button = Button(window, text="Clear", font=("Arial", 16), width=10, command=clear_board)
clear_button.grid(row=4, column=0, columnspan=3)

window.mainloop()





1 comment:

  1. I searched in google about python project but i found many sites, finally i got good information in your site thanks for sharing.
    If you need any construction supervisor course related service please check our website Diploma Engineering Classes in Ambernath

    ReplyDelete

Post your comments

Powered by Blogger.