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 further your knowledge of Tkinter, Python’s standard interface to the Tk GUI toolkit by building 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 fun love calculator from scratch!
Today, we will cover:
Today, we’ll develop a game that will calculate the love percentage between two input names. In this game, you just have to enter two names and answer some basic yes-or-no questions. In the end, you’ll generate the love percentage between these two people.
Let’s develop the same game using Python 3 and Tkinter!
Before diving deeper, let’s recap a few building blocks you’ll use across any Python Tkinter app:
Widgets you’ll touch often: Label (text and images), Entry (single-line input), Text (multiline), Button, Frame (structure), Canvas (drawing), Toplevel (extra window), and the modern ttk versions of most of these.
Three layout managers
pack: quickest for stacking small sections.
grid: best all-rounder for form-like layouts; supports row/column weights for responsiveness.
place: absolute positioning; use sparingly. In one container, stick to one manager. For complex pages, nest Frames and use grid.
State variables: Use StringVar/IntVar/BooleanVar for two-way binding between the UI and your game state:
name_var = tk.StringVar()tk.Entry(root, textvariable=name_var).grid(row=0, column=1)
You can react to changes with name_var.trace_add("write", handler).
Events and scheduling: .bind("<Return>", on_enter) for keyboard shortcuts; .after(ms, fn) to schedule updates, animations, or progress without freezing the UI.
These basics will keep your love calculator snappy and easier to extend.
When the user runs the program, they will be asked to enter two names to calculate a love percentage for. After the user enters the names, they will be asked five different yes-or-no to answer. Our program will use these answers to calculate the value of love percentage later in our program.
Now our program will calculate the love percentage based on the letters in the two names entered. Then, it will use the answer values to increase the love percentage. Now, after all the inputs are answered, our program will open the Tkinter window, which will display both the names separated by “LOVES”.
For example: If the user entered “Chandler” and “Monica” as two names, our program will display “Chandler Loves Monica”.
There will be a button under the second name. When the user presses the button, it will reveal the love percentage as an image. The user can then close the program and play again.
Let’s look at an example to understand how the program counts the love percentage by letters in the names:
The program will count the occurrence of all the characters according to their order. The more letters are shared in the name, the higher the rating.
For example: Take the names Chandler and Monica. Here, the first character is “C”. So our program will search through the list to find an other occurrence of “C”. If there is another “C”, it’ll increase the counter for “C” by 1. Our program will proceed until it reaches the end of the list.
So, how will it calculate the percentage? We will use the output from the previous calculations. Our program is going to take the first and last number and add it. If the addition is ≥10, then it will add them internally.
For example: A score of 14 letters will be .
Once it adds the first and last numbers, it will store the addition in the first place and it will delete the last element and this will go on until there are only two numbers left.
After we have the a value for love percentage, we will use the answer values to increase the percentage. If the current value is low, then we will increase it by 5 points. Keep in mind that you can modify your program as you wish by specifying various conditions.
Now that we have a sense of what the love calculator 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.
Implementing this game requires two parts:
tkinter: To add widgets in our application.simpleaudio: To play the sound files.Counter: To calculate the occurrence of each character.Here, we take two name inputs from user. The user must enter the name values After, we convert the inputs into upper case so that it aligns with our file names.
Here, we set the longer name as name1 and the shorter name as name2 so that it doesn’t distort our tkinter module. If the lengths of both names are equal, then we do not make this change.
Here, we take inputs from user for our questions. We’re going to use the result of these questions to calculate the love percentage later in our program. The user must enter yes or no.
Here, we calculate the final string by adding LOVES in between the two names.
For example: If our two name inputs are “Chandler” and “Monica”, then our final string will be “Chandler Loves Monica”. Then, we convert the string into list.
Here, we calculate the occurrence of each character in our list. As the result of this, list2 stores the number of occurrences for each characters.
We now calculate the value of the love percentage based on the two input names. We create an empty list called list 5. Our program is going to take the first and last numbers from the list and add them.
If the number is ≥10, it will add the digits internally to make a single digit number. Then we remove the last digit from our list. Now we insert the value of addition operation we performed at the 0th index.
List 5 will store the updated values of list 2 after removing the last number and inserting the addition value at 0th index. Then we update list 2 with list 5, so that this operation can continue on the new list.
This operation will continue until there are only 2 numbers remaining. List 5 should have only two numbers in it that. we use to calculate the percentage alongside the yes-or-no questions.
We now convert the list of two numbers to string. Then, we convert it to an integer so that we can further modify it based on the answer of the questions given by user.
We increase the love score based on the answers to the questions. We only increase the value if it’s less than 95. If the answer to the first question is “YES”, increase the value by 5.
If the value is less than 90, we increasing the value by 5 more. This process continues for 5 if-statements. The purpose of this code is to emphasize the structure of nested if-statements.
Keep in mind that there are many ways to increase the value for love percentage. You can use you creative imagination here!
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.root.configure( ): We use this to specify the background color for our application.root.resizable( ): Here we are using it to prevent the users from resizing our main window.root.iconphoto( ): We use it to set the icon in the title bar for our application window. We set the first parameter to True, so that all windows have the same icon.Then we add the found files that will play at various events. One thing to notice is that the program only accepts .wav files. We need to load the sound file as an object. Then we can play it using .play( ) method.
For a polished feel, make the window respond to resizing gracefully. With Python Tkinter’s grid, give rows/columns “weight” so empty space stretches where you want:
root.grid_columnconfigure(c, weight=1, uniform="cols")
for r in range(6):
root.grid_rowconfigure(r, weight=0)
root.grid_rowconfigure(5, weight=1) # let the bottom area expand
Place your Frames and Labels using sticky="nsew" so they grow into their cells. Nesting Frames lets you switch entire sections on/off without recalculating pixel positions.
1.png2.png3.pngWe display each letter as a label, so we need to make the list. We use a loop to display the images for each letter, so we need to find the length of each name. Notice that here we convert the “LOVES” string to a list and find its length.
Here, we load the images for each of the letters for name1, name2 and loves. Then we display the label images for name1 and then add a blank space in the next row. Notice that our loop will run for each character. As it runs, it will increase the column value by 1 so that all the letters are placed side-by-side. We do the same for name2 and loves.
Here, we add a button that will trigger the Reveal( ) function. Then we set the button in the center of the window by using columnspan. We then add a blank space and a label that will show the result when the user presses the button.
When the user presses the button, this function will be triggered. Here, we show the image label that will display the love percentage. If the user presses the button again, it uses the close( ) function to close our application. The close( ) function will play a sound file before closing the window.
We have to enter the main loop to run the program. Our program will remain in the main loop until we press the close button.
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 learned 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