Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
87: Handling Keyboard Input with Turtle
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!
There are no comments for now.