Master Python Turtle Graphics: From Basics to Creative Drawing Techniques
This tutorial introduces Python's built‑in turtle library, explains its window layout, coordinate systems, color handling, pen and movement functions, and demonstrates how to create shapes and wave patterns using loops and code examples.
1. Introduction to the turtle library
The turtle library is Python's standard drawing system that provides a virtual "turtle" moving on a canvas; the turtle can be controlled to change color, line width, and direction, producing graphical output.
2. Turtle drawing window layout
A window appears on the computer as the drawing canvas, measured in pixels. The initial size and position can be set with turtle.setup(width, height, startx, starty).
3. Turtle coordinate systems – absolute and turtle‑relative
Absolute coordinates use the screen as the reference with (0,0) at the center. Use turtle.goto(x, y) to move the turtle to a specific point.
import turtle
from time import sleep
# turtle.setup(800,800,0,0)
turtle.goto(100,100)
turtle.goto(100,-100)
turtle.goto(-100,-100)
turtle.goto(-100,100)
turtle.goto(0,0)
sleep(5)The turtle‑relative system treats the turtle itself as the origin; forward and backward commands move relative to its heading.
turtle.fd(d) # move forward d units
turtle.bk(d) # move backward d units
turtle.circle(radius, angle) # draw an arc with given radius and angle4. Turtle angle coordinate system
Absolute heading can be set with turtle.seth(angle), which only changes direction. In turtle‑relative mode, turtle.left(angle) and turtle.right(angle) rotate the turtle around its own axis.
5. RGB color system
Colors are defined by red, green, and blue channels, each ranging from 0‑255 (or 0‑1 as a float). The default mode uses floats; switch to integer mode with turtle.colormode(255).
turtle.colormode(255) # use 0‑255 integer values6. Turtle pen control functions
turtle.penup() # lift pen (alias turtle.pu())
turtle.pendown() # lower pen (alias turtle.pd())
turtle.pensize(width) # set pen width (alias turtle.width())
turtle.pencolor(color) # set pen color (color can be a name or RGB tuple)
turtle.speed(s) # set drawing speed, 0‑10 (higher is faster)
turtle.fillcolor(col) # fill color for shapes
turtle.color(pen, fill) # set both pen and fill colorsPen state remains until changed again.
7. Turtle movement control functions
turtle.forward(d) # move forward d pixels (alias turtle.fd(d))
turtle.circle(r, angle) # draw an arc of radius r spanning angle degrees8. Turtle direction control functions
turtle.setheading(angle) # set absolute heading (alias turtle.seth())
turtle.left(angle) # turn left by angle degrees
turtle.right(angle) # turn right by angle degreesThese functions only change orientation; movement requires the functions from section 7.
9. Loop statements and the range() function
for i in range(5):
print(i)
# range(N) generates 0 … N‑1
# range(M, N) generates M … N‑1Loops are useful for drawing repeated patterns.
10. Wave line drawing example
import turtle
from time import sleep
turtle.setup(650, 350, 200, 200) # set window size and position
turtle.penup()
turtle.fd(-250) # move back without drawing
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("blue")
turtle.seth(-40)
for i in range(4):
turtle.circle(40, 80) # draw downward arc
turtle.circle(-40, 80) # draw upward arc
turtle.done() # keep window open until closed manuallyThe script produces a series of wave‑like arcs.
Appendix: Common turtle operations table
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
