Object and event triggers

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

Object and event triggers

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A student clicks a Submit button and a message appears. What is the click?
A An event handler
B An event trigger
2. Which is an object trigger?
A A for loop that repeats five times
B A button that the user can click
3. What is an event handler?
A The code that runs in response to an event
B The user action that starts the event
4. Which one is an event trigger?
A Pressing the Enter key
B Declaring x <- 5
5. A program waits for the user to act before it responds. What starts the handler code?
A The event trigger happening
B The program reaching the end

Match the Pairs

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

Left columnRight column
1button.bind('<Button-1>', on_click) ALinks a left mouse click on the button (the event trigger) to the on_click handler, so on_click runs when the click happens
2def on_click(event): BThe handler is called with () instead of being passed by name. It runs immediately and not when the click happens. It should be on_click.
3An object trigger: the interface object the user acts on, such as a button, which is the source of the event CDefines an event handler, the code that runs when its trigger happens
4An event trigger such as a key press can be linked to a handler so the program responds Droot.bind('<Key>', on_key)
5button.bind('<Button-1>', on_click()) EThe handler has no event parameter, but bind() passes an event object, so a TypeError occurs when a key is pressed. It should be def on_key(event):
6def on_key(): print('Key pressed') root.bind('<Key>', on_key) Fbutton = tk.Button(root, text='Go')

Definitions

event trigger

Fill in the Blanks

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

1.
___ on_click(): print("Button clicked!")
2.
btn = Button(root, text="Go", _______ =on_click)
3.
root.____("<Key>", on_key)
4.
root.bind("__________", on_click)
5.
root.________()

Spot the Error

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

Question 1
A def hi():
B print('Hi!')
C b = Button(w, command=hi())
D b.pack()
Question 2
A def on_key(event):
B print(key.char)
C w.bind('<Key>', on_key)
Question 3
A def clicked(event):
B print(event.x, event.z)
C c.bind('<Button-1>', clicked)
Question 4
A count = 0
B def add():
C global count
D count == count + 1
E b = Button(w, command=add)
Question 5
A def greet():
B lbl.config(text='Hi!')
C b = Button(w, onclick=greet)
D b.pack()

Answer Key

Object and event triggers
Section A: Quick Questions
Q1
B — An event trigger
Q2
B — A button that the user can click
Q3
A — The code that runs in response to an event
Q4
A — Pressing the Enter key
Q5
A — The event trigger happening
Section B: Match the Pairs
Q1
button.bind('<Button-1>', on_click) → Links a left mouse click on the button (the event trigger) to the on_click handler, so on_click runs when the click happens
Q2
def on_click(event): → Defines an event handler, the code that runs when its trigger happens
Q3
An object trigger: the interface object the user acts on, such as a button, which is the source of the event → button = tk.Button(root, text='Go')
Q4
An event trigger such as a key press can be linked to a handler so the program responds → root.bind('<Key>', on_key)
Q5
button.bind('<Button-1>', on_click()) → The handler is called with () instead of being passed by name. It runs immediately and not when the click happens. It should be on_click.
Q6
def on_key(): print('Key pressed') root.bind('<Key>', on_key) → The handler has no event parameter, but bind() passes an event object, so a TypeError occurs when a key is pressed. It should be def on_key(event):
Section C: Definitions
Q1
event trigger — An action on an interface object, such as a click or key press, that starts a response in the program. The code that responds is called an event handler.
Section D: Fill in the Blanks
Q1
def on_click(): print("Button clicked!")
Q2
btn = Button(root, text="Go", command =on_click)
Q3
root.bind("<Key>", on_key)
Q4
root.bind("<Button-1>", on_click)
Q5
root.mainloop()
Section E: Spot the Error
Q1
C — command needs the handler's name only. hi() runs the function straight away instead of waiting for the click. It should be command=hi.
Q2
B — The key press details arrive in the event parameter. The name key is not defined. It should be event.char.
Q3
B — A mouse event stores the click position in x and y. There is no z attribute. It should be event.y.
Q4
D — == compares values and does not store anything, so count never changes when the button is clicked. The handler needs the assignment operator: count = count + 1.
Q5
C — tkinter buttons link a click to a handler with the command option. onclick is not a valid option. It should be command=greet.