← QCE Digital Solutions Unit 1

Object and event triggers

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.

Understanding Object and event triggers in QCE Digital Solutions Unit 1

What it does: The button is the interface object that the user clicks, so it is the object that carries the trigger. bind() ties the key press event to the on_key handler, so pressing a key starts the response. A key press is an action by the user that can start a response. Assigning a variable is just a step in the code. In an event-driven program, the handler runs when the trigger happens, not when the program ends. Buttons are made to be clicked. A label only displays text, so typing into it does not trigger anything.

Common mistakes: command needs the handler's name only. hi() runs the function straight away instead of waiting for the click. It should be command=hi. The key press details arrive in the event parameter. The name key is not defined. It should be event.char.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 158–380) and word search (Modules 82–380).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A student clicks a Submit button and a message appears. What is the click?
An event handlerAn event trigger
Matches
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.
Applying
Meanings
Term
event trigger
Meaning
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.
Practice
import tkinter as tk
Testing
Gaps
onclick():
Check Answer
Spots
2 print('Hi!')
3b = Button(w, command=hi())

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Nautilus' badge — Default Expert, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A student clicks a Submit button and a message appears. What is the click?
A. An event handler
B. An event trigger ✓
The click is the action that starts the response. The code that shows the message is the event handler.
2. Which is an object trigger?
A. A for loop that repeats five times
B. A button that the user can click ✓
An object trigger is an interface object, such as a button or text box, that the user acts on. A loop is not an interface object.
3. What is an event handler?
A. The code that runs in response to an event ✓
B. The user action that starts the event
The user action is the event trigger. The event handler is the code that responds to it.
4. Which one is an event trigger?
A. Pressing the Enter key ✓
B. Declaring x <- 5
A key press is an action by the user that can start a response. Assigning a variable is just a step in the code.
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
In an event-driven program, the handler runs when the trigger happens, not when the program ends.

Match the Pairs

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
def on_click(event): → Defines an event handler, the code that runs when its trigger happens
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')
An event trigger such as a key press can be linked to a handler so the program responds → root.bind('<Key>', on_key)
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.
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):

Key Term

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.

Fill in the Blanks

1. def on_click(): print("Button clicked!")
Hint: Type the keyword that starts defining an event handler function.
2. btn = Button(root, text="Go", command =on_click)
Hint: Type the option name that links the button's click trigger to its handler.
3. root.bind("<Key>", on_key)
Hint: Type the method name that connects an event trigger to its handler.
4. root.bind("<Button-1>", on_click)
Hint: Type the event pattern, with angle brackets, for a left mouse click.
5. root.mainloop()
Hint: Type the method name that keeps the window open and listening for event triggers.

Spot the Error

Question 1
A def hi():
B print('Hi!')
C b = Button(w, command=hi())
D b.pack()
Line 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.
Question 2
A def on_key(event):
B print(key.char)
C w.bind('<Key>', on_key)
Line B — The key press details arrive in the event parameter. The name key is not defined. It should be event.char.
Question 3
A def clicked(event):
B print(event.x, event.z)
C c.bind('<Button-1>', clicked)
Line B — A mouse event stores the click position in x and y. There is no z attribute. It should be event.y.
Question 4
A count = 0
B def add():
C global count
D count == count + 1
E b = Button(w, command=add)
Line 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.
Question 5
A def greet():
B lbl.config(text='Hi!')
C b = Button(w, onclick=greet)
D b.pack()
Line C — tkinter buttons link a click to a handler with the command option. onclick is not a valid option. It should be command=greet.

Student with a class code? Join your class →