Responding to keyboard events
Links a key press to a handler function. Each time a key is pressed, tkinter calls handler(event), and event.keysym tells you which key it was.
Understanding Responding to keyboard events in QCE Digital Solutions Unit 1
What it does: Tkinter calls handler(event), so the handler needs one parameter (here event) to receive the key press details. Comparing event.keysym with a key name inside the handler lets the program react to one particular key. Pass the function name without brackets. Brackets would call on_key straight away instead of letting tkinter call it when a key is pressed. For the arrow keys, keysym is Up, Down, Left or Right. Use == to compare values. A single = is assignment and causes a syntax error in a condition. The handler is an event handler. It waits quietly and runs every time the event happens.
Common mistakes: A function header must end with a colon. The first line should be def key(event): so the handler body can follow. Python is case sensitive. The window class is Tk with a capital T, so it must be root = tk.Tk(). Without it, root.bind() has no window to work on.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
root.bind('<KeyPress>', handler)Fill in the Blanks
Spot the Error
Student with a class code? Join your class →