Responding to mouse events

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

Responding to mouse events

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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())
2. In tkinter, which event string means a left mouse click?
A '<Left-Click>'
B '<Button-1>'
3. Which handler header can receive the click information from bind()?
A def on_click():
B def on_click(event):
4. Inside a click handler, how do you read the horizontal position of the mouse click?
A event.x
B event.horizontal
5. Which event string makes the handler run on a double click with the left button?
A '<Double-Button-1>'
B '<Button-2>'

Match the Pairs

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

Left columnRight column
1canvas.bind('<Button-1>', on_click) ARuns on_click when the left mouse button is clicked on the canvas
2canvas.bind('<Double-Button-1>', on_double) Bwindow.bind('<Button-1>', show_menu)
3A handler reads where the mouse was clicked using the event it receives Cdef on_click(event): print(event.x, event.y)
4Bind the left click on a window so that the function show_menu runs when it happens DRuns on_double when the left mouse button is double-clicked on the canvas
5canvas.bind('<Button-1>', on_click()) EThe handler has brackets, so it is called straight away instead of waiting for a click. It should be passed as on_click.
6def on_click(): print(event.x, event.y) FThe 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):

Definitions

widget.bind('<Button-1>', handler)

Fill in the Blanks

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

1.
canvas.____('<Button-1>', on_click)
2.
canvas.bind('<________>', on_click)
3.
print('Y:', event._)
4.
canvas.bind('<_______________>', on_click)
5.
def clicked(_____): print(event.x)

Spot the Error

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

Question 1
A def click(event):
B print(x, y)
C c.bind('<Button-1>', click)
Question 2
A def hello():
B label.config(text='Hi')
C btn.bind('<Button-1>', hello)
Question 3
A cv = tk.Canvas(root)
B def mark(event):
C print('Click at', event.x)
D 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)
Question 5
A def dot(event):
B x = event.x
C y = event.x
D print(x, y)
E c.bind('<Button-1>', dot)

Answer Key

Responding to mouse events
Section A: Quick Questions
Q1
A — canvas.bind('<Button-1>', on_click)
Q2
B — '<Button-1>'
Q3
B — def on_click(event):
Q4
A — event.x
Q5
A — '<Double-Button-1>'
Section B: Match the Pairs
Q1
canvas.bind('<Button-1>', on_click) → Runs on_click when the left mouse button is clicked on the canvas
Q2
canvas.bind('<Double-Button-1>', on_double) → Runs on_double when the left mouse button is double-clicked on the canvas
Q3
A handler reads where the mouse was clicked using the event it receives → def on_click(event): print(event.x, event.y)
Q4
Bind the left click on a window so that the function show_menu runs when it happens → window.bind('<Button-1>', show_menu)
Q5
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.
Q6
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):
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
canvas.bind('<Button-1>', on_click)
Q2
canvas.bind('<Button-1>', on_click)
Q3
print('Y:', event.y)
Q4
canvas.bind('<Double-Button-1>', on_click)
Q5
def clicked(event): print(event.x)
Section E: Spot the Error
Q1
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).
Q2
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.
Q3
D — mark() calls the handler straight away instead of registering it. Pass the function name without brackets: cv.bind('<Button-1>', mark).
Q4
A — '<Double-Click-1>' is not a valid tkinter event name. A double left click is '<Double-Button-1>'.
Q5
C — y is being given the horizontal position. The vertical mouse position is event.y, so the line should be y = event.y.