blob: 673ca7be0e0cbafa0fe43a5f0d4fa19ee5553253 [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
Andrew M. Kuchling7092f4c2006-07-29 14:42:48 +000030\begin{funcdesc}{setup}{**kwargs}
31Sets the size and position of the main window. Keywords are:
32\begin{itemize}
33 \item \code{width}: either a size in pixels or a fraction of the screen.
34 The default is 50\% of the screen.
35 \item \code{height}: either a size in pixels or a fraction of the screen.
36 The default is 50\% of the screen.
37 \item \code{startx}: starting position in pixels from the left edge
38 of the screen. \code{None} is the default value and
39 centers the window horizontally on screen.
40 \item \code{starty}: starting position in pixels from the top edge
41 of the screen. \code{None} is the default value and
42 centers the window vertically on screen.
43\end{itemize}
44
45 Examples:
46
47\begin{verbatim}
48# Uses default geometry: 50% x 50% of screen, centered.
49setup()
50
51# Sets window to 200x200 pixels, in upper left of screen
52setup (width=200, height=200, startx=0, starty=0)
53
54# Sets window to 75% of screen by 50% of screen, and centers it.
55setup(width=.75, height=0.5, startx=None, starty=None)
56\end{verbatim}
57
58\end{funcdesc}
59
60\begin{funcdesc}{title}{title_str}
61Set the window's title to \var{title}.
62\end{funcdesc}
63
64\begin{funcdesc}{done}{}
65Enters the Tk main loop. The window will continue to
66be displayed until the user closes it or the process is killed.
67\end{funcdesc}
68
Fred Drake55e93961999-11-15 17:03:41 +000069\begin{funcdesc}{reset}{}
70Clear the screen, re-center the pen, and set variables to the default
71values.
72\end{funcdesc}
73
74\begin{funcdesc}{clear}{}
75Clear the screen.
76\end{funcdesc}
77
78\begin{funcdesc}{tracer}{flag}
79Set tracing on/off (according to whether flag is true or not). Tracing
80means line are drawn more slowly, with an animation of an arrow along the
81line.
82\end{funcdesc}
83
Martin v. Löwis82c276e2006-07-03 11:12:06 +000084\begin{funcdesc}{speed}{speed}
85Set the speed of the turtle. Valid values for the parameter
86\var{speed} are \code{'fastest'} (no delay), \code{'fast'},
87(delay 5ms), \code{'normal'} (delay 10ms), \code{'slow'}
88(delay 15ms), and \code{'slowest'} (delay 20ms).
89\versionadded{2.5}
90\end{funcdesc}
91
92\begin{funcdesc}{delay}{delay}
93Set the speed of the turtle to \var{delay}, which is given
94in ms. \versionadded{2.5}
95\end{funcdesc}
96
Fred Drake55e93961999-11-15 17:03:41 +000097\begin{funcdesc}{forward}{distance}
98Go forward \var{distance} steps.
99\end{funcdesc}
100
101\begin{funcdesc}{backward}{distance}
102Go backward \var{distance} steps.
103\end{funcdesc}
104
105\begin{funcdesc}{left}{angle}
106Turn left \var{angle} units. Units are by default degrees, but can be
107set via the \function{degrees()} and \function{radians()} functions.
108\end{funcdesc}
109
110\begin{funcdesc}{right}{angle}
111Turn right \var{angle} units. Units are by default degrees, but can be
112set via the \function{degrees()} and \function{radians()} functions.
113\end{funcdesc}
114
115\begin{funcdesc}{up}{}
116Move the pen up --- stop drawing.
117\end{funcdesc}
118
119\begin{funcdesc}{down}{}
Raymond Hettingerff6dd0b2003-12-06 01:35:56 +0000120Move the pen down --- draw when moving.
Fred Drake55e93961999-11-15 17:03:41 +0000121\end{funcdesc}
122
123\begin{funcdesc}{width}{width}
124Set the line width to \var{width}.
125\end{funcdesc}
126
127\begin{funcdesc}{color}{s}
Fred Drake482b9a82001-11-15 20:41:03 +0000128\funclineni{color}{(r, g, b)}
129\funclineni{color}{r, g, b}
130Set the pen color. In the first form, the color is specified as a
131Tk color specification as a string. The second form specifies the
132color as a tuple of the RGB values, each in the range [0..1]. For the
133third form, the color is specified giving the RGB values as three
134separate parameters (each in the range [0..1]).
Fred Drake55e93961999-11-15 17:03:41 +0000135\end{funcdesc}
136
137\begin{funcdesc}{write}{text\optional{, move}}
138Write \var{text} at the current pen position. If \var{move} is true,
139the pen is moved to the bottom-right corner of the text. By default,
140\var{move} is false.
141\end{funcdesc}
142
143\begin{funcdesc}{fill}{flag}
144The complete specifications are rather complex, but the recommended
145usage is: call \code{fill(1)} before drawing a path you want to fill,
146and call \code{fill(0)} when you finish to draw the path.
147\end{funcdesc}
148
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000149\begin{funcdesc}{begin\_fill}{}
Martin v. Löwis06c68b82006-07-10 22:11:28 +0000150Switch turtle into filling mode;
151Must eventually be followed by a corresponding end_fill() call.
152Otherwise it will be ignored.
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000153\versionadded{2.5}
154\end{funcdesc}
155
156\begin{funcdesc}{end\_fill}{}
157End filling mode, and fill the shape; equivalent to \code{fill(0)}.
158\versionadded{2.5}
159\end{funcdesc}
160
Fred Drake55e93961999-11-15 17:03:41 +0000161\begin{funcdesc}{circle}{radius\optional{, extent}}
Raymond Hettingera97e4f32003-02-21 03:14:08 +0000162Draw a circle with radius \var{radius} whose center-point is
163\var{radius} units left of the turtle.
164\var{extent} determines which part of a circle is drawn: if
Fred Drake55e93961999-11-15 17:03:41 +0000165not given it defaults to a full circle.
166
167If \var{extent} is not a full circle, one endpoint of the arc is the
168current pen position. The arc is drawn in a counter clockwise
169direction if \var{radius} is positive, otherwise in a clockwise
Raymond Hettingera97e4f32003-02-21 03:14:08 +0000170direction. In the process, the direction of the turtle is changed
171by the amount of the \var{extent}.
Fred Drake55e93961999-11-15 17:03:41 +0000172\end{funcdesc}
173
174\begin{funcdesc}{goto}{x, y}
Fred Drake482b9a82001-11-15 20:41:03 +0000175\funclineni{goto}{(x, y)}
176Go to co-ordinates \var{x}, \var{y}. The co-ordinates may be
177specified either as two separate arguments or as a 2-tuple.
Fred Drake55e93961999-11-15 17:03:41 +0000178\end{funcdesc}
179
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000180\begin{funcdesc}{towards}{x, y}
181Return the angle of the line from the turtle's position
182to the point \var{x}, \var{y}. The co-ordinates may be
183specified either as two separate arguments, as a 2-tuple,
184or as another pen object.
185\versionadded{2.5}
186\end{funcdesc}
187
188\begin{funcdesc}{heading}{}
189Return the current orientation of the turtle.
190\versionadded{2.3}
191\end{funcdesc}
192
193\begin{funcdesc}{setheading}{angle}
194Set the orientation of the turtle to \var{angle}.
195\versionadded{2.3}
196\end{funcdesc}
197
198\begin{funcdesc}{position}{}
199Return the current location of the turtle as an \code{(x,y)} pair.
200\versionadded{2.3}
201\end{funcdesc}
202
203\begin{funcdesc}{setx}{x}
204Set the x coordinate of the turtle to \var{x}.
205\versionadded{2.3}
206\end{funcdesc}
207
208\begin{funcdesc}{sety}{y}
209Set the y coordinate of the turtle to \var{y}.
210\versionadded{2.3}
211\end{funcdesc}
212
213\begin{funcdesc}{window\_width}{}
214Return the width of the canvas window.
215\versionadded{2.3}
216\end{funcdesc}
217
218\begin{funcdesc}{window\_height}{}
219Return the height of the canvas window.
220\versionadded{2.3}
221\end{funcdesc}
222
Fred Drake74242421999-11-17 16:09:57 +0000223This module also does \code{from math import *}, so see the
224documentation for the \refmodule{math} module for additional constants
225and functions useful for turtle graphics.
Fred Drake55e93961999-11-15 17:03:41 +0000226
227\begin{funcdesc}{demo}{}
228Exercise the module a bit.
229\end{funcdesc}
230
231\begin{excdesc}{Error}
232Exception raised on any error caught by this module.
233\end{excdesc}
234
235For examples, see the code of the \function{demo()} function.
236
237This module defines the following classes:
238
239\begin{classdesc}{Pen}{}
240Define a pen. All above functions can be called as a methods on the given
241pen. The constructor automatically creates a canvas do be drawn on.
242\end{classdesc}
243
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000244\begin{classdesc}{Turtle}{}
245Define a pen. This is essentially a synonym for \code{Pen()};
246\class{Turtle} is an empty subclass of \class{Pen}.
247\end{classdesc}
248
Fred Drake55e93961999-11-15 17:03:41 +0000249\begin{classdesc}{RawPen}{canvas}
250Define a pen which draws on a canvas \var{canvas}. This is useful if
251you want to use the module to create graphics in a ``real'' program.
252\end{classdesc}
253
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000254\subsection{Turtle, Pen and RawPen Objects \label{pen-rawpen-objects}}
Fred Drake55e93961999-11-15 17:03:41 +0000255
Martin v. Löwis82c276e2006-07-03 11:12:06 +0000256\class{Turtle}, \class{Pen} and \class{RawPen} objects have all the
257global functions described above, except for \function{demo()} as
258methods, which manipulate the given pen.
Fred Drake55e93961999-11-15 17:03:41 +0000259
260The only method which is more powerful as a method is
261\function{degrees()}.
262
263\begin{methoddesc}{degrees}{\optional{fullcircle}}
264\var{fullcircle} is by default 360. This can cause the pen to have any
265angular units whatever: give \var{fullcircle} 2*$\pi$ for radians, or
266400 for gradians.
267\end{methoddesc}