blob: 2b329b7209e2f6c2a2073dba33d97e29588ea835 [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}{}
Martin v. Löwis06c68b82006-07-10 22:11:28 +0000111Switch turtle into filling mode;
112Must eventually be followed by a corresponding end_fill() call.
113Otherwise it will be ignored.
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000114\versionadded{2.5}
115\end{funcdesc}
116
117\begin{funcdesc}{end\_fill}{}
118End filling mode, and fill the shape; equivalent to \code{fill(0)}.
119\versionadded{2.5}
120\end{funcdesc}
121
Fred Drake55e93961999-11-15 17:03:41 +0000122\begin{funcdesc}{circle}{radius\optional{, extent}}
Raymond Hettingera97e4f32003-02-21 03:14:08 +0000123Draw a circle with radius \var{radius} whose center-point is
124\var{radius} units left of the turtle.
125\var{extent} determines which part of a circle is drawn: if
Fred Drake55e93961999-11-15 17:03:41 +0000126not given it defaults to a full circle.
127
128If \var{extent} is not a full circle, one endpoint of the arc is the
129current pen position. The arc is drawn in a counter clockwise
130direction if \var{radius} is positive, otherwise in a clockwise
Raymond Hettingera97e4f32003-02-21 03:14:08 +0000131direction. In the process, the direction of the turtle is changed
132by the amount of the \var{extent}.
Fred Drake55e93961999-11-15 17:03:41 +0000133\end{funcdesc}
134
135\begin{funcdesc}{goto}{x, y}
Fred Drake482b9a82001-11-15 20:41:03 +0000136\funclineni{goto}{(x, y)}
137Go to co-ordinates \var{x}, \var{y}. The co-ordinates may be
138specified either as two separate arguments or as a 2-tuple.
Fred Drake55e93961999-11-15 17:03:41 +0000139\end{funcdesc}
140
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000141\begin{funcdesc}{towards}{x, y}
142Return the angle of the line from the turtle's position
143to the point \var{x}, \var{y}. The co-ordinates may be
144specified either as two separate arguments, as a 2-tuple,
145or as another pen object.
146\versionadded{2.5}
147\end{funcdesc}
148
149\begin{funcdesc}{heading}{}
150Return the current orientation of the turtle.
151\versionadded{2.3}
152\end{funcdesc}
153
154\begin{funcdesc}{setheading}{angle}
155Set the orientation of the turtle to \var{angle}.
156\versionadded{2.3}
157\end{funcdesc}
158
159\begin{funcdesc}{position}{}
160Return the current location of the turtle as an \code{(x,y)} pair.
161\versionadded{2.3}
162\end{funcdesc}
163
164\begin{funcdesc}{setx}{x}
165Set the x coordinate of the turtle to \var{x}.
166\versionadded{2.3}
167\end{funcdesc}
168
169\begin{funcdesc}{sety}{y}
170Set the y coordinate of the turtle to \var{y}.
171\versionadded{2.3}
172\end{funcdesc}
173
174\begin{funcdesc}{window\_width}{}
175Return the width of the canvas window.
176\versionadded{2.3}
177\end{funcdesc}
178
179\begin{funcdesc}{window\_height}{}
180Return the height of the canvas window.
181\versionadded{2.3}
182\end{funcdesc}
183
Fred Drake74242421999-11-17 16:09:57 +0000184This module also does \code{from math import *}, so see the
185documentation for the \refmodule{math} module for additional constants
186and functions useful for turtle graphics.
Fred Drake55e93961999-11-15 17:03:41 +0000187
188\begin{funcdesc}{demo}{}
189Exercise the module a bit.
190\end{funcdesc}
191
192\begin{excdesc}{Error}
193Exception raised on any error caught by this module.
194\end{excdesc}
195
196For examples, see the code of the \function{demo()} function.
197
198This module defines the following classes:
199
200\begin{classdesc}{Pen}{}
201Define a pen. All above functions can be called as a methods on the given
202pen. The constructor automatically creates a canvas do be drawn on.
203\end{classdesc}
204
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000205\begin{classdesc}{Turtle}{}
206Define a pen. This is essentially a synonym for \code{Pen()};
207\class{Turtle} is an empty subclass of \class{Pen}.
208\end{classdesc}
209
Fred Drake55e93961999-11-15 17:03:41 +0000210\begin{classdesc}{RawPen}{canvas}
211Define a pen which draws on a canvas \var{canvas}. This is useful if
212you want to use the module to create graphics in a ``real'' program.
213\end{classdesc}
214
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000215\subsection{Turtle, Pen and RawPen Objects \label{pen-rawpen-objects}}
Fred Drake55e93961999-11-15 17:03:41 +0000216
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000217\class{Turtle}, \class{Pen} and \class{RawPen} objects have all the
218global functions described above, except for \function{demo()} as
219methods, which manipulate the given pen.
Fred Drake55e93961999-11-15 17:03:41 +0000220
221The only method which is more powerful as a method is
222\function{degrees()}.
223
224\begin{methoddesc}{degrees}{\optional{fullcircle}}
225\var{fullcircle} is by default 360. This can cause the pen to have any
226angular units whatever: give \var{fullcircle} 2*$\pi$ for radians, or
227400 for gradians.
228\end{methoddesc}