The turtle screen: Screen(), speed() and tracer()
turtle.Screen() gives you the turtle window itself. Use it to set the
window's title, size and colour, to control drawing speed, and to hold several
turtles.
Get the screen
import turtle
screen = turtle.Screen()
screen.title("My drawing")
screen.bgcolor("light yellow")
print(screen.window_width(), screen.window_height())
There is only one window. Every call to turtle.Screen() gives you the same
one.
Change the window size with setup()
setup(width, height) sets the window size in pixels. The centre stays at
(0, 0).
import turtle
screen = turtle.Screen()
screen.setup(400, 300)
t = turtle.Turtle()
t.goto(150, 100)
print(screen.window_width(), screen.window_height())
Draw faster with speed() and tracer()
speed() sets how fast a turtle moves: 1 is slowest, 10 is fast and 0
is fastest. For a big drawing, tracer(0) turns the animation off. Draw
everything, then call update() to show it all at once.
import turtle
screen = turtle.Screen()
screen.tracer(0)
t = turtle.Turtle()
for i in range(200):
t.forward(i)
t.left(59)
screen.update()
Without update(), the window stays blank.
Use several turtles
Each turtle.Turtle() is a new turtle with its own position, heading and pen.
They all draw on the same screen.
import turtle
screen = turtle.Screen()
red = turtle.Turtle()
red.pencolor("red")
blue = turtle.Turtle()
blue.pencolor("blue")
red.forward(100)
blue.left(90)
blue.forward(100)
print(len(screen.turtles()))
screen.turtles() is a list of every turtle on the screen.
Note: On this page you see the finished drawing, so speed() changes
nothing here; in the Python editor you watch it. Mouse and key events
(onclick(), onkey(), ontimer()) don't run in the browser yet, so use them
in Python on your own computer.
See also: Turtle colours, Drawing shapes with turtle loops.
Your turn
Add three lines: make a second turtle called b, turn it left 90, then move
it forward 100.
import turtle
screen = turtle.Screen()
a = turtle.Turtle()
a.forward(100)
# add three lines: make turtle b, turn it left 90, move it forward 100
print(len(screen.turtles()))
print(b.position())
Expected output
2
(0.00,100.00)