Event triggers changing the user interface

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

Event triggers changing the user interface

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line changes the text of a tkinter label called status when a handler runs?
A status.text = config('Saved')
B status.config(text='Saved')
2. What is the main reason a handler calls label.config(text=...)?
A To give the user feedback after an event
B To create a new window each time
3. A button's handler contains this line: msg.config(fg='red'). What happens when the button is clicked?
A The colour of the text in msg changes to red
B The button itself turns red
4. Which handler gives feedback when a key is pressed?
A def on_key(event): label.config(event.keysym)
B def on_key(event): label.config(text=event.keysym)
5. True or false: the label on screen updates by itself when you change a variable, without calling config().
A False. You must call config() in the handler
B True. The label always follows the variable

Match the Pairs

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

Left columnRight column
1label.config(text='Saved') Aconfig() needs the option name text= before the value. It should be label.config(text='Done').
2button.config(bg='green') Bdef on_click(event): label.config(text='Clicked!')
3A key press handler updates a label to show which key the user pressed CThe text value is missing quotes, so Python treats Done as a variable name and gives a NameError. It should be text='Done'.
4A mouse click handler changes a label so the user knows the click was detected Ddef on_key(event): label.config(text=event.keysym)
5label.config(text=Done) EChanges the background colour of a widget when the handler runs
6label.config('Done') FChanges the text shown on a label so the user sees feedback after an event

Definitions

label.config(text='Done')

Fill in the Blanks

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

1.
label.______(text='Done')
2.
label.config(____='Saved')
3.
def warn(): label.config(__='red') button = tk.Button(root, command=warn)
4.
def show(event): label.config(text='Key pressed') root.____('<Key>', show)
5.
def show(_____): label.config(text='Mouse clicked') label.bind('<Button-1>', show)

Spot the Error

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

Question 1
A def done():
B label.config(txt='Done')
C btn = tk.Button(command=done)
Question 2
A def on_key(event):
B key = event.keysym
C label.text = key
D root.bind('<Key>', on_key)
Question 3
A def hover(event):
B btn.config(bg='yellow')
C msg.config(text='Hover')
D btn.bind('<Enter>', hover())
Question 4
A def leave(event):
B btn.config(bg='white')
C msg.config(text='Bye')
D btn.bind(leave, '<Leave>')
Question 5
A def on_key():
B label.config(text='Key!')
C root.bind('<Key>', on_key)

Answer Key

Event triggers changing the user interface
Section A: Quick Questions
Q1
B — status.config(text='Saved')
Q2
A — To give the user feedback after an event
Q3
A — The colour of the text in msg changes to red
Q4
B — def on_key(event): label.config(text=event.keysym)
Q5
A — False. You must call config() in the handler
Section B: Match the Pairs
Q1
label.config(text='Saved') → Changes the text shown on a label so the user sees feedback after an event
Q2
button.config(bg='green') → Changes the background colour of a widget when the handler runs
Q3
A key press handler updates a label to show which key the user pressed → def on_key(event): label.config(text=event.keysym)
Q4
A mouse click handler changes a label so the user knows the click was detected → def on_click(event): label.config(text='Clicked!')
Q5
label.config(text=Done) → The text value is missing quotes, so Python treats Done as a variable name and gives a NameError. It should be text='Done'.
Q6
label.config('Done') → config() needs the option name text= before the value. It should be label.config(text='Done').
Section C: Definitions
Q1
label.config(text='Done') — A handler runs when an event happens and can call config() to change the interface, such as updating a label's text to give the user feedback.
Section D: Fill in the Blanks
Q1
label.config(text='Done')
Q2
label.config(text='Saved')
Q3
def warn(): label.config(fg='red') button = tk.Button(root, command=warn)
Q4
def show(event): label.config(text='Key pressed') root.bind('<Key>', show)
Q5
def show(event): label.config(text='Mouse clicked') label.bind('<Button-1>', show)
Section E: Spot the Error
Q1
B — The option that sets a label's text is text, not txt. It should be label.config(text='Done'), otherwise tkinter raises an error and the label never updates.
Q2
C — Setting an attribute called text does not change what the label shows on screen. Use label.config(text=key) so the handler updates the interface.
Q3
D — hover() calls the handler straight away instead of passing it to bind. It should be btn.bind('<Enter>', hover) so it only runs when the mouse enters the button.
Q4
D — The arguments to bind are in the wrong order. The event name comes first, then the handler: btn.bind('<Leave>', leave).
Q5
A — bind() passes an event object to the handler, so the handler needs a parameter. It should be def on_key(event):.