๐ข Python Turtle Graphics โ Comprehensive Guideยถ
The turtle
module in Python is a built-in library that makes it easy to introduce programming to beginners. You control a โturtleโ that moves and draws on the screen. It is perfect for learning loops, functions, geometry, and logic through visual feedback. Turtle methods are available using Python 2.6 and are fully supported in Python 3.x, including versions 3.10+, 3.11, and 3.12.
๐น Getting Started
import turtle
t = turtle.Turtle() # Create a turtle object
turtle.done() # Keep the window open at the end
๐ง What Is the โTurtleโ?
The turtle is a cursor (triangle by default) that moves around the screen.
It can go forward or backward
Turn right or left
Draw lines as it moves
Change shape, color, and size
You control the turtle with methods.
๐ Common Mistake: self.forward(100)
โ
That only works inside a class. For Turtle, you must use:
t = turtle.Turtle()
t.forward(100) # โ
๐ง Core Turtle Methods by Categoryยถ
๐งญ Turtle Motion & Positioning
Method |
Description |
---|---|
|
Move forward |
|
Move backward |
|
Turn right (clockwise) |
|
Turn left (counterclockwise) |
|
Move to absolute position |
|
Set x or y coordinate |
|
Face a specific direction in degrees |
|
Go to (0, 0) and face right |
|
Draw a circle or arc |
|
Draw a dot |
|
Stamp turtle shape at current location |
|
Remove a specific stamp |
|
Clear all or last |
|
Undo last action |
|
Set turtle animation speed |
๐ง Turtle State & Status
Method |
Description |
---|---|
|
Current (x, y) position |
|
Get x or y coordinate |
|
Get current angle turtle is facing |
|
Angle from current position to (x, y) |
|
Distance to point |
|
Whether pen is down (drawing) |
|
Whether the turtle is visible |
โ๏ธ Pen Control (Drawing Tools)
Method |
Description |
---|---|
|
Lift the pen (stop drawing) |
|
Lower the pen (start drawing) |
|
Set pen thickness |
|
Set pen (line) color |
|
Set fill color |
|
Fill shape drawn between these calls |
|
Set pen and fill color together |
|
Clear drawing but keep turtle position |
|
Clear everything and reset turtle state |
|
Write text on screen |
๐ข Turtle Appearance
Method |
Description |
---|---|
|
Set turtle shape (e.g., |
|
Stretch turtleโs size |
|
Tilt shape diagonally |
|
Tilt shape clockwise |
|
Make turtle visible |
|
Hide the turtle |
|
Control size behavior |
|
Get shape as polygon points |
๐ผ๏ธ Screen Control & Events
Method |
Description |
---|---|
|
Get turtle window control object |
|
Set background color |
|
Set background image |
|
Set window title |
|
Bind click or key event |
|
Focus screen to capture keys |
|
Start main loop (keep window open) |
|
Control animation redraw speed |
|
Close turtle window |
๐งฉ Miscellaneous / Advanced
Method |
Description |
---|---|
|
Start recording points for a shape |
|
Stop and return polygon |
|
Access underlying Tkinter canvas |
|
Get screen object (same as |
|
Set animation delay in milliseconds |
|
Get window size |
๐ข Example: Draw a Filled Star
import turtle
t = turtle.Turtle()
t.color("gold")
t.begin_fill()
for _ in range(5):
t.forward(100)
t.right(144)
t.end_fill()
turtle.done()
๐ Common Pitfalls and Fixes
Mistake |
Fix |
---|---|
Forgetting |
Window closes immediately after drawing |
Using |
Use |
Mixing up |
Practice turning to understand directions |
Drawing before |
Know when the turtle is supposed to draw |
Expecting |
Use strings of length 1 (e.g., |