STARLEAP / PYTHON TURTLE
Build 2026.09.17-001210-0400
On this page
9. Keyboard-Controlled Turtle: Programs That Wait
Most programs in these handouts run from top to bottom and then finish. Today you will write a program that waits for keys.
Keyboard programs use events. An event is something that happens while the program is running, like pressing a key.
Start With A Turtle File
Save a new file named keyboard_turtle.py.
Start with this template.
import turtle
screen = turtle.Screen()
screen.setup(600, 800, 0, 0)
t = turtle.Turtle()
t.speed(0)
t.pensize(3)
# Your keyboard code goes here.
screen.mainloop()
Run the program. A blank turtle window is fine. Close the turtle window before you change the code again.
Step 1 - One Movement Function
Replace the middle comment with this function.
t.speed(0)
t.pensize(3)
def move_forward():
t.forward(30)
screen.mainloop()
Run the program. Nothing new should happen yet.
You defined a function, but you did not call it. In a keyboard program, the key press will call the function later.
Step 2 - Connect A Key
Add these lines under the function.
def move_forward():
t.forward(30)
screen.listen()
screen.onkey(move_forward, 'Up')
screen.mainloop()
Run the program. Click the turtle window, then press the Up arrow key.
Important: use move_forward, not move_forward(), inside screen.onkey(...). You are handing the function to the screen so it can call the function later.
Step 3 - Add Turning
Add a left turn function.
def move_forward():
t.forward(30)
def turn_left():
t.left(30)
screen.listen()
screen.onkey(move_forward, 'Up')
Connect the Left arrow key.
screen.listen()
screen.onkey(move_forward, 'Up')
screen.onkey(turn_left, 'Left')
screen.mainloop()
Run the program. Click the turtle window, then try Up and Left.
Step 4 - Add The Right Arrow
Add a right turn function.
def turn_left():
t.left(30)
def turn_right():
t.right(30)
screen.listen()
screen.onkey(move_forward, 'Up')
screen.onkey(turn_left, 'Left')
Connect the Right arrow key.
screen.onkey(move_forward, 'Up')
screen.onkey(turn_left, 'Left')
screen.onkey(turn_right, 'Right')
screen.mainloop()
Run the program. Click the turtle window, then steer with the arrow keys.
Step 5 - Add Pen Controls
Add two functions that lift and lower the pen.
def turn_right():
t.right(30)
def lift_pen():
t.penup()
def drop_pen():
t.pendown()
screen.listen()
Connect them to letter keys.
screen.onkey(turn_left, 'Left')
screen.onkey(turn_right, 'Right')
screen.onkey(lift_pen, 'u')
screen.onkey(drop_pen, 'd')
screen.mainloop()
Run the program. Use u to move without drawing and d to draw again.
Step 6 - Change Color With A Key
Add a color function.
def drop_pen():
t.pendown()
def use_purple():
t.color('purple')
screen.listen()
Connect it to a key.
screen.onkey(lift_pen, 'u')
screen.onkey(drop_pen, 'd')
screen.onkey(use_purple, 'p')
screen.mainloop()
Run the program. Press p, then draw.
Final Project - Turtle Drawing Pad
Use this full program as a keyboard-controlled drawing pad.
import turtle
screen = turtle.Screen()
screen.setup(600, 800, 0, 0)
t = turtle.Turtle()
t.speed(0)
t.pensize(3)
def move_forward():
t.forward(30)
def move_backward():
t.backward(30)
def turn_left():
t.left(30)
def turn_right():
t.right(30)
def lift_pen():
t.penup()
def drop_pen():
t.pendown()
def use_purple():
t.color('purple')
def use_teal():
t.color('teal')
screen.listen()
screen.onkey(move_forward, 'Up')
screen.onkey(move_backward, 'Down')
screen.onkey(turn_left, 'Left')
screen.onkey(turn_right, 'Right')
screen.onkey(lift_pen, 'u')
screen.onkey(drop_pen, 'd')
screen.onkey(use_purple, 'p')
screen.onkey(use_teal, 't')
screen.mainloop()
Run the program. Click the turtle window before using the keyboard.
Make a drawing using the arrow keys. Use u to lift the pen, d to draw again, p for purple, and t for teal.
Drawing Pad Challenge - Random Color Key
Make the r key change the turtle to a random color.
You may need to look back at the random flower lesson and the keyboard code on the previous page.
When you are finished, your drawing pad should still work with the arrow keys, u, d, p, and t.
Run the program. Press r several times while you draw. Each press should be able to choose a different color.
Teacher Check
Show your keyboard drawing. Then point to:
- one movement function
- one color function
- the random color function, if you completed the challenge
- the
screen.listen()line - one
screen.onkey(...)line - the function name inside
screen.onkey(...)that does not have parentheses
Say this out loud: "This program waits for events. A key press calls the function connected to that key."