Added section on passing contextual information to logging and documentation for...
[python.git] / Doc / library / turtle.rst
blob30cb7293626699d40aa743b091c782d58a145c0c
2 :mod:`turtle` --- Turtle graphics for Tk
3 ========================================
5 .. module:: turtle
6    :platform: Tk
7    :synopsis: An environment for turtle graphics.
8 .. moduleauthor:: Guido van Rossum <guido@python.org>
11 .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
14 The :mod:`turtle` module provides turtle graphics primitives, in both an
15 object-oriented and procedure-oriented ways. Because it uses :mod:`Tkinter` for
16 the underlying graphics, it needs a version of python installed with Tk support.
18 The procedural interface uses a pen and a canvas which are automagically created
19 when any of the functions are called.
21 The :mod:`turtle` module defines the following functions:
24 .. function:: degrees()
26    Set angle measurement units to degrees.
29 .. function:: radians()
31    Set angle measurement units to radians.
34 .. function:: setup(**kwargs)
36    Sets the size and position of the main window.  Keywords are:
38    * ``width``: either a size in pixels or a fraction of the screen. The default is
39      50% of the screen.
41    * ``height``: either a size in pixels or a fraction of the screen. The default
42      is 50% of the screen.
44    * ``startx``: starting position in pixels from the left edge of the screen.
45      ``None`` is the default value and  centers the window horizontally on screen.
47    * ``starty``: starting position in pixels from the top edge of the screen.
48      ``None`` is the default value and  centers the window vertically on screen.
50    Examples::
52       # Uses default geometry: 50% x 50% of screen, centered.
53       setup()  
55       # Sets window to 200x200 pixels, in upper left of screen
56       setup (width=200, height=200, startx=0, starty=0)
58       # Sets window to 75% of screen by 50% of screen, and centers it.
59       setup(width=.75, height=0.5, startx=None, starty=None)
62 .. function:: title(title_str)
64    Set the window's title to *title*.
67 .. function:: done()
69    Enters the Tk main loop.  The window will continue to  be displayed until the
70    user closes it or the process is killed.
73 .. function:: reset()
75    Clear the screen, re-center the pen, and set variables to the default values.
78 .. function:: clear()
80    Clear the screen.
83 .. function:: tracer(flag)
85    Set tracing on/off (according to whether flag is true or not). Tracing means
86    line are drawn more slowly, with an animation of an arrow along the  line.
89 .. function:: speed(speed)
91    Set the speed of the turtle. Valid values for the parameter *speed* are
92    ``'fastest'`` (no delay), ``'fast'``, (delay 5ms), ``'normal'`` (delay 10ms),
93    ``'slow'`` (delay 15ms), and ``'slowest'`` (delay 20ms).
95    .. versionadded:: 2.5
98 .. function:: delay(delay)
100    Set the speed of the turtle to *delay*, which is given in ms.
102    .. versionadded:: 2.5
105 .. function:: forward(distance)
107    Go forward *distance* steps.
110 .. function:: backward(distance)
112    Go backward *distance* steps.
115 .. function:: left(angle)
117    Turn left *angle* units. Units are by default degrees, but can be set via the
118    :func:`degrees` and :func:`radians` functions.
121 .. function:: right(angle)
123    Turn right *angle* units. Units are by default degrees, but can be set via the
124    :func:`degrees` and :func:`radians` functions.
127 .. function:: up()
129    Move the pen up --- stop drawing.
132 .. function:: down()
134    Move the pen down --- draw when moving.
137 .. function:: width(width)
139    Set the line width to *width*.
142 .. function:: color(s)
143               color((r, g, b))
144               color(r, g, b)
146    Set the pen color.  In the first form, the color is specified as a Tk color
147    specification as a string.  The second form specifies the color as a tuple of
148    the RGB values, each in the range [0..1].  For the third form, the color is
149    specified giving the RGB values as three separate parameters (each in the range
150    [0..1]).
153 .. function:: write(text[, move])
155    Write *text* at the current pen position. If *move* is true, the pen is moved to
156    the bottom-right corner of the text. By default, *move* is false.
159 .. function:: fill(flag)
161    The complete specifications are rather complex, but the recommended  usage is:
162    call ``fill(1)`` before drawing a path you want to fill, and call ``fill(0)``
163    when you finish to draw the path.
166 .. function:: begin_fill()
168    Switch turtle into filling mode;  Must eventually be followed by a corresponding
169    end_fill() call. Otherwise it will be ignored.
171    .. versionadded:: 2.5
174 .. function:: end_fill()
176    End filling mode, and fill the shape; equivalent to ``fill(0)``.
178    .. versionadded:: 2.5
181 .. function:: circle(radius[, extent])
183    Draw a circle with radius *radius* whose center-point is *radius* units left of
184    the turtle. *extent* determines which part of a circle is drawn: if not given it
185    defaults to a full circle.
187    If *extent* is not a full circle, one endpoint of the arc is the current pen
188    position. The arc is drawn in a counter clockwise direction if *radius* is
189    positive, otherwise in a clockwise direction.  In the process, the direction of
190    the turtle is changed by the amount of the *extent*.
193 .. function:: goto(x, y)
194               goto((x, y))
196    Go to co-ordinates *x*, *y*.  The co-ordinates may be specified either as two
197    separate arguments or as a 2-tuple.
200 .. function:: towards(x, y)
202    Return the angle of the line from the turtle's position to the point *x*, *y*.
203    The co-ordinates may be specified either as two separate arguments, as a
204    2-tuple, or as another pen object.
206    .. versionadded:: 2.5
209 .. function:: heading()
211    Return the current orientation of the turtle.
213    .. versionadded:: 2.3
216 .. function:: setheading(angle)
218    Set the orientation of the turtle to *angle*.
220    .. versionadded:: 2.3
223 .. function:: position()
225    Return the current location of the turtle as an ``(x,y)`` pair.
227    .. versionadded:: 2.3
230 .. function:: setx(x)
232    Set the x coordinate of the turtle to *x*.
234    .. versionadded:: 2.3
237 .. function:: sety(y)
239    Set the y coordinate of the turtle to *y*.
241    .. versionadded:: 2.3
244 .. function:: window_width()
246    Return the width of the canvas window.
248    .. versionadded:: 2.3
251 .. function:: window_height()
253    Return the height of the canvas window.
255    .. versionadded:: 2.3
257 This module also does ``from math import *``, so see the documentation for the
258 :mod:`math` module for additional constants and functions useful for turtle
259 graphics.
262 .. function:: demo()
264    Exercise the module a bit.
267 .. exception:: Error
269    Exception raised on any error caught by this module.
271 For examples, see the code of the :func:`demo` function.
273 This module defines the following classes:
276 .. class:: Pen()
278    Define a pen. All above functions can be called as a methods on the given pen.
279    The constructor automatically creates a canvas do be drawn on.
282 .. class:: Turtle()
284    Define a pen. This is essentially a synonym for ``Pen()``; :class:`Turtle` is an
285    empty subclass of :class:`Pen`.
288 .. class:: RawPen(canvas)
290    Define a pen which draws on a canvas *canvas*. This is useful if  you want to
291    use the module to create graphics in a "real" program.
294 .. _pen-rawpen-objects:
296 Turtle, Pen and RawPen Objects
297 ------------------------------
299 Most of the global functions available in the module are also available as
300 methods of the :class:`Turtle`, :class:`Pen` and :class:`RawPen` classes,
301 affecting only the state of the given pen.
303 The only method which is more powerful as a method is :func:`degrees`, which
304 takes an optional argument letting  you specify the number of units
305 corresponding to a full circle:
308 .. method:: Turtle.degrees([fullcircle])
310    *fullcircle* is by default 360. This can cause the pen to have any angular units
311    whatever: give *fullcircle* ``2*pi`` for radians, or 400 for gradians.