blob: 6b9585f0019c766291ecfcbf2132cea0b5f00077 [file] [log] [blame]
Fred Drake55e93961999-11-15 17:03:41 +00001\section{\module{turtle} ---
2 Turtle graphics for Tk}
3
4\declaremodule{standard}{turtle}
5 \platform{Tk}
6\moduleauthor{Guido van Rossum}{guido@python.org}
7\modulesynopsis{An environment for turtle graphics.}
8
Fred Drake57657bc2000-12-01 15:25:23 +00009\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
Fred Drake55e93961999-11-15 17:03:41 +000010
11
12The \module{turtle} module provides turtle graphics primitives, in both an
13object-oriented and procedure-oriented ways. Because it uses \module{Tkinter}
14for the underlying graphics, it needs a version of python installed with
15Tk support.
16
17The procedural interface uses a pen and a canvas which are automagically
18created when any of the functions are called.
19
20The \module{turtle} module defines the following functions:
21
22\begin{funcdesc}{degrees}{}
23Set angle measurement units to degrees.
24\end{funcdesc}
25
26\begin{funcdesc}{radians}{}
27Set angle measurement units to radians.
28\end{funcdesc}
29
30\begin{funcdesc}{reset}{}
31Clear the screen, re-center the pen, and set variables to the default
32values.
33\end{funcdesc}
34
35\begin{funcdesc}{clear}{}
36Clear the screen.
37\end{funcdesc}
38
39\begin{funcdesc}{tracer}{flag}
40Set tracing on/off (according to whether flag is true or not). Tracing
41means line are drawn more slowly, with an animation of an arrow along the
42line.
43\end{funcdesc}
44
Martin v. Löwis82c276e2006-07-03 11:12:06 +000045\begin{funcdesc}{speed}{speed}
46Set the speed of the turtle. Valid values for the parameter
47\var{speed} are \code{'fastest'} (no delay), \code{'fast'},
48(delay 5ms), \code{'normal'} (delay 10ms), \code{'slow'}
49(delay 15ms), and \code{'slowest'} (delay 20ms).
50\versionadded{2.5}
51\end{funcdesc}
52
53\begin{funcdesc}{delay}{delay}
54Set the speed of the turtle to \var{delay}, which is given
55in ms. \versionadded{2.5}
56\end{funcdesc}
57
Fred Drake55e93961999-11-15 17:03:41 +000058\begin{funcdesc}{forward}{distance}
59Go forward \var{distance} steps.
60\end{funcdesc}
61
62\begin{funcdesc}{backward}{distance}
63Go backward \var{distance} steps.
64\end{funcdesc}
65
66\begin{funcdesc}{left}{angle}
67Turn left \var{angle} units. Units are by default degrees, but can be
68set via the \function{degrees()} and \function{radians()} functions.
69\end{funcdesc}
70
71\begin{funcdesc}{right}{angle}
72Turn right \var{angle} units. Units are by default degrees, but can be
73set via the \function{degrees()} and \function{radians()} functions.
74\end{funcdesc}
75
76\begin{funcdesc}{up}{}
77Move the pen up --- stop drawing.
78\end{funcdesc}
79
80\begin{funcdesc}{down}{}
Raymond Hettingerff6dd0b2003-12-06 01:35:56 +000081Move the pen down --- draw when moving.
Fred Drake55e93961999-11-15 17:03:41 +000082\end{funcdesc}
83
84\begin{funcdesc}{width}{width}
85Set the line width to \var{width}.
86\end{funcdesc}
87
88\begin{funcdesc}{color}{s}
Fred Drake482b9a82001-11-15 20:41:03 +000089\funclineni{color}{(r, g, b)}
90\funclineni{color}{r, g, b}
91Set the pen color. In the first form, the color is specified as a
92Tk color specification as a string. The second form specifies the
93color as a tuple of the RGB values, each in the range [0..1]. For the
94third form, the color is specified giving the RGB values as three
95separate parameters (each in the range [0..1]).
Fred Drake55e93961999-11-15 17:03:41 +000096\end{funcdesc}
97
98\begin{funcdesc}{write}{text\optional{, move}}
99Write \var{text} at the current pen position. If \var{move} is true,
100the pen is moved to the bottom-right corner of the text. By default,
101\var{move} is false.
102\end{funcdesc}
103
104\begin{funcdesc}{fill}{flag}
105The complete specifications are rather complex, but the recommended
106usage is: call \code{fill(1)} before drawing a path you want to fill,
107and call \code{fill(0)} when you finish to draw the path.
108\end{funcdesc}
109
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000110\begin{funcdesc}{begin\_fill}{}
111Switch turtle into filling mode; equivalent to \code{fill(1)}.
112\versionadded{2.5}
113\end{funcdesc}
114
115\begin{funcdesc}{end\_fill}{}
116End filling mode, and fill the shape; equivalent to \code{fill(0)}.
117\versionadded{2.5}
118\end{funcdesc}
119
Fred Drake55e93961999-11-15 17:03:41 +0000120\begin{funcdesc}{circle}{radius\optional{, extent}}
Raymond Hettingera97e4f32003-02-21 03:14:08 +0000121Draw a circle with radius \var{radius} whose center-point is
122\var{radius} units left of the turtle.
123\var{extent} determines which part of a circle is drawn: if
Fred Drake55e93961999-11-15 17:03:41 +0000124not given it defaults to a full circle.
125
126If \var{extent} is not a full circle, one endpoint of the arc is the
127current pen position. The arc is drawn in a counter clockwise
128direction if \var{radius} is positive, otherwise in a clockwise
Raymond Hettingera97e4f32003-02-21 03:14:08 +0000129direction. In the process, the direction of the turtle is changed
130by the amount of the \var{extent}.
Fred Drake55e93961999-11-15 17:03:41 +0000131\end{funcdesc}
132
133\begin{funcdesc}{goto}{x, y}
Fred Drake482b9a82001-11-15 20:41:03 +0000134\funclineni{goto}{(x, y)}
135Go to co-ordinates \var{x}, \var{y}. The co-ordinates may be
136specified either as two separate arguments or as a 2-tuple.
Fred Drake55e93961999-11-15 17:03:41 +0000137\end{funcdesc}
138
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000139\begin{funcdesc}{towards}{x, y}
140Return the angle of the line from the turtle's position
141to the point \var{x}, \var{y}. The co-ordinates may be
142specified either as two separate arguments, as a 2-tuple,
143or as another pen object.
144\versionadded{2.5}
145\end{funcdesc}
146
147\begin{funcdesc}{heading}{}
148Return the current orientation of the turtle.
149\versionadded{2.3}
150\end{funcdesc}
151
152\begin{funcdesc}{setheading}{angle}
153Set the orientation of the turtle to \var{angle}.
154\versionadded{2.3}
155\end{funcdesc}
156
157\begin{funcdesc}{position}{}
158Return the current location of the turtle as an \code{(x,y)} pair.
159\versionadded{2.3}
160\end{funcdesc}
161
162\begin{funcdesc}{setx}{x}
163Set the x coordinate of the turtle to \var{x}.
164\versionadded{2.3}
165\end{funcdesc}
166
167\begin{funcdesc}{sety}{y}
168Set the y coordinate of the turtle to \var{y}.
169\versionadded{2.3}
170\end{funcdesc}
171
172\begin{funcdesc}{window\_width}{}
173Return the width of the canvas window.
174\versionadded{2.3}
175\end{funcdesc}
176
177\begin{funcdesc}{window\_height}{}
178Return the height of the canvas window.
179\versionadded{2.3}
180\end{funcdesc}
181
Fred Drake74242421999-11-17 16:09:57 +0000182This module also does \code{from math import *}, so see the
183documentation for the \refmodule{math} module for additional constants
184and functions useful for turtle graphics.
Fred Drake55e93961999-11-15 17:03:41 +0000185
186\begin{funcdesc}{demo}{}
187Exercise the module a bit.
188\end{funcdesc}
189
190\begin{excdesc}{Error}
191Exception raised on any error caught by this module.
192\end{excdesc}
193
194For examples, see the code of the \function{demo()} function.
195
196This module defines the following classes:
197
198\begin{classdesc}{Pen}{}
199Define a pen. All above functions can be called as a methods on the given
200pen. The constructor automatically creates a canvas do be drawn on.
201\end{classdesc}
202
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000203\begin{classdesc}{Turtle}{}
204Define a pen. This is essentially a synonym for \code{Pen()};
205\class{Turtle} is an empty subclass of \class{Pen}.
206\end{classdesc}
207
Fred Drake55e93961999-11-15 17:03:41 +0000208\begin{classdesc}{RawPen}{canvas}
209Define a pen which draws on a canvas \var{canvas}. This is useful if
210you want to use the module to create graphics in a ``real'' program.
211\end{classdesc}
212
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000213\subsection{Turtle, Pen and RawPen Objects \label{pen-rawpen-objects}}
Fred Drake55e93961999-11-15 17:03:41 +0000214
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000215\class{Turtle}, \class{Pen} and \class{RawPen} objects have all the
216global functions described above, except for \function{demo()} as
217methods, which manipulate the given pen.
Fred Drake55e93961999-11-15 17:03:41 +0000218
219The only method which is more powerful as a method is
220\function{degrees()}.
221
222\begin{methoddesc}{degrees}{\optional{fullcircle}}
223\var{fullcircle} is by default 360. This can cause the pen to have any
224angular units whatever: give \var{fullcircle} 2*$\pi$ for radians, or
225400 for gradians.
226\end{methoddesc}