Module turtle
[hide private]
[frames] | no frames]

Module turtle

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig and Seymour Papert in 1966.

Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.left(25), and it rotates in-place 25 degrees clockwise.

By combining together these and similar commands, intricate shapes and pictures can easily be drawn.

Classes [hide private]
Error
RawPen
Pen
Turtle
Functions [hide private]
 
_getpen()
 
degrees()
Set angle measurement units to degrees.
 
radians()
Set the angle measurement units to radians.
 
reset()
Clear the screen, re-center the pen, and set variables to the default values.
 
clear()
Clear the screen.
 
tracer(flag)
Set tracing on if flag is True, and off if it is False.
 
forward(distance)
Go forward distance steps.
 
backward(distance)
Go backwards distance steps.
 
left(angle)
Turn left angle units (units are by default degrees, but can be set via the degrees() and radians() functions.)
 
right(angle)
Turn right angle units (units are by default degrees, but can be set via the degrees() and radians() functions.)
 
up()
Pull the pen up -- no drawing when moving.
 
down()
Put the pen down -- draw when moving.
 
width(width)
Set the line to thickness to width.
 
color(*args)
Set the pen color.
 
write(arg, move=0)
Write text at the current pen position.
 
fill(flag)
Call fill(1) before drawing the shape you want to fill, and fill(0) when done.
 
begin_fill()
Called just before drawing a shape to be filled.
 
end_fill()
Called after drawing a shape to be filled.
 
circle(radius, extent=None)
Draw a circle with given radius.
 
goto(*args)
Go to the given point.
 
heading()
Return the turtle's current heading.
 
setheading(angle)
Set the turtle facing the given angle.
 
position()
Return the current (x, y) location of the turtle.
 
window_width()
Returns the width of the turtle window.
 
window_height()
Return the height of the turtle window.
 
setx(xpos)
Set the turtle's x coordinate to be xpos.
 
sety(ypos)
Set the turtle's y coordinate to be ypos.
 
towards(*args)
Returs the angle, which corresponds to the line from turtle-position to point (x,y).
 
done()
 
delay(delay)
Set the drawing delay in milliseconds.
 
speed(speed)
Set the turtle's speed.
 
setup(**geometry)
Sets the size and position of the main window.
 
title(title)
Set the window title.
 
demo()
 
demo2()
Variables [hide private]
  speeds = ['fastest', 'fast', 'normal', 'slow', 'slowest']
  _root = None
  _canvas = None
  _pen = None
  _width = 0.5
  _height = 0.75
  _startx = None
  _starty = None
  _title = 'Turtle Graphics'
  e = 2.71828182846
  methodname = 'write'
  pi = 3.14159265359

Imports: Tkinter, acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor, fmod, frexp, hypot, ldexp, log, log10, modf, pow, sin, sinh, sqrt, tan, tanh


Function Details [hide private]

degrees()

 

Set angle measurement units to degrees.

Example: >>> turtle.degrees()

radians()

 

Set the angle measurement units to radians.

Example: >>> turtle.radians()

reset()

 

Clear the screen, re-center the pen, and set variables to the default values.

Example: >>> turtle.position() [0.0, -22.0] >>> turtle.heading() 100.0 >>> turtle.reset() >>> turtle.position() [0.0, 0.0] >>> turtle.heading() 0.0

clear()

 

Clear the screen. The turtle does not move.

Example: >>> turtle.clear()

tracer(flag)

 

Set tracing on if flag is True, and off if it is False. Tracing means line are drawn more slowly, with an animation of an arrow along the line.

Example: >>> turtle.tracer(False) # turns off Tracer

forward(distance)

 

Go forward distance steps.

Example: >>> turtle.position() [0.0, 0.0] >>> turtle.forward(25) >>> turtle.position() [25.0, 0.0] >>> turtle.forward(-75) >>> turtle.position() [-50.0, 0.0]

backward(distance)

 

Go backwards distance steps.

The turtle's heading does not change.

Example: >>> turtle.position() [0.0, 0.0] >>> turtle.backward(30) >>> turtle.position() [-30.0, 0.0]

left(angle)

 

Turn left angle units (units are by default degrees, but can be set via the degrees() and radians() functions.)

When viewed from above, the turning happens in-place around its front tip.

Example: >>> turtle.heading() 22 >>> turtle.left(45) >>> turtle.heading() 67.0

right(angle)

 

Turn right angle units (units are by default degrees, but can be set via the degrees() and radians() functions.)

When viewed from above, the turning happens in-place around its front tip.

Example: >>> turtle.heading() 22 >>> turtle.right(45) >>> turtle.heading() 337.0

up()

 

Pull the pen up -- no drawing when moving.

Example: >>> turtle.up()

down()

 

Put the pen down -- draw when moving.

Example: >>> turtle.down()

width(width)

 

Set the line to thickness to width.

Example: >>> turtle.width(10)

color(*args)

 
Set the pen color.

Three input formats are allowed:

    color(s)
    s is a Tk specification string, such as "red" or "yellow"

    color((r, g, b))
    *a tuple* of r, g, and b, which represent, an RGB color,
    and each of r, g, and b are in the range [0..1]

    color(r, g, b)
    r, g, and b represent an RGB color, and each of r, g, and b
    are in the range [0..1]

Example:

>>> turtle.color('brown')
>>> tup = (0.2, 0.8, 0.55)
>>> turtle.color(tup)
>>> turtle.color(0, .5, 0)

write(arg, move=0)

 

Write text at the current pen position.

If move is true, the pen is moved to the bottom-right corner of the text. By default, move is False.

Example: >>> turtle.write('The race is on!') >>> turtle.write('Home = (0, 0)', True)

fill(flag)

 
Call fill(1) before drawing the shape you
 want to fill, and fill(0) when done.

Example:
>>> turtle.fill(1)
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)
>>> turtle.fill(0)

begin_fill()

 
Called just before drawing a shape to be filled.
    Must eventually be followed by a corresponding end_fill() call.
    Otherwise it will be ignored.

Example:
>>> turtle.begin_fill()
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)
>>> turtle.end_fill()

end_fill()

 

Called after drawing a shape to be filled.

Example: >>> turtle.begin_fill() >>> turtle.forward(100) >>> turtle.left(90) >>> turtle.forward(100) >>> turtle.left(90) >>> turtle.forward(100) >>> turtle.left(90) >>> turtle.forward(100) >>> turtle.end_fill()

circle(radius, extent=None)

 

Draw a circle with given radius. The center is radius units left of the turtle; extent determines which part of the circle is drawn. If not given, the entire circle is drawn.

If extent is not a full circle, one endpoint of the arc is the current pen position. The arc is drawn in a counter clockwise direction if radius is positive, otherwise in a clockwise direction. In the process, the direction of the turtle is changed by the amount of the extent.

>>> turtle.circle(50)
>>> turtle.circle(120, 180)  # half a circle

goto(*args)

 
Go to the given point.

If the pen is down, then a line will be drawn. The turtle's
orientation does not change.

Two input formats are accepted:

   goto(x, y)
   go to point (x, y)

   goto((x, y))
   go to point (x, y)

Example:
>>> turtle.position()
[0.0, 0.0]
>>> turtle.goto(50, -45)
>>> turtle.position()
[50.0, -45.0]

heading()

 

Return the turtle's current heading.

Example: >>> turtle.heading() 67.0

setheading(angle)

 
Set the turtle facing the given angle.

Here are some common directions in degrees:

   0 - east
  90 - north
 180 - west
 270 - south

Example:
>>> turtle.setheading(90)
>>> turtle.heading()
90
>>> turtle.setheading(128)
>>> turtle.heading()
128

position()

 

Return the current (x, y) location of the turtle.

Example: >>> turtle.position() [0.0, 240.0]

window_width()

 

Returns the width of the turtle window.

Example: >>> turtle.window_width() 640

window_height()

 

Return the height of the turtle window.

Example: >>> turtle.window_height() 768

setx(xpos)

 

Set the turtle's x coordinate to be xpos.

Example: >>> turtle.position() [10.0, 240.0] >>> turtle.setx(10) >>> turtle.position() [10.0, 240.0]

sety(ypos)

 

Set the turtle's y coordinate to be ypos.

Example: >>> turtle.position() [0.0, 0.0] >>> turtle.sety(-22) >>> turtle.position() [0.0, -22.0]

towards(*args)

 

Returs the angle, which corresponds to the line from turtle-position to point (x,y).

Argument can be two coordinates or one pair of coordinates or a RawPen/Pen instance.

Example: >>> turtle.position() [10.0, 10.0] >>> turtle.towards(0,0) 225.0

delay(delay)

 

Set the drawing delay in milliseconds.

This is intended to allow finer control of the drawing speed than the speed() method

Example: >>> turtle.delay(15)

speed(speed)

 
Set the turtle's speed.

speed must one of these five strings:

    'fastest' is a 0 ms delay
    'fast' is a 5 ms delay
    'normal' is a 10 ms delay
    'slow' is a 15 ms delay
    'slowest' is a 20 ms delay

 Example:
 >>> turtle.speed('slow')

setup(**geometry)

 
Sets the size and position of the main window.

Keywords are width, height, startx and starty:

width: either a size in pixels or a fraction of the screen.
  Default is 50% of screen.
height: either the height in pixels or a fraction of the screen.
  Default is 75% of screen.

Setting either width or height to None before drawing will force
  use of default geometry as in older versions of turtle.py

startx: starting position in pixels from the left edge of the screen.
  Default is to center window. Setting startx to None is the default
  and centers window horizontally on screen.

starty: starting position in pixels from the top edge of the screen.
  Default is to center window. Setting starty to None is the default
  and centers window vertically on screen.

Examples:
>>> setup (width=200, height=200, startx=0, starty=0)

sets window to 200x200 pixels, in upper left of screen

>>> setup(width=.75, height=0.5, startx=None, starty=None)

sets window to 75% of screen by 50% of screen and centers

>>> setup(width=None)

forces use of default geometry as in older versions of turtle.py

title(title)

 

Set the window title.

By default this is set to 'Turtle Graphics'

Example: >>> title("My Window")