Color Game with GUI in Python using Tkinter
Prerequisite for Color Game with GUI in Python using Tkinter:
- Basic Knowledge about Tkinter
- Basic knowledge about Oops in Python
Working: Color Game
- In this game, the player will answer the name of the color of the displayed word.
- After every correct answer, the score will be incremented by 1.
- Every time word will change after submitting the answer.
- The player has 30 sec of time.
- Colors used in the Game are Red, Green, Blue, Brown, Yellow, Orange, Pink, Black, Purple.
Let’s try to make the Color game with GUI in python using Tkinter.
Let’s start with the code:
Build a Color Game with GUI in Python using Tkinter
Importing Libraries
from tkinter import * import random from tkinter import messagebox import time
Colors in a list:
color=["red","yellow","brown","blue","orange","purple","pink","black","green"]
__init__ is a constructor of the class Color
class Color: def __init__(self,master): self.count=0 self.col=random.choice(color) self.timevar=31 self.structure(master) self.countdown() self.rr=master
- Color is the name of class.
- master is the Tk root widget.
- count represents the score.
- timevar represents the remaining time.
structure() is also a member function of the Color class, it defines the structure of GUI. GUI contains Tkinter widgets like Entry, Label, Button, Messagebox.
def structure(self,master): """ Instruction Label """ # Create instruction label for Program self.inst_lbl = Label(master, text = "Welcome to Color Game!") self.inst_lbl.grid(row =0,column =0, columnspan = 2,sticky = W) # Create a space self.gap2_lbl1 = Label(master, text = " ") self.gap2_lbl1.grid(row = 1, column = 0, sticky = W) #Create label to display Score self.score = Label(master, text ="Score: "+str(self.count)) self.score.grid(row = 1, column = 0, sticky = W) # Create label for Display color with word self.display_lbl = Label(master,text =random.choice(color), fg=self.col,font=("Courier", 30)) self.display_lbl.grid(row = 2, column = 0, sticky = W) # Create label for Display timer self.timer = Label(master, text ="") self.timer.grid(row = 3, column = 1, columnspan = 2) # Create entry widget to accept Color Name self.col_ent = Entry(master) self.col_ent.grid(row = 4, column =0, sticky = W) self.col_ent.focus() # Create a space self.gap2_lbl1 = Label(master, text = " ") self.gap2_lbl1.grid(row = 5, column = 0, sticky = W) # Creating a submit button self.submit_bttn=Button(master,text="Submit",command=self.submit) self.submit_bttn.grid(row = 6, column =0,sticky = W) master.bind('<Return>',self.submit) """ RESET """ # Creating a reset button self.reset_bttn = Button(master,text ="Reset",command=self.reset) self.reset_bttn.grid(row = 6,column=2,sticky=W)
Image of GUI:
def changecolor(self): self.col=random.choice(color) self.display_lbl.config(text=random.choice(color),fg=self.col) self.col_ent.delete(0, 'end')
changecolor is also the member function of the class Color. This method changing the color the word and updating the text of the label display_lbl
def reset(self): self.col_ent.delete(0, 'end')
reset is also the member function of the class Color and used to clear the Entry widget that is col_ent.
def countdown(self): if self.timevar > 0: self.timevar -= 1 self.timer.config(text = "Time left: "+ str(self.timevar)) self.timer.after(1000, self.countdown) else: messagebox.showinfo("Hello", "Your Score "+str(self.count)) self.rr.destroy()
The countdown is also the member function of the class Color. It is updating the timevar after every seconds. If the timevar (remaining time) becomes zero the window will close.
def submit(self,*args): if(self.col_ent.get()==self.col): self.count+=1 self.score.config(text ="Score: "+str(self.count)) self.changecolor() self.col_ent.focus()
- The submit is also the member function of the class Color.
- It is called when the submit button is clicked.
- It increments the score by one when the answer is correct.
root = Tk() root.title("Color Game") root.geometry("300x250") app = Color(root) root.mainloop()
This piece of code involves, Creation of the Tk root widget. Creation of Object of Class Color.
Leave a Reply