GUI Button control and event handlers

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

GUI Button control and event handlers

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly makes a button that runs the function show when clicked?
A tk.Button(root, text='Go', command=show())
B tk.Button(root, text='Go', command=show)
2. What does the command option of a tk.Button do?
A It sets the function that runs when the button is clicked
B It sets the text shown on the button
3. Which statement about an event handler function is correct?
A It is defined with def and runs only when the event happens, such as a click
B It runs once as soon as the window opens, before any click
4. A student writes command=save() and the save code runs as the window opens. Why?
A The brackets call save immediately, so the button gets its result, not the function
B Buttons always run their command once when they are created
5. Inside a handler, which line reads the text typed into an Entry called name_entry?
A name = name_entry.text
B name = name_entry.get()

Match the Pairs

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

Left columnRight column
1tk.Button(root, text='Save', command=save_data) ACreates a button labelled Save that runs the function save_data each time it is clicked
2def on_click(): Bbtn = tk.Button(root, text='Go', command=show)
3Pass the handler to command by name only, with no brackets, so it runs on click and not straight away CThe button is created before show is defined, so Python raises a NameError. Define the handler function first, then create the button.
4A handler can read text from an Entry with .get() and update a Label with .config() Ddef greet(): lbl.config(text='Hi ' + ent.get())
5tk.Button(root, text='Go', command=show()) EBrackets after show make the function run immediately when the button is created, not when it is clicked. Use command=show.
6tk.Button(root, text='Go', command=show) def show(): print('Hi') FDefines an event handler function that will run when the user clicks the button

Definitions

tk.Button(root, text='Go', command=show)

Fill in the Blanks

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

1.
b = tk.Button(root, text='Go', command=____)
2.
b = tk.Button(root, text='Go', _______=show)
3.
def greet(): label.config(text='Hi!') b = tk.Button(root, text='Go', command=_____)
4.
def greet(): label.config(text=entry.___()) b = tk.Button(root, text='Go', command=greet)
5.
def show(): label.______(text='Clicked!') b = tk.Button(root, text='Go', command=show)

Spot the Error

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

Question 1
A def go():
B print('Hi')
C b = tk.Button(r, command=go())
D b.pack()
Question 2
A def hi():
B name = ent.get()
C lbl.config(text=name)
D b = tk.Button(r, command='hi')
E b.pack()
Question 3
A root = tk.Tk()
B lbl = tk.Label(root, text='')
C def show()
D lbl.config(text='Clicked')
E lbl.pack()
Question 4
A ent = tk.Entry(root)
B def greet():
C name = ent.get
D lbl.config(text=name)
Question 5
A def show():
B lbl.text = 'Done'
C b = tk.Button(r, command=show)
D b.pack()

Answer Key

GUI Button control and event handlers
Section A: Quick Questions
Q1
B — tk.Button(root, text='Go', command=show)
Q2
A — It sets the function that runs when the button is clicked
Q3
A — It is defined with def and runs only when the event happens, such as a click
Q4
A — The brackets call save immediately, so the button gets its result, not the function
Q5
B — name = name_entry.get()
Section B: Match the Pairs
Q1
tk.Button(root, text='Save', command=save_data) → Creates a button labelled Save that runs the function save_data each time it is clicked
Q2
def on_click(): → Defines an event handler function that will run when the user clicks the button
Q3
Pass the handler to command by name only, with no brackets, so it runs on click and not straight away → btn = tk.Button(root, text='Go', command=show)
Q4
A handler can read text from an Entry with .get() and update a Label with .config() → def greet(): lbl.config(text='Hi ' + ent.get())
Q5
tk.Button(root, text='Go', command=show()) → Brackets after show make the function run immediately when the button is created, not when it is clicked. Use command=show.
Q6
tk.Button(root, text='Go', command=show) def show(): print('Hi') → The button is created before show is defined, so Python raises a NameError. Define the handler function first, then create the button.
Section C: Definitions
Q1
tk.Button(root, text='Go', command=show) — Makes a button that runs the function show when clicked. Write the function name with no brackets, or it runs straight away instead of on click.
Section D: Fill in the Blanks
Q1
b = tk.Button(root, text='Go', command=show)
Q2
b = tk.Button(root, text='Go', command=show)
Q3
def greet(): label.config(text='Hi!') b = tk.Button(root, text='Go', command=greet)
Q4
def greet(): label.config(text=entry.get()) b = tk.Button(root, text='Go', command=greet)
Q5
def show(): label.config(text='Clicked!') b = tk.Button(root, text='Go', command=show)
Section E: Spot the Error
Q1
C — Brackets after the function name call go() straight away, before any click. It should be command=go with no brackets so it runs only when the button is clicked.
Q2
D — command needs the function itself, not a string. It should be command=hi with no quotes and no brackets.
Q3
C — A function header must end with a colon. It should be def show(): so the indented handler body belongs to the function.
Q4
C — Without brackets, ent.get is only the method itself and does not read the Entry text. It should be ent.get() to call it and return what the user typed.
Q5
B — Setting lbl.text does not change what the label displays. Use lbl.config(text='Done') to update the label's text.