This article is part of a Python Tkinter series written by Pratik Shukla. Check out his Medium account to read his other articles. And stay tuned for more Tkinker articles from Pratik!
This series aims to introduce you to Tkinter, Python’s standard interface to the Tk GUI toolkit, and help you build interactive games you can add to your resume. To get up to speed on Tkinter, go read our first article in this series Python Tkinter Tutorial: build a number guessing game. Once you get down the basics, come back here to build a jumbled words game from scratch!
Today, we will cover:
In this article, we’ll develop a cool game in which our program will generate jumbled words from the list of words provided. The user has to guess the actual word from the jumbled word. Our program will also count the amount of time taken to guess the correct word. Here we will use Tkinter module of python to use different widgets like entry, label, and button.
When the user runs the program, the user will see a jumbled word on the Tkinter window. Now the user has to guess the actual word from the jumbled word provided and enter their guess in the entry widget. Now, to check if the guessed word was correct, the user has to press the check
button.
If the guessed word is correct, the label will transform its image to Win
and the correct word will be shown to user. Our program will also show us the time taken by the user to guess the correct word.
If the user’s guess is wrong, then label will show a Lose
image, and the entry widget will be cleared for to enter the word again. Our program will not show the correct word or time taken if the user’s guess is wrong.
If the user wants to play the game again, they wait for 3 seconds, and the new window will open with new word. If the user wants to terminate the game, they have to press the close
button on the window. Our program will also play different sound files on various events.
Now that we have a sense of what the jumbled words game will entail, let’s walk step-by-step through the process in Python. If you don’t yet know how to set up Tkinter, check out our first article in this series for a brief tutorial.
#Import required libraries from tkinter import * import simpleaudio as sa import random import tkinter.font as font import time
tkinter
: To add widgets in our applicationsimpleaudio
: To play the sound filesrandom
: To select a random wordtkinter.font
: To change the properties of fonttime
: To calculate the time taken by userrunning = True while running==True: #Calculate starting time start_time = time.time() root = Tk() root.geometry('+100+0') root.configure(bg="#000000") root.iconphoto(True,PhotoImage(file="Surprise.png")) root.title("JUMBLE WORDS") root.resizable(width=False,height=False)
running
and set its value to True
. Our program will generate a new window when the variable is set to True
. When the user presses the main close button in title bar, it will exit the loop.root = Tk( )
: This is used to initialize our tkinter module.root.title( )
: We use it to set the title for our application.root.geometry( )
: We use this to specify at which location our application window will open. In our case, it will open at 100 points to the left and 0 points to the down from the top-left corner.root.configure( )
: We use this to specify the background color for our application. In our case the background color will be black.root.resizable( )
: Here we are using it to prevent the users from resizing our main window.root.iconphoto( )
: We are using it to set the icon in the title bar for our application window. We are setting the first parameter to True
, so that will ensure that all the other windows created from this application has the same icon.start time
, which stores the time value when the window was opened.#Load sound files start = sa.WaveObject.from_wave_file("Start.wav") one = sa.WaveObject.from_wave_file("Win.wav") two = sa.WaveObject.from_wave_file("Lose.wav") three = sa.WaveObject.from_wave_file("6.wav") start.play()
Now we will use some sound files that will play at various events. When our program starts it will play start
file. We’ll play the other three files when the user’s guess is correct, when the user’s guess is wrong and when user closes the application respectively.
One thing to notice is that it only accepts .wav
files. First of all we need to load the sound file as an object. Then we can play it using .play( )
method when required.
#select a word from list def random_word(): words=["RAIN","CLOUD","NATURE","BEAUTIFUL","HOUSE"] pick = random.choice(words) return pick
This function will select a word randomly from the list of words provided. It can contain as many words as you want. The random.choice( )
method randomly selects a word from of list of word. Then we’ll return that word to use it in other functions.
# Jumble the selected word def jumbled_word(word): word = ranom.sample(word,len(word)) word_jumbled = "".join(word) return word_jumbled
Now that our program has selected a word, we need to jumble it for our game. This function takes into input the word selected by our previous function. The random.sample( )
function takes a word, jumbles it, and gives list of letters. We can specify the length of the word, but in our case, it’ll be same as the original word. Then we need to join the jumbled letters to make a string. Finally, it will return the jumbled word so that we can use it in next function.
# Randmoly selected word pick = random_word() # Jumbled word jumbled = jumbled_word(pick) # Get the letters from the jumbled word list1 = list(jumbled) len_list1 = len(list1) # Get the Images for our letters for i in range(len_list1): list1[i] = PhotoImage(file=str(list1[i])+str("_P.png")) row_0 = 0 col_0 = 0 # Arrange the letters on the main window for i in range(len_list1): B = Label(root,image=list1[i]) B.grid(row=row_0,column=col_0) col_0 = col_0 + 1
pick
variable.jumbled
variable.List1
will convert our string into a list of letters. len_list1
gives us the length of the word.A_P.png
, B_P.png
, C_P.png
, and so on.# Blank space for row_1 root.gird_rowconfigure(1, minsize=10) # Modify font myFont = font.Font(family='Calibri', weight='bold') # For label image your_choice = PhotoImage(file"YOUR_GUESS.png") surprise = PhotoImage(file="suprprise.png") win = PhotoImage(file="WINNN.png") lose = PhotoImage(file="LOSEEE.png") check = PhotoImage(file="CHECKK.png") close = PhotoImage(file="CLOSEE.png")
main label
, win
, lose
, check
, close
, etc. So, here we are loading them into variables to use it later.#Entry label label = Label(root,image=your_choice) label.grid(row=2,column=0,columnspan=len_list1) label["font"] = myFont #Add blank space root.grid_rowconfigure(3, minsize=10) #Add entry widget e1 = Entry(root,bd=5,bg="#9ca1db",justify=CENTER,font=myFont,fg="#000000") e1.grid(row=4,column=0,columnspan=len_list1) #Add blank space root.grid_rowconfigure(5, minsize=10)
columnspan
to make that into center.#Check button button =Button(root,image=check,command=lambda:result()) button.grid(row=6,column=0) #Close button Btn = Button(root,image=close,command=lambda:reset()) Btn.grid(row=6,column=len_list1-1) #Add blank space root.grid_rowconfigure(7, minsize=10)
Now, we need to add the check button and close button. Pressing these buttons will trigger the result( )
and reset( )
functions respectively. The Check
button will be on the first column, and the close button will be on the last column.
#Label that will display result label2 = Label(root,image=surprise) label2.grid(row=8,column=0,columnspan=len_list1) root.grid_rowconfigure(9, minsize=10) #Modify the fonts myFont = font.Font(family='Comic Sans MS',weight='bold') #Label to show time taken label3= Label(root,text="TIME : ",width=12,bg="#f5ab55",justify=CENTER,font=myFont,fg="#000000",relief=RAISED) label3.grid(row=12,column=0,columnspan=len_list1)
time
label will display the time taken by the user to guess the correct answer.#Function to check whether the user's guess is correct or not def result(): #Get the entry value in upper case answer = (e1.get()).upper() if answer == pick: #Caculate the time consumed time_taken = time.time() - start_time time_taken = int(time_taken) #Change the label label3.configure(text="TIME : "+str(time_taken)+" Sec") #Play win sound one.play() #Change label image to win label2.configure(image=win) #Showing original word col_2=0 row_2=10 #Display the origianl word for i in range(len_list1): B = Label(root,image=list2[i]) B.grid(row=row_2,column=col_2) col_2 = col_2+1 #Add blank space root.grid_rowconfigure(11, minsize=10) #To play again after 3 sec root.update_idletasks() root.after(3000) root.destroy() else: #Play a sound file two.play() #Change the label label2.configure(image=lose) #Change back to original label image root.update_idletasks() root.after(500) label2.configure(image=surprise) #Clear the entry e1.delete(0,"end")
WIN
image. Now we will display the image label for the actual word. Our program will wait for 3 seconds, close the current window, and open a new one.lose
image, and the entry widget will be cleared for user to reenter the word. After 0.5 seconds, the Lose
label will go back to its original image.Reset( )
function and enter the main loopdef reset(): #Play a sound file : three.play() #Change the running value to false : global running running=False #CLose the main window : root.destroy() #Enter the main loop : root.mainloop()
reset( )
function will trigger. It will set the running variable to False
so that our program can’t open a new window.Take a look at the complete code for this Python Tkinter game.
Want to download the code and images? You can download all the necessary images and code here.
#Import required libraries : from tkinter import * import simpleaudio as sa import random import tkinter.font as font import time running = True while running==True: #Calculate starting time : start_time = time.time() root = Tk() root.geometry('+100+0') root.configure(bg="#000000") root.iconphoto(True,PhotoImage(file="Surprise.png")) root.title("JUMBLE WORDS") root.resizable(width=False,height=False) #Load sound files : start = sa.WaveObject.from_wave_file("Start.wav") one = sa.WaveObject.from_wave_file("Win.wav") two = sa.WaveObject.from_wave_file("Lose.wav") three = sa.WaveObject.from_wave_file("6.wav") start.play() #Select a word from list : def random_word(): words=["RAIN","CLOUD","NATURE","BEAUTIFUL","HOUSE"] pick = random.choice(words) return pick #Jumble the selected word : def jumbled_word(word): word = random.sample(word,len(word)) word_jumbled = "".join(word) return word_jumbled #Randomly selected word : pick = random_word() #Jumbled word : jumbled = jumbled_word(pick) #Get the letters from jumbled word : list1 = list(jumbled) len_list1 = len(list1) #Get the PhotoImages for our letters : for i in range(len_list1): list1[i] = PhotoImage(file=str(list1[i])+str("_P.png")) row_0 = 0 col_0 = 0 #Arrange the letters on the main window : for i in range(len_list1): B = Label(root,image=list1[i]) B.grid(row=row_0,column=col_0) col_0 = col_0 + 1 #Blank space for row_1 : root.grid_rowconfigure(1, minsize=10) #Modify font : myFont = font.Font(family='Calibri', weight='bold') #For label image : your_choice = PhotoImage(file="YOUR_GUESS.png") surprise = PhotoImage(file="surprise.png") win = PhotoImage(file="WINNN.png") lose = PhotoImage(file="LOSEEE.png") check = PhotoImage(file="CHECKK.png") close = PhotoImage(file="CLOSEE.png") #To arrange the labels in center : #x = math.floor(len_list1/2) #Entry label : label = Label(root,image=your_choice) label.grid(row=2,column=0,columnspan=len_list1) label["font"] = myFont #Add blank space : root.grid_rowconfigure(3, minsize=10) #Add entry widget : e1 = Entry(root,bd=5,bg="#9ca1db",justify=CENTER,font=myFont,fg="#000000") e1.grid(row=4,column=0,columnspan=len_list1) #Add blank space : root.grid_rowconfigure(5, minsize=10) #Get the entry value in upper case : answer = (e1.get()).upper() #Make list of correct word : list2=list(pick) #Load images for correct word : for j in range(len(list2)): list2[j] = PhotoImage(file=str(list2[j])+str("_P.png")) #Check button : button =Button(root,image=check,command=lambda:result()) button.grid(row=6,column=0) #Close button : Btn = Button(root,image=close,command=lambda:reset()) Btn.grid(row=6,column=len_list1-1) #Add blank space : root.grid_rowconfigure(7, minsize=10) #Label that will display result : label2 = Label(root,image=surprise) label2.grid(row=8,column=0,columnspan=len_list1) root.grid_rowconfigure(9, minsize=10) #Modify the fonts : myFont = font.Font(family='Comic Sans MS',weight='bold') #Label to show time taken : label3= Label(root,text="TIME : ",width=12,bg="#f5ab55",justify=CENTER,font=myFont,fg="#000000",relief=RAISED) label3.grid(row=12,column=0,columnspan=len_list1) #Function to check whether the user's guess is correct or not : def result(): #Get the entry value in upper case : answer = (e1.get()).upper() if answer == pick: #Caculate the time consumed : time_taken = time.time() - start_time time_taken = int(time_taken) #Change the label : label3.configure(text="TIME : "+str(time_taken)+" Sec") #Play win sound : one.play() #Change label image to win : label2.configure(image=win) #Showing original word : col_2=0 row_2=10 #Display the origianl word : for i in range(len_list1): B = Label(root,image=list2[i]) B.grid(row=row_2,column=col_2) col_2 = col_2+1 #Add blank space : root.grid_rowconfigure(11, minsize=10) #To play again after 3 sec : root.update_idletasks() root.after(3000) root.destroy() else: #Play a sound file : two.play() #Change the label : label2.configure(image=lose) #Change back to original label image : root.update_idletasks() root.after(500) label2.configure(image=surprise) #Clear the entry : e1.delete(0,"end") #Function that triggers by pressing CLOSE button : def reset(): #Play a sound file : three.play() #Change the running value to false : global running running=False #CLose the main window : root.destroy() #Enter the main loop : root.mainloop()
Congrats! You’ve reached the end and built a successful Python Tkinter game. Great work applying your Python knowledge. I hope you enjoyed this article and learnt something new from it. Python is such an intuitive programming language that the easiest way to master it is through hands-on practice like this. Keep up the work and keep building!
In this series on Tkinter, we’ll be developing a total of Five games using Tkinter. So, hang tight! If you have any doubts, questions, or thoughts regarding this article, feel free to contact me at shuklapratik22@gmail.com
Join a community of more than 1 million readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.