← QCE Digital Solutions Unit 1

Responding to mouse events

Runs the handler function when the left mouse button is clicked on that widget. The handler receives an event with event.x and event.y. Use '<Double-Button-1>' for a double click.

Understanding Responding to mouse events in QCE Digital Solutions Unit 1

What it does: The handler takes an event parameter, and event.x and event.y hold the click position in the widget. Pass the function name without brackets. bind() calls the handler for you when the click happens. Adding () would run it straight away. tkinter uses '<Button-1>' for the left button. '<Left-Click>' is not a real event name. x and y are not defined variables here. The position must be read from the event object as event.x and event.y.

Common mistakes: x and y are not defined variables. The mouse position is stored on the event object, so the line should be print(event.x, event.y). bind() passes an event object to the handler, so the function needs an event parameter. It should be def hello(event): or the click causes a TypeError.

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
Which line makes on_click run when the left mouse button is clicked on canvas?
canvas.bind('<Button-1>', on_click)canvas.bind('<Button-1>', on_click())
Matches
canvas.bind('<Button-1>', on_click())
↓
The handler has brackets, so it is called straight away instead of waiting for a click. It should be passed as on_click.
Applying
Meanings
Term
widget.bind('<Button-1>', handler)
Meaning
Runs the handler function when the left mouse button is clicked on that widget. The handler receives an event with event.x and event.y. Use '<Double-Button-1>' for a double click.
Practice
import tkinter as tk
Testing
Gaps
canvas.('<Button-1>', onclick)
Check Answer
Spots
1def click(event):
2 print(x, y)

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

The 'Kangaroo Rat' badge — Speed Typer, 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. Which line makes on_click run when the left mouse button is clicked on canvas?
A. canvas.bind('<Button-1>', on_click) ✓
B. canvas.bind('<Button-1>', on_click())
Pass the function name without brackets. bind() calls the handler for you when the click happens. Adding () would run it straight away.
2. In tkinter, which event string means a left mouse click?
A. '<Left-Click>'
B. '<Button-1>' ✓
tkinter uses '<Button-1>' for the left button. '<Left-Click>' is not a real event name.
3. Which handler header can receive the click information from bind()?
A. def on_click():
B. def on_click(event): ✓
bind() passes an event object to the handler, so the handler needs a parameter to receive it. Without one, Python raises a TypeError when the click happens.
4. Inside a click handler, how do you read the horizontal position of the mouse click?
A. event.x ✓
B. event.horizontal
The event object stores the click position in event.x (across) and event.y (down), measured in pixels from the widget's top-left corner.
5. Which event string makes the handler run on a double click with the left button?
A. '<Double-Button-1>' ✓
B. '<Button-2>'
'<Double-Button-1>' is the double left click. '<Button-2>' is the middle mouse button, not a double click.

Match the Pairs

canvas.bind('<Button-1>', on_click) → Runs on_click when the left mouse button is clicked on the canvas
canvas.bind('<Double-Button-1>', on_double) → Runs on_double when the left mouse button is double-clicked on the canvas
A handler reads where the mouse was clicked using the event it receives → def on_click(event): print(event.x, event.y)
Bind the left click on a window so that the function show_menu runs when it happens → window.bind('<Button-1>', show_menu)
canvas.bind('<Button-1>', on_click()) → The handler has brackets, so it is called straight away instead of waiting for a click. It should be passed as on_click.
def on_click(): print(event.x, event.y) → The handler is missing the event parameter, so event is not defined and the click position cannot be read. It should be def on_click(event):

Key Term

widget.bind('<Button-1>', handler)
Runs the handler function when the left mouse button is clicked on that widget. The handler receives an event with event.x and event.y. Use '<Double-Button-1>' for a double click.

Fill in the Blanks

1. canvas.bind('<Button-1>', on_click)
Hint: Type the widget method that links an event to a handler function
2. canvas.bind('<Button-1>', on_click)
Hint: Type the name for a left mouse click, without the angle brackets
3. print('Y:', event.y)
Hint: Type the attribute that holds the vertical position of the mouse click
4. canvas.bind('<Double-Button-1>', on_click)
Hint: Type the name for a left double click, without the angle brackets
5. def clicked(event): print(event.x)
Hint: Type the parameter name that receives the details of the click

Spot the Error

Question 1
A def click(event):
B print(x, y)
C c.bind('<Button-1>', click)
Line B — x and y are not defined variables. The mouse position is stored on the event object, so the line should be print(event.x, event.y).
Question 2
A def hello():
B label.config(text='Hi')
C btn.bind('<Button-1>', hello)
Line A — bind() passes an event object to the handler, so the function needs an event parameter. It should be def hello(event): or the click causes a TypeError.
Question 3
A cv = tk.Canvas(root)
B def mark(event):
C print('Click at', event.x)
D cv.bind('<Button-1>', mark())
Line D — mark() calls the handler straight away instead of registering it. Pass the function name without brackets: cv.bind('<Button-1>', mark).
Question 4
A seq = '<Double-Click-1>'
B def zoom(event):
C print('Zoom in')
D c.bind(seq, zoom)
Line A — '<Double-Click-1>' is not a valid tkinter event name. A double left click is '<Double-Button-1>'.
Question 5
A def dot(event):
B x = event.x
C y = event.x
D print(x, y)
E c.bind('<Button-1>', dot)
Line C — y is being given the horizontal position. The vertical mouse position is event.y, so the line should be y = event.y.

Student with a class code? Join your class →