AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

Graphical interface: tkinter

Lessons 53–54 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
54

Practical lesson: a graphical application

Textbook: p. 107
GoalIn the practical lesson, builds small apps with tkinter: a random number button and a window that adds two numbers, and checks the typed text.
New words
Entry.get(): returns the text typed in the field as a string · Entry.get()config(text=…): changes the text of a label · config(text=…)isdigit(): True if a string consists only of digits · isdigit()event: an action of the user, such as pressing a button · voqea
Explanation

A GUI program responds to events: the user presses a button and the matching function runs. In the die window the button calls random.randint(1, 6) and writes the result into a label. In the addition window there are two Entry fields and the function takes their text with get(). The user may type letters instead of numbers, so we first check with a.isdigit() and b.isdigit() that the text consists only of digits; without the check int("abc") gives a ValueError and the app fails. According to the result of the check the label shows either the sum or the message “Son kiriting”. When making an app do not ask for real personal data (a password, an address, a phone number): a study app should have only numbers and plain text. Before running the app check the place of every widget in the grid, otherwise they may fall on top of each other.

💡 Where will I use this?
In real life

Before a class quiz, pupils sketched a scoreboard window on paper: a label for the score and a “+1” button. They wrote step by step what happens when the button is pressed and then tested the plan by acting it out.

Who uses it

Interface designers, game developers and testers plan the events that respond to a button press in advance.

Try it today

Imagine a window that asks for a number of tickets. On paper list three wrong things someone could type and the short message your window would show for each.

Worked examples
Python
1import tkinter as tk
2import random
3def tashla():
4    natija.config(text=str(random.randint(1, 6)))
5oyna = tk.Tk()
6oyna.title("Kubik")
7natija = tk.Label(oyna, text="")
8natija.grid(row=0, column=0)
9tugma = tk.Button(oyna, text="Tashla", command=tashla)
10tugma.grid(row=1, column=0)
11oyna.mainloop()
Rolling a die – code above. When the button is pressed, one number from 1 to 6 appears in the label.
Python
1import tkinter as tk
2def qosh():
3    a = maydon1.get()
4    b = maydon2.get()
5    if a.isdigit() and b.isdigit():
6        natija.config(text=str(int(a) + int(b)))
7    else:
8        natija.config(text="Son kiriting")
9oyna = tk.Tk()
10oyna.title("Qo‘shish")
11maydon1 = tk.Entry(oyna, width=8)
12maydon1.grid(row=0, column=0)
13maydon2 = tk.Entry(oyna, width=8)
14maydon2.grid(row=0, column=1)
15tugma = tk.Button(oyna, text="Qo‘sh", command=qosh)
16tugma.grid(row=1, column=0)
17natija = tk.Label(oyna, text="")
18natija.grid(row=1, column=1)
19oyna.mainloop()
Adding two numbers – code above. If 7 and 5 are typed, the label shows 12; if letters are typed in the fields, the label shows Son kiriting.
Class activity

“App tester”: pairs give their apps to each other; the tester types correct and wrong data (a number, letters, empty) into the fields and writes in a table how the app responds.

Practice
1
Which values can appear in the label in the die app?
2
Write a function that shows in the label the product of the numbers in two Entry fields (35 for 7 and 5).
3
What are "12a".isdigit() and "125".isdigit() equal to?
4
Why must the typed text be checked before it is passed to int()?
Story of the Week · The Khwarizmi WayThe end of the year and a summer planClose the week: all of this week’s subjects in one storyRead →