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