Turtle - Turtle Graphics - Python 3.10.6 Documentation
Turtle - Turtle Graphics - Python 3.10.6 Documentation
6 documentation
3.10.6 Go
Introduction
Turtle graphics is a popular way for introducing programming to kids. It was
part of the original Logo programming language developed by Wally Feurzeig,
Seymour Papert and Cynthia
Solomon in 1967.
Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle , 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.right(25) , and it rotates in-place 25 degrees clockwise.
Turtle star
Turtle can draw intricate shapes using programs that repeat simple
moves.
By combining together these and similar commands, intricate shapes and pictures
can easily be drawn.
It tries to keep the merits of the old turtle module and to be (nearly) 100%
compatible with it. This means in the first place to enable the learning
programmer to use all the commands,
classes and methods interactively when using
the module from within IDLE run with the -n switch.
The procedural interface provides functions which are derived from the methods
of the classes Screen and Turtle . They have the same names as
the corresponding methods. A
screen object is automatically created whenever a
function derived from a Screen method is called. An (unnamed) turtle object is
automatically created whenever any of the functions
derived from a Turtle method
is called.
To use multiple turtles on a screen one has to use the object-oriented interface.
Note:
In the following documentation the argument list for functions is given.
Methods, of course, have the additional first argument self which is
omitted here.
Turtle motion
Move and draw
forward() | fd()
backward() | bk() | back()
right() | rt()
left() | lt()
goto() | setpos() | setposition()
setx()
sety()
setheading() | seth()
home()
https://docs.python.org/3/library/turtle.html#turtle.circle 1/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
circle()
3.10.6
dot() Go
stamp()
clearstamp()
clearstamps()
undo()
speed()
Pen control
Drawing state
pendown() | pd() | down()
penup() | pu() | up()
pensize() | width()
pen()
isdown()
Color control
color()
pencolor()
fillcolor()
Filling
filling()
begin_fill()
end_fill()
Turtle state
Visibility
showturtle() | st()
hideturtle() | ht()
isvisible()
Appearance
shape()
resizemode()
shapesize() | turtlesize()
shearfactor()
settiltangle()
tiltangle()
tilt()
shapetransform()
get_shapepoly()
Using events
onclick()
onrelease()
ondrag()
Methods of TurtleScreen/Screen
Window control
bgcolor()
bgpic()
clearscreen()
resetscreen()
screensize()
setworldcoordinates()
Animation control
delay()
tracer()
update()
onkey() | onkeyrelease()
onkeypress()
onclick() | onscreenclick()
ontimer()
mainloop() | done()
Input methods
textinput()
numinput()
Turtle motion
Move the turtle forward by the specified distance, in the direction the
turtle is headed.
Turn turtle right by angle units. (Units are by default degrees, but
can be set via the degrees() and radians() functions.) Angle
orientation depends on the turtle mode, see
mode() .
Turn turtle left by angle units. (Units are by default degrees, but
can be set via the degrees() and radians() functions.) Angle
orientation depends on the turtle mode, see
mode() .
If y is 3.10.6
None , x must be a pair of coordinates or a Vec2D
(e.g. as returned by pos() ).
Go
>>> tp = turtle.pos()
>>> tp
(0.00,0.00)
>>> turtle.setpos(60,30)
>>> turtle.pos()
(60.00,30.00)
>>> turtle.setpos((20,80))
>>> turtle.pos()
(20.00,80.00)
>>> turtle.setpos(tp)
>>> turtle.pos()
(0.00,0.00)
turtle. setx( x)
Parameters: x – a number (integer or float)
turtle. sety( y)
Parameters: y – a number (integer or float)
Set the orientation of the turtle to to_angle. Here are some common
directions in degrees:
0 - east 0 - north
90 - north 90 - east
turtle. home()
Move turtle to the origin – coordinates (0,0) – and set its heading to
its start-orientation (which depends on the mode, see mode() ).
Draw a circle with given radius. The center is radius units left of
the turtle; extent – an angle – determines which part of the circle
is drawn. If extent is not given, draw the entire
circle. If extent
is not a full circle, one endpoint of the arc is the current pen
position. Draw the arc in counterclockwise direction if radius is
positive, otherwise in clockwise direction.
Finally the direction of the
turtle is changed by the amount of extent.
turtle. stamp()
Stamp a copy of the turtle shape onto the canvas at the current turtle
position. Return a stamp_id for that stamp, which can be used to delete
it by calling clearstamp(stamp_id) .
turtle. undo()
Undo (repeatedly) the last turtle action(s). Number of available
undo actions is determined by the size of the undobuffer.
“fastest”: 0
“fast”: 10
“normal”: 6
“slow”: 3
“slowest”: 1
turtle. position()
l ()
https://docs.python.org/3/library/turtle.html#turtle.circle 5/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
turtle. pos()
Return3.10.6
the turtle’s current location (x,y) (as a Vec2D vector). Go
Return the angle between the line from turtle position to position specified
by (x,y), the vector or the other turtle. This depends on the turtle’s start
orientation which depends on the
mode - “standard”/”world” or “logo”.
turtle. xcor()
Return the turtle’s x coordinate.
turtle. ycor()
Return the turtle’s y coordinate.
turtle. heading()
Return the turtle’s current heading (value depends on the turtle mode, see
mode() ).
Return the distance from the turtle to (x,y), the given vector, or the given
other turtle, in turtle step units.
Set angle measurement units, i.e. set number of “degrees” for a full circle.
Default value is 360 degrees.
turtle. radians()
Set the angle measurement units to radians. Equivalent to
degrees(2*math.pi) .
Pen control
Drawing state
https://docs.python.org/3/library/turtle.html#turtle.circle 6/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
Drawing state
3.10.6 Go
turtle. pendown()
turtle. pd()
turtle. down()
Pull the pen down – drawing when moving.
turtle. penup()
turtle. pu()
turtle. up()
Pull the pen up – no drawing when moving.
“shown”: True/False
“pendown”: True/False
“pencolor”: color-string or color-tuple
“fillcolor”: color-string or color-tuple
“pensize”: positive number
“speed”: number in range 0..10
“resizemode”: “auto” or “user” or “noresize”
“stretchfactor”: (positive number, positive number)
“outline”: positive number
“tilt”: number
turtle. isdown()
Return True if pen is down, False if it’s up.
Color control
pencolor()
Return the current pencolor as color specification string or
as a tuple (see example). May be used as input to another
color/pencolor/fillcolor call.
pencolor(colorstring)
Set pencolor to colorstring, which is a Tk color specification string,
such as "red" , "yellow" , or "#33cc8c" .
pencolor((r, g, b))
Set pencolor to the RGB color represented by the tuple of r, g, and
b. Each of r, g, and b must be in the range 0..colormode, where
colormode is either 1.0 or 255 (see
colormode() ).
pencolor(r, g, b)
Set pencolor to the RGB color represented by r, g, and b. Each of
r, g, and b must be in the range 0..colormode.
>>> colormode()
1.0
>>> turtle.pencolor()
'red'
>>> turtle pencolor("brown")
https://docs.python.org/3/library/turtle.html#turtle.circle 7/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
>>> turtle.pencolor( brown )
>>>3.10.6
turtle.pencolor() Go
'brown'
>>> tup = (0.2, 0.8, 0.55)
>>> turtle.pencolor(tup)
>>> turtle.pencolor()
(0.2, 0.8, 0.5490196078431373)
>>> colormode(255)
>>> turtle.pencolor()
(51.0, 204.0, 140.0)
>>> turtle.pencolor('#32c18f')
>>> turtle.pencolor()
(50.0, 193.0, 143.0)
fillcolor()
Return the current fillcolor as color specification string, possibly
in tuple format (see example). May be used as input to another
color/pencolor/fillcolor call.
fillcolor(colorstring)
Set fillcolor to colorstring, which is a Tk color specification string,
such as "red" , "yellow" , or "#33cc8c" .
fillcolor((r, g, b))
Set fillcolor to the RGB color represented by the tuple of r, g, and
b. Each of r, g, and b must be in the range 0..colormode, where
colormode is either 1.0 or 255 (see
colormode() ).
fillcolor(r, g, b)
Set fillcolor to the RGB color represented by r, g, and b. Each of
r, g, and b must be in the range 0..colormode.
>>> turtle.fillcolor("violet")
>>> turtle.fillcolor()
'violet'
>>> turtle.pencolor()
(50.0, 193.0, 143.0)
>>> turtle.fillcolor((50, 193, 143)) # Integers, not floats
>>> turtle.fillcolor()
(50.0, 193.0, 143.0)
>>> turtle.fillcolor('#ffffff')
>>> turtle.fillcolor()
(255.0, 255.0, 255.0)
color()
Return the current pencolor and the current fillcolor as a pair of color
specification strings or tuples as returned by pencolor() and
fillcolor() .
Filling
turtle. filling()
Return fillstate ( True if filling, False else).
>>> turtle.begin_fill()
>>> if turtle.filling():
... turtle.pensize(5)
... else:
... turtle.pensize(3)
turtle. begin_fill()
To be called just before drawing a shape to be filled.
turtle. end_fill()
Fill the shape drawn after the last call to begin_fill() .
3.10.6 Go
turtle. reset()
Delete the turtle’s drawings from the screen, re-center the turtle and set
variables to the default values.
turtle. clear()
Delete the turtle’s drawings from the screen. Do not move turtle. State and
position of the turtle as well as drawings of other turtles are not affected.
Turtle state
Visibility
turtle. hideturtle()
turtle. ht()
Make the turtle invisible. It’s a good idea to do this while you’re in the
middle of doing some complex drawing, because hiding the turtle speeds up the
drawing observably.
turtle. showturtle()
turtle. st()
Make the turtle visible.
turtle. isvisible()
Return True if the Turtle is shown, False if it’s hidden.
Appearance
Set turtle shape to shape with given name or, if name is not given, return
name of current shape. Shape with name must exist in the TurtleScreen’s
shape dictionary. Initially there
are the following polygon shapes: “arrow”,
“turtle”, “circle”, “square”, “triangle”, “classic”. To learn about how to
deal with shapes see Screen method register_shape() .
“auto”: adapts the appearance of the turtle corresponding to the value of pensize.
“user”: adapts the appearance of the turtle according to the values of
stretchfactor and outlinewidth (outline), which are set by
shapesize() .
“noresize”: no adaption of the turtle’s appearance takes place.
https://docs.python.org/3/library/turtle.html#turtle.circle 9/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
Return or set the pen’s attributes x/y-stretchfactors and/or outline. Set
resizemode to “user”. If and only if resizemode is set to “user”, the turtle
will be displayed stretched according
3.10.6 Go
to its stretchfactors: stretch_wid is
stretchfactor perpendicular to its orientation, stretch_len is
stretchfactor in direction of its orientation, outline determines the width
of the shapes’s
outline.
>>> turtle.shape("circle")
>>> turtle.shapesize(5,2)
>>> turtle.shearfactor(0.5)
>>> turtle.shearfactor()
0.5
Rotate the turtleshape by angle from its current tilt-angle, but do not
change the turtle’s heading (direction of movement).
turtle. get_shapepoly()
Return the current shape polygon as tuple of coordinate pairs. This
can be used to define a new shape or components of a compound shape.
Using events
https://docs.python.org/3/library/turtle.html#turtle.circle 10/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
turtle. onclick(
3.10.6 fun, btn=1, add=None) Go
Parameters: fun – a function with two arguments which will be called with the
coordinates of the clicked point on the canvas
btn – number of the mouse-button, defaults to 1 (left mouse button)
add – True or False – if True , a new binding will be
added, otherwise it will replace a former binding
turtle. begin_poly()
Start recording the vertices of a polygon. Current turtle position is first
vertex of polygon.
turtle. end_poly()
Stop recording the vertices of a polygon. Current turtle position is last
vertex of polygon. This will be connected with the first vertex.
turtle. get_poly()
Return the last recorded polygon.
turtle. clone()
Create and return a clone of the turtle with same position, heading and
turtle properties.
turtle. getturtle()
turtle. getpen()
Return the Turtle object itself. Only reasonable use: as a function to
return the “anonymous turtle”:
turtle. getscreen()
Return the TurtleScreen object the turtle is drawing on.
TurtleScreen methods can then be called for that object.
https://docs.python.org/3/library/turtle.html#turtle.circle 11/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
>>> 3.10.6
turtle.setundobuffer(42) >>>
Go
turtle. undobufferentries()
Return number of entries in the undobuffer.
Compound shapes
For example:
3. Now add the Shape to the Screen’s shapelist and use it:
Note:
The Shape class is used internally by the register_shape()
method in different ways. The application programmer has to deal with the
Shape class only when using
compound shapes like shown above!
Window control
turtle. clear()
Note:
This TurtleScreen method is available as a global function only under the
name clearscreen . The global function clear is a different one
derived from the Turtle
method clear .
turtle. clearscreen()
Delete all drawings and all turtles from the TurtleScreen. Reset the now
empty TurtleScreen to its initial state: white background, no background
image, no event bindings and
tracing on.
turtle. reset()
Note:
This TurtleScreen method is available as a global function only under the
name resetscreen . The global function reset is another one
derived from the Turtle method
reset .
turtle. resetscreen()
Reset all Turtles on the Screen to their initial state.
t hf l d t tl )
https://docs.python.org/3/library/turtle.html#turtle.circle 12/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
e.g. to search for an erroneously escaped turtle ;-)
3.10.6 Go
Animation control
Optional argument:
Turn turtle animation on/off and set delay for update drawings. If
n is given, only each n-th regular screen update is really
performed. (Can be used to accelerate the drawing of
complex
graphics.) When called without arguments, returns the currently
stored value of n. Second argument sets delay value (see
delay() ).
turtle. update()
Perform a TurtleScreen update. To be used when tracer is turned off.
https://docs.python.org/3/library/turtle.html#turtle.circle 13/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
3.10.6
Bind fun to mouse-click events on this screen. If fun is None ,
existing bindings are removed. Go
Note:
This TurtleScreen method is available as a global function only under the
name onscreenclick . The global function onclick is another one
derived from the Turtle
method onclick .
turtle. mainloop()
turtle. done()
Starts event loop - calling Tkinter’s mainloop function.
Must be the last statement in a turtle graphics program.
Must not be used if a script is run from within IDLE in -n mode
(No
subprocess) - for interactive use of turtle graphics.
Input methods
Pop up a dialog window for input of a number. title is the title of the
dialog window, prompt is a text mostly describing what numerical information
to input. default: default value,
minval: minimum value for input,
maxval: maximum value for input
The number input must be in the range minval .. maxval if these are
given. If not, a hint is issued and the dialog
remains open for
correction.
Return the number input. If the dialog is canceled, return None .
Set turtle mode (“standard”, “logo” or “world”) and perform reset. If mode
is not given, current mode is returned.
turtle. getcanvas()
Return the Canvas of this TurtleScreen. Useful for insiders who know what to
do with a Tkinter Canvas.
turtle. getshapes()
Return a list of names of all currently available turtle shapes.
Note:
Image shapes do not rotate when turning the turtle, so they do not
display the heading of the turtle!
turtle. turtles()
Return the list of turtles on the screen.
turtle. window_height()
Return the height of the turtle window.
turtle. window_width()
Return the width of the turtle window.
turtle. bye()
Shut the turtlegraphics window.
turtle. exitonclick()
Bind bye() method to mouse clicks on the Screen.
Public classes
Create a turtle. The turtle has all methods described above as “methods of
Turtle/RawTurtle”.
Provides screen oriented methods like setbg() etc. that are described
above.
Data structure modeling shapes. The pair (type_, data) must follow this
specification:
type_ data
Example:
a + b vector addition
a - b vector subtraction
a * b inner product
k * a and a * k multiplication with scalar
abs(a) absolute value of a
a.rotate(angle) rotation
The public methods of the Screen and Turtle classes are documented extensively
via docstrings. So these can be used as online-help via the Python help
facilities:
When using IDLE, tooltips show the signatures and first lines of the
docstrings of typed in function-/method calls.
>>> screen.bgcolor("orange")
>>> screen.bgcolor()
"orange"
>>> screen.bgcolor(0.5,0,0.5)
>>> screen.bgcolor()
"#800080"
>>> help(Turtle.penup)
Help on method penup in module turtle:
Aliases: penup | pu | up
No argument
>>> turtle.penup()
The docstrings of the functions which are derived from methods have a modified
form:
https://docs.python.org/3/library/turtle.html#turtle.circle 16/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
Help on function bgcolor in module turtle:
3.10.6 Go
bgcolor(*args)
Set or return backgroundcolor of the TurtleScreen.
Example::
>>> bgcolor("orange")
>>> bgcolor()
"orange"
>>> bgcolor(0.5,0,0.5)
>>> bgcolor()
"#800080"
>>> help(penup)
Help on function penup in module turtle:
penup()
Pull the pen up -- no drawing when moving.
Aliases: penup | pu | up
No argument
Example:
>>> penup()
These modified docstrings are created automatically together with the function
definitions that are derived from the methods at import time.
There is a utility to create a dictionary the keys of which are the method names
and the values of which are the docstrings of the public methods of the classes
Screen and Turtle.
If you (or your students) want to use turtle with online help in your
native language, you have to translate the docstrings and save the resulting
file as e.g.
turtle_docstringdict_german.py .
At the time of this writing there are docstring dictionaries in German and in
Italian. (Requests please to [email protected].)
The built-in default configuration mimics the appearance and behaviour of the
old turtle module in order to retain best possible compatibility with it.
If you want to use a different configuration which better reflects the features
of this module or which better fits to your needs, e.g. for use in a classroom,
you can prepare a configuration
file turtle.cfg which will be read at import
time and modify the configuration according to its settings.
width = 0.5
height = 0.75
leftright = None
topbottom = None
canvwidth = 400
canvheight = 300
mode = standard
colormode = 1.0
delay = 10
undobuffersize = 1000
shape = classic
pencolor = black
fillcolor = black
resizemode = noresize
visible = True
language = english
exampleturtle = turtle
examplescreen = screen
title = Python Turtle Graphics
using_IDLE = False
https://docs.python.org/3/library/turtle.html#turtle.circle 17/18
4/9/22, 22:03 turtle — Turtle graphics — Python 3.10.6 documentation
3.10.6
python -m turtledemo Go
Alternatively, you can run the demo scripts individually. For example,
python -m turtledemo.bytedesign
lindenmayer ethnomathematics
(indian kolams) L-System
planet_and_moon simulation of
gravitational system compound shapes,
Vec2D
Have fun!
https://docs.python.org/3/library/turtle.html#turtle.circle 18/18