import tkinter as tk
from tkinter import messagebox
root=tk.Tk()
root.title("Tic-Tac-Toe Game")
root.geometry("400x200")

label1=tk.Label(root,text="Sarfaraj's Tic-Tac-Toe Game")
label1.grid(row=0,column=0)
label2=tk.Label(root,text="Player 1: O")
label2.grid(row=2,column=0)
label3=tk.Label(root,text="Player 2: X")
label3.grid(row=3,column=0)
turn=1
def clicked1():
    global turn
    if btn1["text"]==" ":
        if turn==1:
            turn =2
            btn1["text"]="O"
        elif turn==2:
            turn=1
            btn1["text"]="X"
        check()
def clicked2():
    global turn
    if btn2["text"]==" ":
        if turn==1:
            turn =2
            btn2["text"]="O"
        elif turn==2:
            turn=1
            btn2["text"]="X"
        check()
def clicked3():
    global turn
    if btn3["text"]==" ":
        if turn==1:
            turn =2
            btn3["text"]="O"
        elif turn==2:
            turn=1
            btn3["text"]="X"
        check()
def clicked4():
    global turn
    if btn4["text"]==" ":
        if turn==1:
            turn =2
            btn4["text"]="O"
        elif turn==2:
            turn=1
            btn4["text"]="X"
        check()
def clicked5():
    global turn
    if btn5["text"]==" ":
        if turn==1:
            turn =2
            btn5["text"]="O"
        elif turn==2:
            turn=1
            btn5["text"]="X"
        check()
def clicked6():
    global turn
    if btn6["text"]==" ":
        if turn==1:
            turn =2
            btn6["text"]="O"
        elif turn==2:
            turn=1
            btn6["text"]="X"
        check()
def clicked7():
    global turn
    if btn7["text"]==" ":
        if turn==1:
            turn =2
            btn7["text"]="O"
        elif turn==2:
            turn=1
            btn7["text"]="X"
        check()
def clicked8():
    global turn
    if btn8["text"]==" ":
        if turn==1:
            turn =2
            btn8["text"]="O"
        elif turn==2:
            turn=1
            btn8["text"]="X"
        check()
def clicked9():
    global turn
    if btn9["text"]==" ":
        if turn==1:
            turn =2
            btn9["text"]="O"
        elif turn==2:
            turn=1
            btn9["text"]="X"
        check()
flag=1
def check():
    global flag
    b1 = btn1["text"]
    b2 = btn2["text"]
    b3 = btn3["text"]
    b4 = btn4["text"]
    b5 = btn5["text"]
    b6 = btn6["text"]
    b7 = btn7["text"]
    b8 = btn8["text"]
    b9 = btn9["text"]
    flag=flag+1
    if b1==b2 and b1==b3 and b1=="O" or b1==b2 and b1==b3 and b1=="X":
        win(btn1["text"])
    if b4==b5 and b4==b6 and b4=="O" or b4==b5 and b4==b6 and b4=="X":
        win(btn4["text"])
    if b7==b8 and b7==b9 and b7=="O" or b7==b8 and b7==b9 and b7=="X":
        win(btn7["text"])
    if b1==b4 and b1==b7 and b1=="O" or b1==b4 and b1==b7 and b1=="X":
        win(btn1["text"])
    if b2==b5 and b2==b8 and b2=="O" or b2==b5 and b2==b8 and b2=="X":
        win(btn2["text"])
    if b3==b6 and b3==b9 and b3=="O" or b3==b6 and b3==b9 and b3=="X":
        win(btn3["text"])
    if b1==b5 and b1==b9 and b1=="O" or b1==b5 and b1==b9 and b1=="X":
        win(btn1["text"])
    if b7==b5 and b7==b3 and b7=="O" or b7==b5 and b7==b3 and b7=="X":
        win(btn7["text"])
    if flag ==10:
        messagebox.showinfo("Match Tied", "Please try Again!!!!!")
        root.destroy()
def exit():
    root.destroy()
def win(player):
        playername = "Game complete " +player + " won!!!! "
        messagebox.showinfo("Congratulations", playername)
btn1 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked1)
btn1.grid(column=2, row=2)
btn2 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked2)
btn2.grid(column=3, row=2)
btn3 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked3)
btn3.grid(column=4, row=2)
btn4 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked4)
btn4.grid(column=2, row=3)
btn5 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked5)
btn5.grid(column=3, row=3)
btn6 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked6)
btn6.grid(column=4, row=3)
btn7 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked7)
btn7.grid(column=2, row=4)
btn8 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked8)
btn8.grid(column=3, row=4)
btn9 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked9)
btn9.grid(column=4, row=4)
btn11 = tk.Button(root,text="Exit",bg="white", fg="Black",command=exit)
btn11.grid(column=0, row=7)
root.mainloop()
import tkinter as tk
from tkinter import messagebox
root=tk.Tk()
root.title("Tic-Tac-Toe Game")
root.geometry("400x200")

label1=tk.Label(root,text="Sarfaraj's Tic-Tac-Toe Game")
label1.grid(row=0,column=0)
label2=tk.Label(root,text="Player 1: O")
label2.grid(row=2,column=0)
label3=tk.Label(root,text="Player 2: X")
label3.grid(row=3,column=0)
turn=1
def clicked1():
    global turn
    if btn1["text"]==" ":
        if turn==1:
            turn =2
            btn1["text"]="O"
        elif turn==2:
            turn=1
            btn1["text"]="X"
        check()
def clicked2():
    global turn
    if btn2["text"]==" ":
        if turn==1:
            turn =2
            btn2["text"]="O"
        elif turn==2:
            turn=1
            btn2["text"]="X"
        check()
def clicked3():
    global turn
    if btn3["text"]==" ":
        if turn==1:
            turn =2
            btn3["text"]="O"
        elif turn==2:
            turn=1
            btn3["text"]="X"
        check()
def clicked4():
    global turn
    if btn4["text"]==" ":
        if turn==1:
            turn =2
            btn4["text"]="O"
        elif turn==2:
            turn=1
            btn4["text"]="X"
        check()
def clicked5():
    global turn
    if btn5["text"]==" ":
        if turn==1:
            turn =2
            btn5["text"]="O"
        elif turn==2:
            turn=1
            btn5["text"]="X"
        check()
def clicked6():
    global turn
    if btn6["text"]==" ":
        if turn==1:
            turn =2
            btn6["text"]="O"
        elif turn==2:
            turn=1
            btn6["text"]="X"
        check()
def clicked7():
    global turn
    if btn7["text"]==" ":
        if turn==1:
            turn =2
            btn7["text"]="O"
        elif turn==2:
            turn=1
            btn7["text"]="X"
        check()
def clicked8():
    global turn
    if btn8["text"]==" ":
        if turn==1:
            turn =2
            btn8["text"]="O"
        elif turn==2:
            turn=1
            btn8["text"]="X"
        check()
def clicked9():
    global turn
    if btn9["text"]==" ":
        if turn==1:
            turn =2
            btn9["text"]="O"
        elif turn==2:
            turn=1
            btn9["text"]="X"
        check()
flag=1
def check():
    global flag
    b1 = btn1["text"]
    b2 = btn2["text"]
    b3 = btn3["text"]
    b4 = btn4["text"]
    b5 = btn5["text"]
    b6 = btn6["text"]
    b7 = btn7["text"]
    b8 = btn8["text"]
    b9 = btn9["text"]
    flag=flag+1
    if b1==b2 and b1==b3 and b1=="O" or b1==b2 and b1==b3 and b1=="X":
        win(btn1["text"])
    if b4==b5 and b4==b6 and b4=="O" or b4==b5 and b4==b6 and b4=="X":
        win(btn4["text"])
    if b7==b8 and b7==b9 and b7=="O" or b7==b8 and b7==b9 and b7=="X":
        win(btn7["text"])
    if b1==b4 and b1==b7 and b1=="O" or b1==b4 and b1==b7 and b1=="X":
        win(btn1["text"])
    if b2==b5 and b2==b8 and b2=="O" or b2==b5 and b2==b8 and b2=="X":
        win(btn2["text"])
    if b3==b6 and b3==b9 and b3=="O" or b3==b6 and b3==b9 and b3=="X":
        win(btn3["text"])
    if b1==b5 and b1==b9 and b1=="O" or b1==b5 and b1==b9 and b1=="X":
        win(btn1["text"])
    if b7==b5 and b7==b3 and b7=="O" or b7==b5 and b7==b3 and b7=="X":
        win(btn7["text"])
    if flag ==10:
        messagebox.showinfo("Match Tied", "Please try Again!!!!!")
        root.destroy()
def exit():
    root.destroy()
def win(player):
        playername = "Game complete " +player + " won!!!! "
        messagebox.showinfo("Congratulations", playername)
btn1 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked1)
btn1.grid(column=2, row=2)
btn2 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked2)
btn2.grid(column=3, row=2)
btn3 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked3)
btn3.grid(column=4, row=2)
btn4 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked4)
btn4.grid(column=2, row=3)
btn5 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked5)
btn5.grid(column=3, row=3)
btn6 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked6)
btn6.grid(column=4, row=3)
btn7 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked7)
btn7.grid(column=2, row=4)
btn8 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked8)
btn8.grid(column=3, row=4)
btn9 = tk.Button(root, text=" ",bg="white", fg="Black",width=3,height=1,command=clicked9)
btn9.grid(column=4, row=4)
btn11 = tk.Button(root,text="Exit",bg="white", fg="Black",command=exit)
btn11.grid(column=0, row=7)
root.mainloop()