Tic-Tac-Toe GUI Using Tkinter
Tic-Tac-Toe GUI in Python using Tkinter
This tutorial will help you play and Create Tic-Tac-Toe Application, a very renowned game we have all got our hands on since our childhood.
The Complete program looks like so:
EXAMPLE :-
from tkinter import *from tkinter import messageboxfrom random import randintActivePlayer=1p1=[]p2=[]Count=0Score0=0ScoreX=0root=Tk()root.title("Tic Tac Toe")root.config(bg='light green')label1=Label(root,text='Tic Tac Toe',font="Times 15 bold",bg='cadet blue',fg='black',bd=4,height=2,width=10,relief=RAISED,)label1.grid(row=0,column=1,padx=5,pady=5,sticky='nsnew')label2=Label(root,text=f'SCORE-->PLAYER 1: {Score0}\nSCORE--> PLAYER 2: {ScoreX}',font="Times 15 bold",bg='cadet blue',fg='black',bd=4,height=1,width=10,relief=RAISED,)label2.grid(row=4,column=0,padx=5,pady=5,sticky='nsnew')label3=Label(root,text='PLAYER 1',font="Times 15 bold",bg='cadet blue',fg='black',bd=4,height=2,width=10,relief=RAISED,)label3.grid(row=0,column=0,padx=5,pady=5,sticky='nsnew')label4=Label(root,text='PLAYER 2',font="Times 15 bold",bg='white',fg='black',bd=4,height=2,width=10,relief=RAISED,)b1=Button(root,text='',font="Times 35 bold",bg='white',fg='black',width=12)b1.grid(row=1,column=0,padx=5,pady=5,sticky='snew')b1.config(command=lambda:ButtonClick(1))b2=Button(root,text='',font="Times 35 bold",bg='white',fg='black',width=12)b2.grid(row=1,column=1,padx=5,pady=5,sticky='snew')b2.config(command=lambda:ButtonClick(2))b3=Button(root,text='',font="Times 35 bold",bg='white',fg='black',width=12)b3.grid(row=1,column=2,padx=5,pady=5,sticky='snew')b3.config(command=lambda:ButtonClick(3))b4=Button(root,text='',font="Times 35 bold",bg='white',fg='black',width=12)b4.grid(row=2,column=0,padx=5,pady=5,sticky='snew')b4.config(command=lambda:ButtonClick(4))b5=Button(root,text='',font="Times 35 bold",bg='white',fg='black',width=12)b5.grid(row=2,column=1,padx=5,pady=5,sticky='snew')b5.config(command=lambda:ButtonClick(5))b6=Button(root,text='',font="Times 35 bold",bg='white',fg='black',width=12)b6.grid(row=2,column=2,padx=5,pady=5,sticky='snew')b6.config(command=lambda:ButtonClick(6))b7=Button(root,text='',font="Times 35 bold",bg='white',fg='black',width=12)b7.grid(row=3,column=0,padx=5,pady=5,sticky='snew')b7.config(command=lambda:ButtonClick(7))b8=Button(root,text='',font="Times 35 bold",bg='white',fg='black',width=12)b8.grid(row=3,column=1,padx=5,pady=5,sticky='snew')b8.config(command=lambda:ButtonClick(8))b9=Button(root,text='',font="Times 35 bold",bg='white',fg='black',width=12)b9.grid(row=3,column=2,padx=5,pady=5,sticky='snew')b9.config(command=lambda:ButtonClick(9))b10Photo = PhotoImage(file='Restart and enter.png')b10=Button(root,font="Times 15 bold",image=b10Photo ,bg='white',fg='white',height=2,width=10)b10.grid(row=4,column=1,padx=5,pady=5,sticky='snew')b10.config(command=lambda:Restart())b11=Button(root,font="Times 15 bold",text='END',bg='cadet blue',fg='white',height=2,width=10)b11.grid(row=4,column=2,padx=5,pady=5,sticky='snew')b11.config(command=lambda:game_terminate())def game_terminate():root.destroy()def Restart():global label3global label4global ActivePlayerActivePlayer=1global CountCount=0label3.destroy()label4.destroy()root.title("Tic Tac Toe")label3=Label(root,text='PLAYER 1',font="Times 15 bold",bg='cadet blue',fg='black',bd=4,height=2,width=10,relief=RAISED,)label3.grid(row=0,column=0,padx=5,pady=5,sticky='nsnew')b1['state']='active'b1['text']=''b1['bg']='white'b2['state']='active'b2['text']=''b2['bg']='white'b3['state']='active'b3['text']=''b3['bg']='white'b4['state']='active'b4['text']=''b4['bg']='white'b5['state']='active'b5['text']=''b5['bg']='white'b6['state']='active'b6['text']=''b6['bg']='white'b7['state']='active'b7['text']=''b7['bg']='white'b8['state']='active'b8['text']=''b8['bg']='white'b9['state']='active'b9['text']=''b9['bg']='white'p1.clear()p2.clear()def game_ended():global CountCount=0b1['state']='disable'b1['text']=''b2['state']='disable'b2['text']=''b3['state']='disable'b3['text']=''b4['state']='disable'b4['text']=''b5['state']='disable'b5['text']=''b6['state']='disable'b6['text']=''b7['state']='disable'b7['text']=''b8['state']='disable'b8['text']=''b9['state']='disable'b9['text']=''p1.clear()p2.clear()def ButtonClick(id):global label3global label4global ActivePlayerglobal p15global p2if ActivePlayer==1:label3.destroy()SetLayout(id,"0","cadet blue")p1.append(id)label4=Label(root,text='PLAYER 2',font="Times 15 bold",bg='white',fg='black',bd=4,height=2,width=10,relief=RAISED,)label4.grid(row=0,column=2,padx=5,pady=5,sticky='nsnew')root.title("Tic Tac Toe ")ActivePlayer=2print(f"P1:{p1}")else:label4.destroy()SetLayout(id,"X","white")p2.append(id)label3=Label(root,text='PLAYER 1',font="Times 15 bold",bg='cadet blue',fg='black',bd=4,height=2,width=10,relief=RAISED,)label3.grid(row=0,column=0,padx=5,pady=5,sticky='nsnew')root.title("Tic Tac Toe")ActivePlayer=1print(f"P2:{p2}")if Count==5 or Count>5:CheckWiner()def SetLayout(id,PlayerSymbol,colr):global Countif id==1:b1['text']=PlayerSymbolb1['state']='disabled'b1['bg']=colrb1['fg']='black'Count = Count+1elif id==2:b2['text']=PlayerSymbolb2['state']='disable'b2['bg']=colrCount= Count+1elif id==3:b3['text']=PlayerSymbolb3['state']='disable'b3['bg']=colrCount= Count+1elif id==4:b4['text']=PlayerSymbolb4['state']='disable'b4['bg']=colrCount=Count+1elif id==5:b5['text']=PlayerSymbolb5['state']='disable'b5['bg']=colrCount= Count+1elif id==6:b6['text']=PlayerSymbolb6['state']='disable'b6['bg']=colrCount= Count+1elif id==7:b7['text']=PlayerSymbolb7['state']='disable'b7['bg']=colrCount= Count+1elif id==8:b8['text']=PlayerSymbolb8['state']='disable'b8['bg']=colrCount= Count+1elif id==9:b9['text']=PlayerSymbolb9['state']='disable'b9['bg']=colrCount= Count+1def AssignSeperateColorToWiner(id):if id=='R1':b1['bg']='light green'b2['bg']='light green'b3['bg']='light green'if id=='R2':b4['bg']='light green'b5['bg']='light green'b6['bg']='light green'if id=='R3':b7['bg']='light green'b8['bg']='light green'b9['bg']='light green'if id=='C1':b1['bg']='light green'b4['bg']='light green'b7['bg']='light green'if id=='C2':b2['bg']='light green'b5['bg']='light green'b8['bg']='light green'if id=='C3':b3['bg']='light green'b6['bg']='light green'b9['bg']='light green'if id=='D1':b1['bg']='light green'b5['bg']='light green'b9['bg']='light green'if id=='D2':b3['bg']='light green'b5['bg']='light green'b7['bg']='light green'def CheckWiner():global ScoreXglobal ScoreOWiner=-1if ( (1 in p1) and (2 in p1) and (3 in p1) ):Winer=1AssignSeperateColorToWiner('R1')if ( (1 in p2) and (2 in p2) and (3 in p2) ):Winer=2AssignSeperateColorToWiner('R1')if ( (4 in p1) and (5 in p1) and (6 in p1) ):Winer=1AssignSeperateColorToWiner('R2')if ( (4 in p2) and (5 in p2) and (6 in p2) ):Winer=2AssignSeperateColorToWiner('R2')if ( (7 in p1) and (8 in p1) and (9 in p1) ):Winer=1AssignSeperateColorToWiner('R3')if ( (7 in p2) and (8 in p2) and (9 in p2) ):Winer=2AssignSeperateColorToWiner('R3')if ( (1 in p1) and (4 in p1) and (7 in p1) ):Winer=1AssignSeperateColorToWiner('C1')if ( (1 in p2) and (4 in p2) and (7 in p2) ):Winer=2AssignSeperateColorToWiner('C1')if ( (2 in p1) and (5 in p1) and (8 in p1) ):Winer=1AssignSeperateColorToWiner('C2')if ( (2 in p2) and (5 in p2) and (8 in p2) ):Winer=2AssignSeperateColorToWiner('C2')if ( (3 in p1) and (6 in p1) and (9 in p1) ):Winer=1AssignSeperateColorToWiner('C3')if ( (3 in p2) and (6 in p2) and (9 in p2) ):Winer=2AssignSeperateColorToWiner('C3')if ( (1 in p1) and (5 in p1) and (9 in p1) ):Winer=1AssignSeperateColorToWiner('D1')if ( (1 in p2) and (5 in p2) and (9 in p2) ):Winer=2AssignSeperateColorToWiner('D1')if ( (3 in p1) and (5 in p1) and (7 in p1) ):Winer=1AssignSeperateColorToWiner('D2')if ( (3 in p2) and (5 in p2) and (7 in p2) ):Winer=2AssignSeperateColorToWiner('D2')if Winer==1:messagebox.showinfo(title="Congrat.",message="Player 1 is the winner")ScoreX+=1label2['text']=f'SCORE-->PLAYER 1: {Score0}\n-->PLAYER 2: {ScoreX}'game_ended()elif Winer==2:messagebox.showinfo(title="Congrat.",message="Player 2 is the winner")ScoreO+=1label2['text']=f'SCORE-->PLAYER 1: {Score0}\n-->PLAYER 2: {ScoreX}'game_ended()elif Winer==-1 and Count==9:messagebox.showinfo(title="Oops",message="It's a Tie!!")game_ended()for i in range(1,4):root.grid_rowconfigure(i,weight=1)for i in range(3):root.grid_columnconfigure(i,weight=1)def enter_function(event):b10.invoke()root.bind('<Return>',enter_function)root.mainloop()#**********************CREATED BY CODING BLOCKS ************************#
OUTPUT
No comments:
Post a Comment
Please do not any spam link in Comment Box