1 | \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 |
|
---|
9 | \sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
|
---|
10 |
|
---|
11 |
|
---|
12 | The \module{turtle} module provides turtle graphics primitives, in both an
|
---|
13 | object-oriented and procedure-oriented ways. Because it uses \module{Tkinter}
|
---|
14 | for the underlying graphics, it needs a version of python installed with
|
---|
15 | Tk support.
|
---|
16 |
|
---|
17 | The procedural interface uses a pen and a canvas which are automagically
|
---|
18 | created when any of the functions are called.
|
---|
19 |
|
---|
20 | The \module{turtle} module defines the following functions:
|
---|
21 |
|
---|
22 | \begin{funcdesc}{degrees}{}
|
---|
23 | Set angle measurement units to degrees.
|
---|
24 | \end{funcdesc}
|
---|
25 |
|
---|
26 | \begin{funcdesc}{radians}{}
|
---|
27 | Set angle measurement units to radians.
|
---|
28 | \end{funcdesc}
|
---|
29 |
|
---|
30 | \begin{funcdesc}{setup}{**kwargs}
|
---|
31 | Sets 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.
|
---|
49 | setup()
|
---|
50 |
|
---|
51 | # Sets window to 200x200 pixels, in upper left of screen
|
---|
52 | setup (width=200, height=200, startx=0, starty=0)
|
---|
53 |
|
---|
54 | # Sets window to 75% of screen by 50% of screen, and centers it.
|
---|
55 | setup(width=.75, height=0.5, startx=None, starty=None)
|
---|
56 | \end{verbatim}
|
---|
57 |
|
---|
58 | \end{funcdesc}
|
---|
59 |
|
---|
60 | \begin{funcdesc}{title}{title_str}
|
---|
61 | Set the window's title to \var{title}.
|
---|
62 | \end{funcdesc}
|
---|
63 |
|
---|
64 | \begin{funcdesc}{done}{}
|
---|
65 | Enters the Tk main loop. The window will continue to
|
---|
66 | be displayed until the user closes it or the process is killed.
|
---|
67 | \end{funcdesc}
|
---|
68 |
|
---|
69 | \begin{funcdesc}{reset}{}
|
---|
70 | Clear the screen, re-center the pen, and set variables to the default
|
---|
71 | values.
|
---|
72 | \end{funcdesc}
|
---|
73 |
|
---|
74 | \begin{funcdesc}{clear}{}
|
---|
75 | Clear the screen.
|
---|
76 | \end{funcdesc}
|
---|
77 |
|
---|
78 | \begin{funcdesc}{tracer}{flag}
|
---|
79 | Set tracing on/off (according to whether flag is true or not). Tracing
|
---|
80 | means line are drawn more slowly, with an animation of an arrow along the
|
---|
81 | line.
|
---|
82 | \end{funcdesc}
|
---|
83 |
|
---|
84 | \begin{funcdesc}{speed}{speed}
|
---|
85 | Set 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}
|
---|
93 | Set the speed of the turtle to \var{delay}, which is given
|
---|
94 | in ms. \versionadded{2.5}
|
---|
95 | \end{funcdesc}
|
---|
96 |
|
---|
97 | \begin{funcdesc}{forward}{distance}
|
---|
98 | Go forward \var{distance} steps.
|
---|
99 | \end{funcdesc}
|
---|
100 |
|
---|
101 | \begin{funcdesc}{backward}{distance}
|
---|
102 | Go backward \var{distance} steps.
|
---|
103 | \end{funcdesc}
|
---|
104 |
|
---|
105 | \begin{funcdesc}{left}{angle}
|
---|
106 | Turn left \var{angle} units. Units are by default degrees, but can be
|
---|
107 | set via the \function{degrees()} and \function{radians()} functions.
|
---|
108 | \end{funcdesc}
|
---|
109 |
|
---|
110 | \begin{funcdesc}{right}{angle}
|
---|
111 | Turn right \var{angle} units. Units are by default degrees, but can be
|
---|
112 | set via the \function{degrees()} and \function{radians()} functions.
|
---|
113 | \end{funcdesc}
|
---|
114 |
|
---|
115 | \begin{funcdesc}{up}{}
|
---|
116 | Move the pen up --- stop drawing.
|
---|
117 | \end{funcdesc}
|
---|
118 |
|
---|
119 | \begin{funcdesc}{down}{}
|
---|
120 | Move the pen down --- draw when moving.
|
---|
121 | \end{funcdesc}
|
---|
122 |
|
---|
123 | \begin{funcdesc}{width}{width}
|
---|
124 | Set the line width to \var{width}.
|
---|
125 | \end{funcdesc}
|
---|
126 |
|
---|
127 | \begin{funcdesc}{color}{s}
|
---|
128 | \funclineni{color}{(r, g, b)}
|
---|
129 | \funclineni{color}{r, g, b}
|
---|
130 | Set the pen color. In the first form, the color is specified as a
|
---|
131 | Tk color specification as a string. The second form specifies the
|
---|
132 | color as a tuple of the RGB values, each in the range [0..1]. For the
|
---|
133 | third form, the color is specified giving the RGB values as three
|
---|
134 | separate parameters (each in the range [0..1]).
|
---|
135 | \end{funcdesc}
|
---|
136 |
|
---|
137 | \begin{funcdesc}{write}{text\optional{, move}}
|
---|
138 | Write \var{text} at the current pen position. If \var{move} is true,
|
---|
139 | the 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}
|
---|
144 | The complete specifications are rather complex, but the recommended
|
---|
145 | usage is: call \code{fill(1)} before drawing a path you want to fill,
|
---|
146 | and call \code{fill(0)} when you finish to draw the path.
|
---|
147 | \end{funcdesc}
|
---|
148 |
|
---|
149 | \begin{funcdesc}{begin\_fill}{}
|
---|
150 | Switch turtle into filling mode;
|
---|
151 | Must eventually be followed by a corresponding end_fill() call.
|
---|
152 | Otherwise it will be ignored.
|
---|
153 | \versionadded{2.5}
|
---|
154 | \end{funcdesc}
|
---|
155 |
|
---|
156 | \begin{funcdesc}{end\_fill}{}
|
---|
157 | End filling mode, and fill the shape; equivalent to \code{fill(0)}.
|
---|
158 | \versionadded{2.5}
|
---|
159 | \end{funcdesc}
|
---|
160 |
|
---|
161 | \begin{funcdesc}{circle}{radius\optional{, extent}}
|
---|
162 | Draw 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
|
---|
165 | not given it defaults to a full circle.
|
---|
166 |
|
---|
167 | If \var{extent} is not a full circle, one endpoint of the arc is the
|
---|
168 | current pen position. The arc is drawn in a counter clockwise
|
---|
169 | direction if \var{radius} is positive, otherwise in a clockwise
|
---|
170 | direction. In the process, the direction of the turtle is changed
|
---|
171 | by the amount of the \var{extent}.
|
---|
172 | \end{funcdesc}
|
---|
173 |
|
---|
174 | \begin{funcdesc}{goto}{x, y}
|
---|
175 | \funclineni{goto}{(x, y)}
|
---|
176 | Go to co-ordinates \var{x}, \var{y}. The co-ordinates may be
|
---|
177 | specified either as two separate arguments or as a 2-tuple.
|
---|
178 | \end{funcdesc}
|
---|
179 |
|
---|
180 | \begin{funcdesc}{towards}{x, y}
|
---|
181 | Return the angle of the line from the turtle's position
|
---|
182 | to the point \var{x}, \var{y}. The co-ordinates may be
|
---|
183 | specified either as two separate arguments, as a 2-tuple,
|
---|
184 | or as another pen object.
|
---|
185 | \versionadded{2.5}
|
---|
186 | \end{funcdesc}
|
---|
187 |
|
---|
188 | \begin{funcdesc}{heading}{}
|
---|
189 | Return the current orientation of the turtle.
|
---|
190 | \versionadded{2.3}
|
---|
191 | \end{funcdesc}
|
---|
192 |
|
---|
193 | \begin{funcdesc}{setheading}{angle}
|
---|
194 | Set the orientation of the turtle to \var{angle}.
|
---|
195 | \versionadded{2.3}
|
---|
196 | \end{funcdesc}
|
---|
197 |
|
---|
198 | \begin{funcdesc}{position}{}
|
---|
199 | Return 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}
|
---|
204 | Set the x coordinate of the turtle to \var{x}.
|
---|
205 | \versionadded{2.3}
|
---|
206 | \end{funcdesc}
|
---|
207 |
|
---|
208 | \begin{funcdesc}{sety}{y}
|
---|
209 | Set the y coordinate of the turtle to \var{y}.
|
---|
210 | \versionadded{2.3}
|
---|
211 | \end{funcdesc}
|
---|
212 |
|
---|
213 | \begin{funcdesc}{window\_width}{}
|
---|
214 | Return the width of the canvas window.
|
---|
215 | \versionadded{2.3}
|
---|
216 | \end{funcdesc}
|
---|
217 |
|
---|
218 | \begin{funcdesc}{window\_height}{}
|
---|
219 | Return the height of the canvas window.
|
---|
220 | \versionadded{2.3}
|
---|
221 | \end{funcdesc}
|
---|
222 |
|
---|
223 | This module also does \code{from math import *}, so see the
|
---|
224 | documentation for the \refmodule{math} module for additional constants
|
---|
225 | and functions useful for turtle graphics.
|
---|
226 |
|
---|
227 | \begin{funcdesc}{demo}{}
|
---|
228 | Exercise the module a bit.
|
---|
229 | \end{funcdesc}
|
---|
230 |
|
---|
231 | \begin{excdesc}{Error}
|
---|
232 | Exception raised on any error caught by this module.
|
---|
233 | \end{excdesc}
|
---|
234 |
|
---|
235 | For examples, see the code of the \function{demo()} function.
|
---|
236 |
|
---|
237 | This module defines the following classes:
|
---|
238 |
|
---|
239 | \begin{classdesc}{Pen}{}
|
---|
240 | Define a pen. All above functions can be called as a methods on the given
|
---|
241 | pen. The constructor automatically creates a canvas do be drawn on.
|
---|
242 | \end{classdesc}
|
---|
243 |
|
---|
244 | \begin{classdesc}{Turtle}{}
|
---|
245 | Define a pen. This is essentially a synonym for \code{Pen()};
|
---|
246 | \class{Turtle} is an empty subclass of \class{Pen}.
|
---|
247 | \end{classdesc}
|
---|
248 |
|
---|
249 | \begin{classdesc}{RawPen}{canvas}
|
---|
250 | Define a pen which draws on a canvas \var{canvas}. This is useful if
|
---|
251 | you want to use the module to create graphics in a ``real'' program.
|
---|
252 | \end{classdesc}
|
---|
253 |
|
---|
254 | \subsection{Turtle, Pen and RawPen Objects \label{pen-rawpen-objects}}
|
---|
255 |
|
---|
256 | Most of the global functions available in the module are also
|
---|
257 | available as methods of the \class{Turtle}, \class{Pen} and
|
---|
258 | \class{RawPen} classes, affecting only the state of the given pen.
|
---|
259 |
|
---|
260 | The only method which is more powerful as a method is
|
---|
261 | \function{degrees()}, which takes an optional argument letting
|
---|
262 | you specify the number of units corresponding to a full circle:
|
---|
263 |
|
---|
264 | \begin{methoddesc}{degrees}{\optional{fullcircle}}
|
---|
265 | \var{fullcircle} is by default 360. This can cause the pen to have any
|
---|
266 | angular units whatever: give \var{fullcircle} 2*$\pi$ for radians, or
|
---|
267 | 400 for gradians.
|
---|
268 | \end{methoddesc}
|
---|