On this page
turtle
Prev: tty | Up: stdlib_module | Next: turtledemo
Rel: wave, colorsys, gettext, locale, turtledemo
Ref: docs.python.org — turtle
Tags: #public #python3 #stdlib
Import
import turtle
Overview
Logo-style vector graphics and educational drawing library built on top of tkinter (Turtle, Screen, forward(), right(), penup(), pendown()). Use turtle for teaching programming concepts, visualizing recursive fractals/L-systems, or sketching quick 2D geometric animations with zero third-party installs.
Quick Start
# Run in a desktop environment with Tkinter available:
import turtle
def draw_square(size: int = 100) -> None:
t = turtle.Turtle()
for _ in range(4):
t.forward(size)
t.right(90)
turtle.done()
Implementation Walkthrough
1. Speeding Up Complex Drawings (tracer(0) + update())
By default, turtle animates every single step. Disable live animation with screen.tracer(0) and call screen.update() once at the end to render fractals or hundreds of shapes instantaneously:
import turtle
def draw_star(points: int = 5, length: int = 120) -> None:
screen = turtle.Screen()
screen.tracer(0)
t = turtle.Turtle()
for _ in range(points):
t.forward(length)
t.right(180 - 180 / points)
screen.update()
turtle.done()
Gotchas & Boundaries
- Requires a GUI display & tkinter: Calling
turtle.Screen()orturtle.Turtle()on a headless server without a display raises_tkinter.TclError. - Not a game or charting engine: For interactive 2D games use
pygame/arcade; for data plots usematplotlib. - Keep
turtle.done()at the end: Withoutturtle.done()(orscreen.mainloop()), the drawing window closes immediately when the script finishes.
Common Functions
- turtle.backward() —
backward(distance)— Move the turtle backward by distance. - turtle.forward() —
forward(distance)— Move the turtle forward by the specified distance. - turtle.left() —
left(angle)— Turn turtle left by angle units. - turtle.pendown() —
pendown()— Pull the pen down -- drawing when moving. - turtle.penup() —
penup()— Pull the pen up -- no drawing when moving. - turtle.right() —
right(angle)— Turn turtle right by angle units.
Classes & Objects
- turtle.Screen — Return the singleton TurtleScreen object. (Methods:
.bgcolor(),.title(),.setup(),.mainloop(),.exitonclick()) - turtle.Turtle — RawTurtle auto-creating a TurtleScreen. (Methods:
.forward(),.backward(),.right(),.left(),.goto(),.circle())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cmd
- tuts/python3/stdlib_module/colorsys
- tuts/python3/stdlib_module/gettext
- tuts/python3/stdlib_module/idlelib
- tuts/python3/stdlib_module/locale
- tuts/python3/stdlib_module/shlex
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tkinter
- tuts/python3/stdlib_module/tty
- tuts/python3/stdlib_module/turtledemo
- tuts/python3/stdlib_module/wave
- tuts/python3/tutorials/popular_application_domains/desktop_gui_development
Related notes
- tuts/python3/stdlib_module/colorsys Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gettext Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/locale Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tkinter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tty Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/turtledemo Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/wave Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmd links to this page; shared tags: #stdlib