Practical lesson: a graphical application
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.
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.
Interface designers, game developers and testers plan the events that respond to a button press in advance.
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.
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()
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()
“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.