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
53
Graphical user interfaces in Python
Textbook: pp. 104–106
GoalKnows the concepts of a graphical user interface (GUI) and a widget, creates a window, a label, an entry field and a button with the tkinter module, and makes a function run when a button is pressed.
New words
GUI: graphical user interface, made of windows, buttons and fields · GUIwidget: one element of the interface, such as Label, Entry or Button · vijetmainloop(): shows the window and waits for the user’s actions · mainloop()grid(): places a widget in a cell of a grid by row and column · grid()
Explanation
So far we have talked to the program through text; many programs have a graphical interface made of windows, buttons and text fields. The tkinter module, which comes with Python, creates such windows (on some systems it may need to be added separately). The order is: import tkinter as tk; the main window is created with oyna = tk.Tk(); oyna.title() and oyna.geometry("250x100") give the title and the size; widgets are created: tk.Label is a text label, tk.Entry is a text entry field, tk.Button is a button; we put them into grid cells with grid(row=…, column=…); finally oyna.mainloop() opens the window and waits for the user’s actions. The function that runs when the button is pressed is given with command= and written without brackets: command=salom; if it is written with brackets the function runs at once, without the button being pressed. The text in an Entry is taken with get() and is a string; if a number is needed it is turned with int(). The text of a label is changed with config(text=…).
Worked examples
A greeting window: import tkinter as tk ⏎ def salom(): ⏎ yorliq.config(text="Salom!") ⏎ oyna = tk.Tk() ⏎ oyna.title("Mening ilovam") ⏎ oyna.geometry("250x100") ⏎ yorliq = tk.Label(oyna, text="") ⏎ yorliq.grid(row=0, column=0) ⏎ tugma = tk.Button(oyna, text="Bos", command=salom) ⏎ tugma.grid(row=1, column=0) ⏎ oyna.mainloop() The window has the title “Mening ilovam”, and when the button is pressed the text Salom! appears in the label.
Doubling: import tkinter as tk ⏎ def ikki(): ⏎ n = int(maydon.get()) ⏎ natija.config(text=str(n * 2)) ⏎ oyna = tk.Tk() ⏎ oyna.title("Ikkilantirish") ⏎ maydon = tk.Entry(oyna, width=10) ⏎ maydon.grid(row=0, column=0) ⏎ tugma = tk.Button(oyna, text="x 2", command=ikki) ⏎ tugma.grid(row=0, column=1) ⏎ natija = tk.Label(oyna, text="") ⏎ natija.grid(row=1, column=0) ⏎ oyna.mainloop() If 21 is typed in the field and the button is pressed, 42 appears in the label, because get() gives a string and int() turns it into a number.
Class activity
“Design the window on paper”: the group draws a window on paper (label, field, button) and marks their places in the grid (row, column); another group writes the code from this drawing.
Practice
1
Which command opens the window and waits for the user’s actions?
oyna.mainloop()
2
Create a window with the title “Salom oyna” and a label “Xush kelibsiz”.