Responding to keyboard events

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

Responding to keyboard events

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line makes tkinter call on_key every time any key is pressed?
A root.bind('<KeyPress>', on_key)
B root.bind('<KeyPress>', on_key())
2. A handler for a key press needs which parameter?
A def on_key(event):
B def on_key():
3. Which expression tells you the name of the key that was pressed?
A root.keysym
B event.keysym
4. A player presses the Up arrow key. What is event.keysym?
A 'Arrow'
B 'Up'
5. Which condition checks that the Escape key was pressed inside the handler?
A if event.keysym == 'Escape':
B if event.keysym = 'Escape':

Match the Pairs

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

Left columnRight column
1root.bind('<KeyPress>', on_key) AThe handler has no event parameter, so event is undefined and tkinter cannot pass the key press in. It should be def on_key(event):
2event.keysym BTells tkinter to call the function on_key every time any key is pressed in the window
3A handler function must have a parameter to receive the event that tkinter passes in Cdef on_key(event):
4Respond only when a specific key, such as the Up arrow, is pressed DThe handler is called with brackets, so it runs straight away instead of waiting for a key press. Pass on_key without ().
5root.bind('<KeyPress>', on_key()) EGives the name of the key that was pressed, such as 'a', 'Up' or 'space'
6def on_key(): print(event.keysym) Fif event.keysym == 'Up':

Definitions

root.bind('<KeyPress>', handler)

Fill in the Blanks

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

1.
root.____('<KeyPress>', handler)
2.
def handler(_____): print(event.keysym)
3.
key = event.______
4.
if event.keysym __ 'Up': print('Up arrow')
5.
def press(event): print(event.keysym) root.bind('<KeyPress>', _____)

Spot the Error

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

Question 1
A def key(event)
B print(event.keysym)
C root.bind('<KeyPress>', key)
Question 2
A import tkinter as tk
B root = tk.tk()
C def key(event):
D print(event.keysym)
E root.bind('<KeyPress>', key)
Question 3
A root = tk.Tk()
B def key(event):
C print(event.keysym)
D root.bind('<KeyPress>', key())
E root.mainloop()
Question 4
A def key(event):
B if event.keysym == 'Up':
C print('Moving up'
D root.bind('<KeyPress>', key)
Question 5
A def key(event):
B pressed = event.keysym
C if pressed = 'space':
D print('Jump!')
E root.bind('<KeyPress>', key)

Answer Key

Responding to keyboard events
Section A: Quick Questions
Q1
A — root.bind('<KeyPress>', on_key)
Q2
A — def on_key(event):
Q3
B — event.keysym
Q4
B — 'Up'
Q5
A — if event.keysym == 'Escape':
Section B: Match the Pairs
Q1
root.bind('<KeyPress>', on_key) → Tells tkinter to call the function on_key every time any key is pressed in the window
Q2
event.keysym → Gives the name of the key that was pressed, such as 'a', 'Up' or 'space'
Q3
A handler function must have a parameter to receive the event that tkinter passes in → def on_key(event):
Q4
Respond only when a specific key, such as the Up arrow, is pressed → if event.keysym == 'Up':
Q5
root.bind('<KeyPress>', on_key()) → The handler is called with brackets, so it runs straight away instead of waiting for a key press. Pass on_key without ().
Q6
def on_key(): print(event.keysym) → The handler has no event parameter, so event is undefined and tkinter cannot pass the key press in. It should be def on_key(event):
Section C: Definitions
Q1
root.bind('<KeyPress>', handler) — 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.
Section D: Fill in the Blanks
Q1
root.bind('<KeyPress>', handler)
Q2
def handler(event): print(event.keysym)
Q3
key = event.keysym
Q4
if event.keysym == 'Up': print('Up arrow')
Q5
def press(event): print(event.keysym) root.bind('<KeyPress>', press)
Section E: Spot the Error
Q1
A — A function header must end with a colon. The first line should be def key(event): so the handler body can follow.
Q2
B — 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.
Q3
D — bind() needs the handler function itself, not the result of calling it. Writing key() runs it straight away. It should be root.bind('<KeyPress>', key).
Q4
C — The print call is missing its closing bracket, which causes a SyntaxError. It should be print('Moving up').
Q5
C — A single = assigns a value, but an if condition needs the comparison operator ==. It should be if pressed == 'space':.