Skip to Content
Course content

87: Handling Keyboard Input with Turtle

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Up until now, our Turtle programs have been mostly "press play and watch." While that's great for generative art, it's not very exciting if you want to actually interact with what's on the screen. Today, I want to show you how to turn the keyboard into a remote control for your turtle.

Defining our movement logic

To make this work, we can't just put a input() call in the middle of our code. That would pause the entire program and freeze the window. Instead, we use "event listeners." We define a function for what we want to happen, and then we tell Python to run that function whenever a specific key is pressed.

I'll start by defining a few simple functions to handle the basic movement. I'm keeping these lean because they'll be called repeatedly.

import turtle

t = turtle.Turtle()
screen = turtle.Screen()

def go_forward():
    t.forward(20)

def go_backward():
    t.backward(20)

def turn_left():
    t.left(15)

def turn_right():
    t.right(15)

Wiring the keyboard events

Now that we have the logic, we need to bind those functions to actual keys. For this, we use screen.onkey(). This method takes two arguments: the function to run (without parentheses, because we're passing the function itself, not calling it) and the name of the key as a string.

I'll use the arrow keys for this. They feel the most intuitive for navigation.

screen.onkey(go_forward, "Up")
screen.onkey(go_backward, "Down")
screen.onkey(turn_left, "Left")
screen.onkey(turn_right, "Right")

screen.mainloop()

The missing link: Making Turtle listen

I actually just tried running the code above in my editor, and... nothing happened. I hit the arrow keys, and the turtle just sat there staring at me. I made the classic mistake: I forgot to tell the screen to actually listen for the events.

By default, the Turtle window doesn't focus on keyboard input. We have to explicitly call screen.listen(). Without this, onkey is essentially shouting into a void. Let's fix that by adding it right before we set up our key bindings.

import turtle

t = turtle.Turtle()
screen = turtle.Screen()

def go_forward():
    t.forward(20)

def go_backward():
    t.backward(20)

def turn_left():
    t.left(15)

def turn_right():
    t.right(15)

# This is the part I missed!
screen.listen()

screen.onkey(go_forward, "Up")
screen.onkey(go_backward, "Down")
screen.onkey(turn_left, "Left")
screen.onkey(turn_right, "Right")

screen.mainloop()

Now, when you run this, the window captures your keystrokes and triggers the functions immediately. It's a simple pattern, but it's the foundation for building anything from a simple drawing tool to a full-blown game in Turtle.




📋 Practical Task

Build a Pen-Control Dashboard

Create a program where you can control not just the movement of the turtle, but the state of the pen using the keyboard. Your program should implement the following:

  • Use the 'u' key to lift the pen up (stop drawing).
  • Use the 'd' key to put the pen down (start drawing).
  • Use the 'c' key to clear the screen entirely.
  • Keep the arrow key movement from the lesson so you can navigate the screen while toggling the pen.

Make sure you remember to call the method that allows the screen to capture these inputs, or your keys won't do anything!

Rating
0 0

There are no comments for now.

to be the first to leave a comment.