What a graphical user interface (GUI) is

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

What a graphical user interface (GUI) is

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement best describes a GUI?
A Windows and controls such as buttons and text boxes that the user clicks or types in
B A program that only shows text and takes typed commands in a console
2. In an event-driven GUI program, what does the program do after it shows the window?
A It runs every line once from top to bottom and then ends straight away
B It waits for events such as a button click, then responds to each one
3. Which of these is an event?
A The user clicks a Submit button
B A variable is given the value 10
4. What is the main purpose of a Button control?
A Lets the user trigger an action by clicking it
B Lets the user type in a line of text
5. Which control would you choose to let a user type their name?
A A Label
B A text box (Entry)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Button(window, text="Save", command=save_data) Adef on_click(): print("Clicked")
2window.mainloop() Bname_box = Entry(window)
3A text box control lets the user type in a value for the program to use CThere is no window.mainloop() at the end, so the window closes straight away and never waits for events.
4A GUI program is event-driven: it runs a function in response to a user action such as a click DStarts the event loop so the window stays open and waits for the user to click or type
5Button(window, text="Go", command=start()) ECreates a button control that runs the save_data function when the user clicks it
6window = Tk() Label(window, text="Hi").pack() Fstart() has brackets, so the function runs immediately when the button is created instead of on a click. Use command=start.

Definitions

GUI

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
import tkinter as tk root = tk.__() root.title("My App")
2.
root = tk.Tk() btn = tk.______(root, text="Click me") btn.pack()
3.
entry = tk._____(root) entry.pack()
4.
def greet(): print("Hi!") btn = tk.Button(root, _______=greet)
5.
btn.pack() root.________()

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import tkinter as tk
B r = tk.Tk()
C btn = tk.button(r, text="Go")
D btn.pack()
E r.mainloop()
Question 2
A r = tk.Tk()
B def go():
C print("Clicked")
D b = tk.Button(r, command=go())
E b.pack()
Question 3
A import tkinter as tk
B r = tk.Tk()
C lbl = tk.Label(r, txt="Hi")
D lbl.pack()
E r.mainloop()
Question 4
A import tkinter as tk
B root = tk.Window()
C root.title("Quiz")
D root.mainloop()
Question 5
A root = tk.Tk()
B root.title("App")
C root.mainloop
D print("Closed")

Answer Key

What a graphical user interface (GUI) is
Section A: Quick Questions
Q1
A — Windows and controls such as buttons and text boxes that the user clicks or types in
Q2
B — It waits for events such as a button click, then responds to each one
Q3
A — The user clicks a Submit button
Q4
A — Lets the user trigger an action by clicking it
Q5
B — A text box (Entry)
Section B: Match the Pairs
Q1
Button(window, text="Save", command=save_data) → Creates a button control that runs the save_data function when the user clicks it
Q2
window.mainloop() → Starts the event loop so the window stays open and waits for the user to click or type
Q3
A text box control lets the user type in a value for the program to use → name_box = Entry(window)
Q4
A GUI program is event-driven: it runs a function in response to a user action such as a click → def on_click(): print("Clicked")
Q5
Button(window, text="Go", command=start()) → start() has brackets, so the function runs immediately when the button is created instead of on a click. Use command=start.
Q6
window = Tk() Label(window, text="Hi").pack() → There is no window.mainloop() at the end, so the window closes straight away and never waits for events.
Section C: Definitions
Q1
GUI — A graphical user interface: windows and controls like buttons and text boxes the user clicks or types in. The program waits for events, such as a click, and responds to each one.
Section D: Fill in the Blanks
Q1
import tkinter as tk root = tk.Tk() root.title("My App")
Q2
root = tk.Tk() btn = tk.Button(root, text="Click me") btn.pack()
Q3
entry = tk.Entry(root) entry.pack()
Q4
def greet(): print("Hi!") btn = tk.Button(root, command=greet)
Q5
btn.pack() root.mainloop()
Section E: Spot the Error
Q1
C — Python is case sensitive. The control class is Button with a capital B, so it should be tk.Button(r, text="Go").
Q2
D — The brackets call go() straight away instead of waiting for a click. The event handler must be passed without brackets: command=go.
Q3
C — A Label has no option called txt. The option that sets the displayed words is text, so it should be text="Hi".
Q4
B — The main window of a tkinter GUI is created with tk.Tk(). There is no tk.Window() class, so it should be root = tk.Tk().
Q5
C — Without brackets mainloop is never called, so the program never starts waiting for events like clicks. It should be root.mainloop().