guess_the_anime

Guess The Anime

Python Tkinter Tutorial: Guess The Anime GUI

How good are you at guessing the Anime Character dialogues? I created a GUI application where you need to play the dialogue and guess the anime and the Character’s name.

Anime Vyuh is a website that motivates Weeb to Code, with the easiest tutorials provided to them. You can check the Python Tutorials and start creating Desktop applications using Tkinter.

Introduction To Tkinter

Tkinter is a Python module that helps you create amazing GUI-based applications. Moreover, it provides a wide range of widgets that help you create your first application. The widgets include Label, Button, Frame, Entry, ScrollBar, Radio buttons, and many more. Here is one of the best ways to get started with Tkinter, learn by building projects.

After you import the module, the first thing is to initialize Tkinter.

root = tk.Tk()

Then you need to configure the GUI window. By using the root. geometry method, you can set up the dimensions of your application. And resizable(False, False) will make your GUI have a fixed size. And you can also provide the title for your GUI using root.title(“Title Name”).

root.geometry("475x425") #width:475, height:425 
root.resizable(False,False)
root.title("Tkinter App")

Since applications are continuously running, you need to use root.mainloop(). This command will make sure your GUI is running, and it will terminate when you’re close the GUI window.

A Demo of Tkinter GUI application:

import tkinter as tk
root = tk.Tk()
root.geometry("355x300")
root.title("Demo")
content = tk.Label(root,text="Anime Vyuh")
content.pack()
root.mainloop()

Tkinter Widgets

As mentioned already, Tkinter is building with many widgets. We will look into 3 commonly used Tkinter Widgets. And yes to build the Guess The Anime GUI, these three widgets are the building blocks. Make sure you have the Tkinter module installed. Check this article which contains the command to download it.

And here is Python Tutorials Series to check out for reference.

Tkinter Widgets Required To Build Guess the Anime Game

Label

A Label is a Tkinter Widget that is used to display text or content on a GUI application.

Syntax:
name=tk.Label(root,text="Any Name")
name.pack()
Example: You can look into Demo Application Example, as it uses Label

Pack a geometry manager, and you can only display the widget on the GUI application using this Geometry manager. You will look into it next. The label which we just used had a default font and size. And yes we can change the font and size.

Note: 
Make sure you enter the example programs within root.geometry and root.mainloop() as I won't be using them in examples.
content1 = tk.Label(text="Anime Vyuh",font=("arial",18,"bold")).pack()
content2 = tk.Label(text="Fusion Of Coding And Anime",font=("times new roman",14,"italic")).pack()

The order of font should be font=(“font”,size,”weight”).

One thing noticed above is you can write .pack() in the same line. One issue of using pack() in one-liner is that you can’t further modify or configure the label widget. Using content2.configure(text=”new”) you can modify the data of content 2 from Fusion Of Coding And Anime to new. But as you used to pack in one line, content2 will return None Value, therefore there is modification done in content2. You should carefully use one-liners in the Tkinter App.

Button

The button is a widget that is used to trigger the events.

content1 = tk.Label(text="Anime Vyuh",font=("arial",16,"bold"))
content1.pack()
btn = tk.Button(text="Click Me",font=("arial",12,"bold")).pack()

What we just created is plain text, and nothing happened after we clicked the button. The command argument is used to add some event trigger after the button click.

Syntax to use command argument:
tk.Button(text=anything, command=function_name)
def trigger():
    content1.configure(text="Anime Vyuh Welcomes You")

content1 = tk.Label(text="Anime Vyuh")
content1.pack()
btn = tk.Button(text="Click Me",font=("arial",12,"bold"),command=trigger)
btn.pack()

Geometry Management In Tkinter

Geometry managers are important in how you place the widgets on the application. In this article, I won’t be able to share complete Tkinter tutorials, but very soon you might expect 3 Part Tutorial Series On Tkinter.

pack()

the pack places the order of the widgets by order, it follows the first comes first placed rule. You can start from any position, say top, right, left, bottom. You can specify the position by placing a side argument inside the pack().

content1 = tk.Label(text="Anime Vyuh",font=("arial",16,"bold"))
content1.pack(side="left")

By default, the pack places the widgets from the top but here we changed the position to the left.

place()

place () takes x and y coordinates and places the widgets based on the coordinates provided. Using place() you can place your widget anywhere within the GUI.

content1 = tk.Label(text="Anime Vyuh",font=("arial",16,"bold"))
content1.place(x=120,y=140)

Using place you even modify the dimension of the specific widget. This can be done by providing width and height arguments.

click_btn = tk.Button(text="Submit")
click_btn.place(x=20,y=20,width=150,height=150)

grid()

grid() is like an excel cell. Here the widget is placed based on rows and columns.

content1 = tk.Label(text="Anime")
content1.grid(row=1, column=1)
content2 = tk.Label(text="Vyuh")
content2.grid(row=1, column=2)

There are various methods of using the grid, and that will be covered while discussing Tkinter Tutorials Series.

Entry

Entry is a widget that creates a narrow box that comes in handy to take user inputs. There is the concept of Tkinter Variables, i.e., IntVar, StringVar which will be covered in another article that will focus more on Tkinter. So right now think of them as the variables.

name = tk.StringVar()
name_entry = tk.Entry(textvariable = name, bd=4)
name_entry.pack()

bd stands for border.

In the above code, we are taking the name as a String Variable, which is self-explanatory. And using Entry we shall set the initial values and get the input that the user entered.

get and set methods in Entry

The set method is used to add content to the Entry Box through Tkinter variables, this is a kind of placeholder. Providing initial value with the set, the user can understand what he is supposed to enter. Whereas the get method is used to retrieve the information which is entered inside the entry box.

Example:

name.set("Enter Name")
#notice name is Tkinter Variable

Set is adding the placeholder Enter Name, keep in mind that set takes one argument. Consider user enters: Luffy. The entry box doesn’t support the set method, but we can use Tkinter Variable to perform the task.

name_entry.get() 

Now you will receive the name Luffy using the get method. You can use the button to see the get method in action.

Adding Colors In Tkinter: fg And bg

You can even add color and background color to the width. fg stands for foreground and b stands for background. fg is used to add color to the text, whereas bg add color to the background of the widget.

Syntax:
lbl = tk.Label(text="anything",fg="<color>",bg="<color>")
#where <color> can be red, blue, yellow, grey, purple etc..
Example:
lbl = tk.Label(text="Anime Vyuh",fg="white",bg="black").pack()
btn = tk.Button(text="Submit",fg="green",bg="yellow").pack()
enter = tk.Entry(bg="grey",fg="white").pack()

Practice

Two Small Code To Apply what you learned so far. Follow the comments to understand the code flow.

Code 1: Increment Count On Button Click

import tkinter as tk

root = tk.Tk()
root.geometry("355x300")
root.title("Count")

def click():
    global count
    count+=1
    #update the count in label
    lbl.configure(text=count)

count = 0
#set count as the text label
lbl = tk.Label(text=count,font=("arial",16,"bold"))
lbl.pack()
#create a button, on every click increment count by 1
btn = tk.Button(text="Increment",command = click)
btn.pack()

root.mainloop()

Code 2: Simple Form

import tkinter as tk

root = tk.Tk()
root.geometry("355x300")
root.title("Count")

def submit():
    #get entered name and age
    name_get = name_entry.get()
    age_get = age_entry.get()
    lbl.configure(text="Details:\nName:{}\nAge:{}".format(name_get,age_get))


#create one int and string variable
age = tk.IntVar()
name = tk.StringVar()

#create two entry box
age_entry = tk.Entry(textvariable=age,font=("times now roman",12,"bold"))
age.set("Enter your age")
age_entry.place(x=40,y=40)
name_entry = tk.Entry(textvariable=name,font=("times now roman",12,"bold"))
name.set("Enter your name")
name_entry.place(x=40,y=100)


#create a button, and get the submitted details
submit_btn = tk.Button(text="Submit",command = submit,bg="black",fg="white")
submit_btn.place(x=50,y=160)

lbl = tk.Label(text="Details")
lbl.place(x=0,y=200)

root.mainloop()

You can check the documentation for more.

Guess The Anime And The Character Game Using Python Tkinter

Assuming you have a basic understanding of how the code flow of Tkinter works. Let us get down to the main business. In the Guess The Anime GUI game we shall take 10 different dialogues from popular Anime Characters.

tkinter

First, create a button that will play the dialogues. This button will trigger the pygame.mixer which will load and play the sound. In recognition of the dialogue, the user is supposed to guess the Anime Name and the Character Name.

messagebox

You can make use of the 2 Entry Box buttons. And then create another button to check the result. Using the messagebox, you can open a pop-up window that provides feedback to the user. You can check this article which talks on Tkinter MessageBox.

Provide feedback using the messagebox when the user leaves an empty Entry Box or When he doesn’t play the dialogue. Furthermore, provide one success feedback when the user answers properly. And provide an error message when his answer is wrong. And, yes increment the score on each correct answer. Once 10 dialogues are done, display an end message with the total score, and reset back the score to 0 and music as well.

Conclusion

As mentioned earlier, very soon you might expect a 3 Part Tutorial Series On Tkinter. Subscribe to our Newsletter to stay updated. If you are a weeb who knows to code, then you can get the source code. Get the source code for the above demonstrated GUI.

If you are a weeb who doesn’t know to code, then you should wait a little longer. As we will soon launch a subpage named “Project to try”, where you can try many amazing Anime Web Games.

And do support us by Buying A Coffee, if you like our content.