blob: 166655526a7cbe2f34bf5e8c515a883d158be089 [file] [log] [blame]
Georg Brandlc21cd7e2008-05-16 17:37:53 +00001:mod:`tkinter.turtle` --- Turtle graphics for Tk
2================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00003
4.. module:: turtle
Georg Brandlc21cd7e2008-05-16 17:37:53 +00005 :synopsis: Old name for the tkinter.turtle module.
6
7.. module:: tkinter.turtle
Georg Brandl8ec7f652007-08-15 14:28:01 +00008 :platform: Tk
9 :synopsis: An environment for turtle graphics.
10.. moduleauthor:: Guido van Rossum <guido@python.org>
11
Georg Brandlc21cd7e2008-05-16 17:37:53 +000012.. note::
13 The :mod:`turtle` module has been renamed to :mod:`tkinter.turtle` in Python
14 3.0. It is importable under both names in Python 2.6 and the rest of the 2.x
15 series.
16
Georg Brandl8ec7f652007-08-15 14:28:01 +000017
18.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
19
20
Georg Brandlc21cd7e2008-05-16 17:37:53 +000021The :mod:`tkinter.turtle` module provides turtle graphics primitives, in both an
22object-oriented and procedure-oriented ways. Because it uses :mod:`tkinter` for
Georg Brandl8ec7f652007-08-15 14:28:01 +000023the underlying graphics, it needs a version of python installed with Tk support.
24
Georg Brandlc21cd7e2008-05-16 17:37:53 +000025.. versionchanged:: 2.6
26 Renamed from ``turtle``.
27
Georg Brandl8ec7f652007-08-15 14:28:01 +000028The procedural interface uses a pen and a canvas which are automagically created
29when any of the functions are called.
30
Georg Brandlc21cd7e2008-05-16 17:37:53 +000031The :mod:`tkinter.turtle` module defines the following functions:
Georg Brandl8ec7f652007-08-15 14:28:01 +000032
33
34.. function:: degrees()
35
36 Set angle measurement units to degrees.
37
38
39.. function:: radians()
40
41 Set angle measurement units to radians.
42
43
44.. function:: setup(**kwargs)
45
46 Sets the size and position of the main window. Keywords are:
47
48 * ``width``: either a size in pixels or a fraction of the screen. The default is
49 50% of the screen.
50
51 * ``height``: either a size in pixels or a fraction of the screen. The default
52 is 50% of the screen.
53
54 * ``startx``: starting position in pixels from the left edge of the screen.
55 ``None`` is the default value and centers the window horizontally on screen.
56
57 * ``starty``: starting position in pixels from the top edge of the screen.
58 ``None`` is the default value and centers the window vertically on screen.
59
60 Examples::
61
62 # Uses default geometry: 50% x 50% of screen, centered.
63 setup()
64
65 # Sets window to 200x200 pixels, in upper left of screen
66 setup (width=200, height=200, startx=0, starty=0)
67
68 # Sets window to 75% of screen by 50% of screen, and centers it.
69 setup(width=.75, height=0.5, startx=None, starty=None)
70
71
72.. function:: title(title_str)
73
74 Set the window's title to *title*.
75
76
77.. function:: done()
78
79 Enters the Tk main loop. The window will continue to be displayed until the
80 user closes it or the process is killed.
81
82
83.. function:: reset()
84
85 Clear the screen, re-center the pen, and set variables to the default values.
86
87
88.. function:: clear()
89
90 Clear the screen.
91
92
93.. function:: tracer(flag)
94
95 Set tracing on/off (according to whether flag is true or not). Tracing means
96 line are drawn more slowly, with an animation of an arrow along the line.
97
98
99.. function:: speed(speed)
100
101 Set the speed of the turtle. Valid values for the parameter *speed* are
102 ``'fastest'`` (no delay), ``'fast'``, (delay 5ms), ``'normal'`` (delay 10ms),
103 ``'slow'`` (delay 15ms), and ``'slowest'`` (delay 20ms).
104
105 .. versionadded:: 2.5
106
107
108.. function:: delay(delay)
109
110 Set the speed of the turtle to *delay*, which is given in ms.
111
112 .. versionadded:: 2.5
113
114
115.. function:: forward(distance)
116
117 Go forward *distance* steps.
118
119
120.. function:: backward(distance)
121
122 Go backward *distance* steps.
123
124
125.. function:: left(angle)
126
127 Turn left *angle* units. Units are by default degrees, but can be set via the
128 :func:`degrees` and :func:`radians` functions.
129
130
131.. function:: right(angle)
132
133 Turn right *angle* units. Units are by default degrees, but can be set via the
134 :func:`degrees` and :func:`radians` functions.
135
136
137.. function:: up()
138
139 Move the pen up --- stop drawing.
140
141
142.. function:: down()
143
144 Move the pen down --- draw when moving.
145
146
147.. function:: width(width)
148
149 Set the line width to *width*.
150
151
152.. function:: color(s)
153 color((r, g, b))
154 color(r, g, b)
155
156 Set the pen color. In the first form, the color is specified as a Tk color
157 specification as a string. The second form specifies the color as a tuple of
158 the RGB values, each in the range [0..1]. For the third form, the color is
159 specified giving the RGB values as three separate parameters (each in the range
160 [0..1]).
161
162
163.. function:: write(text[, move])
164
165 Write *text* at the current pen position. If *move* is true, the pen is moved to
166 the bottom-right corner of the text. By default, *move* is false.
167
168
169.. function:: fill(flag)
170
171 The complete specifications are rather complex, but the recommended usage is:
172 call ``fill(1)`` before drawing a path you want to fill, and call ``fill(0)``
173 when you finish to draw the path.
174
175
176.. function:: begin_fill()
177
178 Switch turtle into filling mode; Must eventually be followed by a corresponding
179 end_fill() call. Otherwise it will be ignored.
180
181 .. versionadded:: 2.5
182
183
184.. function:: end_fill()
185
186 End filling mode, and fill the shape; equivalent to ``fill(0)``.
187
188 .. versionadded:: 2.5
189
190
191.. function:: circle(radius[, extent])
192
193 Draw a circle with radius *radius* whose center-point is *radius* units left of
194 the turtle. *extent* determines which part of a circle is drawn: if not given it
195 defaults to a full circle.
196
197 If *extent* is not a full circle, one endpoint of the arc is the current pen
198 position. The arc is drawn in a counter clockwise direction if *radius* is
199 positive, otherwise in a clockwise direction. In the process, the direction of
200 the turtle is changed by the amount of the *extent*.
201
202
203.. function:: goto(x, y)
204 goto((x, y))
205
206 Go to co-ordinates *x*, *y*. The co-ordinates may be specified either as two
207 separate arguments or as a 2-tuple.
208
209
210.. function:: towards(x, y)
211
212 Return the angle of the line from the turtle's position to the point *x*, *y*.
213 The co-ordinates may be specified either as two separate arguments, as a
214 2-tuple, or as another pen object.
215
216 .. versionadded:: 2.5
217
218
219.. function:: heading()
220
221 Return the current orientation of the turtle.
222
223 .. versionadded:: 2.3
224
225
226.. function:: setheading(angle)
227
228 Set the orientation of the turtle to *angle*.
229
230 .. versionadded:: 2.3
231
232
233.. function:: position()
234
235 Return the current location of the turtle as an ``(x,y)`` pair.
236
237 .. versionadded:: 2.3
238
239
240.. function:: setx(x)
241
242 Set the x coordinate of the turtle to *x*.
243
244 .. versionadded:: 2.3
245
246
247.. function:: sety(y)
248
249 Set the y coordinate of the turtle to *y*.
250
251 .. versionadded:: 2.3
252
253
254.. function:: window_width()
255
256 Return the width of the canvas window.
257
258 .. versionadded:: 2.3
259
260
261.. function:: window_height()
262
263 Return the height of the canvas window.
264
265 .. versionadded:: 2.3
266
267This module also does ``from math import *``, so see the documentation for the
268:mod:`math` module for additional constants and functions useful for turtle
269graphics.
270
271
272.. function:: demo()
273
274 Exercise the module a bit.
275
276
277.. exception:: Error
278
279 Exception raised on any error caught by this module.
280
281For examples, see the code of the :func:`demo` function.
282
283This module defines the following classes:
284
285
286.. class:: Pen()
287
288 Define a pen. All above functions can be called as a methods on the given pen.
289 The constructor automatically creates a canvas do be drawn on.
290
291
292.. class:: Turtle()
293
294 Define a pen. This is essentially a synonym for ``Pen()``; :class:`Turtle` is an
295 empty subclass of :class:`Pen`.
296
297
298.. class:: RawPen(canvas)
299
300 Define a pen which draws on a canvas *canvas*. This is useful if you want to
301 use the module to create graphics in a "real" program.
302
303
304.. _pen-rawpen-objects:
305
306Turtle, Pen and RawPen Objects
307------------------------------
308
309Most of the global functions available in the module are also available as
310methods of the :class:`Turtle`, :class:`Pen` and :class:`RawPen` classes,
311affecting only the state of the given pen.
312
313The only method which is more powerful as a method is :func:`degrees`, which
314takes an optional argument letting you specify the number of units
315corresponding to a full circle:
316
317
318.. method:: Turtle.degrees([fullcircle])
319
320 *fullcircle* is by default 360. This can cause the pen to have any angular units
Georg Brandl6dfe9562007-12-16 15:59:19 +0000321 whatever: give *fullcircle* ``2*pi`` for radians, or 400 for gradians.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000322