blob: 9a6d49feb8404cfbc21d8b29bc451c90fe748552 [file] [log] [blame]
Martin v. Löwis87184592008-06-04 06:29:55 +00001========================================
Georg Brandl6634bf22008-05-20 07:13:37 +00002:mod:`turtle` --- Turtle graphics for Tk
3========================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00004
Martin v. Löwis87184592008-06-04 06:29:55 +00005Introduction
Georg Brandla2b34b82008-06-04 11:17:26 +00006============
Martin v. Löwis87184592008-06-04 06:29:55 +00007
Georg Brandla2b34b82008-06-04 11:17:26 +00008Turtle graphics is a popular way for introducing programming to kids. It was
9part of the original Logo programming language developed by Wally Feurzig and
10Seymour Papert in 1966.
Martin v. Löwis87184592008-06-04 06:29:55 +000011
Georg Brandla2b34b82008-06-04 11:17:26 +000012Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it the
13command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
14direction it is facing, drawing a line as it moves. Give it the command
15``turtle.left(25)``, and it rotates in-place 25 degrees clockwise.
Martin v. Löwis87184592008-06-04 06:29:55 +000016
Georg Brandla2b34b82008-06-04 11:17:26 +000017By combining together these and similar commands, intricate shapes and pictures
18can easily be drawn.
Martin v. Löwis87184592008-06-04 06:29:55 +000019
Georg Brandla2b34b82008-06-04 11:17:26 +000020The :mod:`turtle` module is an extended reimplementation of the same-named
21module from the Python standard distribution up to version Python 2.5.
Martin v. Löwis87184592008-06-04 06:29:55 +000022
Georg Brandla2b34b82008-06-04 11:17:26 +000023It tries to keep the merits of the old turtle module and to be (nearly) 100%
24compatible with it. This means in the first place to enable the learning
25programmer to use all the commands, classes and methods interactively when using
26the module from within IDLE run with the ``-n`` switch.
Martin v. Löwis87184592008-06-04 06:29:55 +000027
Georg Brandla2b34b82008-06-04 11:17:26 +000028The turtle module provides turtle graphics primitives, in both object-oriented
29and procedure-oriented ways. Because it uses :mod:`Tkinter` for the underlying
30graphics, it needs a version of python installed with Tk support.
Martin v. Löwis87184592008-06-04 06:29:55 +000031
Georg Brandla2b34b82008-06-04 11:17:26 +000032The object-oriented interface uses essentially two+two classes:
Martin v. Löwis87184592008-06-04 06:29:55 +000033
Georg Brandla2b34b82008-06-04 11:17:26 +0000341. The :class:`TurtleScreen` class defines graphics windows as a playground for
35 the drawing turtles. Its constructor needs a :class:`Tkinter.Canvas` or a
36 :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is
37 used as part of some application.
Martin v. Löwis87184592008-06-04 06:29:55 +000038
Georg Brandla2b34b82008-06-04 11:17:26 +000039 Derived from :class:`TurtleScreen` is the subclass :class:`Screen`. Screen
40 is implemented as sort of singleton, so there can exist only one instance of
41 Screen at a time. It should be used when :mod:`turtle` is used as a
42 standalone tool for doing graphics.
Martin v. Löwis87184592008-06-04 06:29:55 +000043
Georg Brandla2b34b82008-06-04 11:17:26 +000044 All methods of TurtleScreen/Screen also exist as functions, i.e. as part of
45 the procedure-oriented interface.
Martin v. Löwis87184592008-06-04 06:29:55 +000046
Georg Brandla2b34b82008-06-04 11:17:26 +0000472. :class:`RawTurtle` (alias: :class:`RawPen`) defines Turtle objects which draw
48 on a :class:`TurtleScreen`. Its constructor needs a Canvas, ScrolledCanvas
49 or TurtleScreen as argument, so the RawTurtle objects know where to draw.
Martin v. Löwis87184592008-06-04 06:29:55 +000050
Georg Brandla2b34b82008-06-04 11:17:26 +000051 Derived from RawTurtle is the subclass :class:`Turtle` (alias: :class:`Pen`),
52 which draws on "the" :class:`Screen` - instance which is automatically
53 created, if not already present.
Martin v. Löwis87184592008-06-04 06:29:55 +000054
Georg Brandla2b34b82008-06-04 11:17:26 +000055 All methods of RawTurtle/Turtle also exist as functions, i.e. part of the
56 procedure-oriented interface.
Martin v. Löwis87184592008-06-04 06:29:55 +000057
Georg Brandla2b34b82008-06-04 11:17:26 +000058The procedural interface provides functions which are derived from the methods
59of the classes :class:`Screen` and :class:`Turtle`. They have the same names as
60the corresponding methods. A screen object is automativally created whenever a
61function derived from a Screen method is called. An (unnamed) turtle object is
62automatically created whenever any of the functions derived from a Turtle method
63is called.
Martin v. Löwis87184592008-06-04 06:29:55 +000064
Georg Brandla2b34b82008-06-04 11:17:26 +000065To use multiple turtles an a screen one has to use the object-oriented interface.
Martin v. Löwis87184592008-06-04 06:29:55 +000066
Georg Brandla2b34b82008-06-04 11:17:26 +000067.. note::
68 In the following documentation the argument list for functions is given.
69 Methods, of course, have the additional first argument *self* which is
70 omitted here.
Martin v. Löwis87184592008-06-04 06:29:55 +000071
Martin v. Löwis87184592008-06-04 06:29:55 +000072
Georg Brandla2b34b82008-06-04 11:17:26 +000073Overview over available Turtle and Screen methods
74=================================================
Martin v. Löwis87184592008-06-04 06:29:55 +000075
Georg Brandla2b34b82008-06-04 11:17:26 +000076Turtle methods
Martin v. Löwis87184592008-06-04 06:29:55 +000077--------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000078
Georg Brandla2b34b82008-06-04 11:17:26 +000079Turtle motion
80 Move and draw
81 | :func:`forward` | :func:`fd`
82 | :func:`backward` | :func:`bk` | :func:`back`
83 | :func:`right` | :func:`rt`
84 | :func:`left` | :func:`lt`
85 | :func:`goto` | :func:`setpos` | :func:`setposition`
86 | :func:`setx`
87 | :func:`sety`
88 | :func:`setheading` | :func:`seth`
89 | :func:`home`
90 | :func:`circle`
91 | :func:`dot`
92 | :func:`stamp`
93 | :func:`clearstamp`
94 | :func:`clearstamps`
95 | :func:`undo`
96 | :func:`speed`
Georg Brandl8ec7f652007-08-15 14:28:01 +000097
Georg Brandla2b34b82008-06-04 11:17:26 +000098 Tell Turtle's state
99 | :func:`position` | :func:`pos`
100 | :func:`towards`
101 | :func:`xcor`
102 | :func:`ycor`
103 | :func:`heading`
104 | :func:`distance`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105
Georg Brandla2b34b82008-06-04 11:17:26 +0000106 Setting and measurement
107 | :func:`degrees`
108 | :func:`radians`
109
110Pen control
111 Drawing state
112 | :func:`pendown` | :func:`pd` | :func:`down`
113 | :func:`penup` | :func:`pu` | :func:`up`
114 | :func:`pensize` | :func:`width`
115 | :func:`pen`
116 | :func:`isdown`
117
118 Color control
119 | :func:`color`
120 | :func:`pencolor`
121 | :func:`fillcolor`
122
123 Filling
124 | :func:`fill`
125 | :func:`begin_fill`
126 | :func:`end_fill`
127
128 More drawing control
129 | :func:`reset`
130 | :func:`clear`
131 | :func:`write`
132
133Turtle state
134 Visibility
135 | :func:`showturtle` | :func:`st`
136 | :func:`hideturtle` | :func:`ht`
137 | :func:`isvisible`
138
139 Appearance
140 | :func:`shape`
141 | :func:`resizemode`
142 | :func:`shapesize` | :func:`turtlesize`
143 | :func:`settiltangle`
144 | :func:`tiltangle`
145 | :func:`tilt`
146
147Using events
148 | :func:`onclick`
149 | :func:`onrelease`
150 | :func:`ondrag`
151
152Special Turtle methods
153 | :func:`begin_poly`
154 | :func:`end_poly`
155 | :func:`get_poly`
156 | :func:`clone`
157 | :func:`getturtle` | :func:`getpen`
158 | :func:`getscreen`
159 | :func:`setundobuffer`
160 | :func:`undobufferentries`
161 | :func:`tracer`
162 | :func:`window_width`
163 | :func:`window_height`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000164
Georg Brandl8ec7f652007-08-15 14:28:01 +0000165
Georg Brandla2b34b82008-06-04 11:17:26 +0000166Methods of TurtleScreen/Screen
167------------------------------
168
169Window control
170 | :func:`bgcolor`
171 | :func:`bgpic`
172 | :func:`clear` | :func:`clearscreen`
173 | :func:`reset` | :func:`resetscreen`
174 | :func:`screensize`
175 | :func:`setworldcoordinates`
176
177Animation control
178 | :func:`delay`
179 | :func:`tracer`
180 | :func:`update`
181
182Using screen events
183 | :func:`listen`
184 | :func:`onkey`
185 | :func:`onclick` | :func:`onscreenclick`
186 | :func:`ontimer`
187
188Settings and special methods
189 | :func:`mode`
190 | :func:`colormode`
191 | :func:`getcanvas`
192 | :func:`getshapes`
193 | :func:`register_shape` | :func:`addshape`
194 | :func:`turtles`
195 | :func:`window_height`
196 | :func:`window_width`
197
198Methods specific to Screen
199 | :func:`bye`
200 | :func:`exitonclick`
201 | :func:`setup`
202 | :func:`title`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000203
Georg Brandl8ec7f652007-08-15 14:28:01 +0000204
Georg Brandla2b34b82008-06-04 11:17:26 +0000205Methods of RawTurtle/Turtle and corresponding functions
206=======================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000207
Georg Brandla2b34b82008-06-04 11:17:26 +0000208Most of the examples in this section refer to a Turtle instance called
209``turtle``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000210
Georg Brandla2b34b82008-06-04 11:17:26 +0000211Turtle motion
212-------------
213
214.. function:: forward(distance)
215 fd(distance)
216
217 :param distance: a number (integer or float)
218
219 Move the turtle forward by the specified *distance*, in the direction the
220 turtle is headed.
221
222 >>> turtle.position()
223 (0.00, 0.00)
224 >>> turtle.forward(25)
225 >>> turtle.position()
226 (25.00,0.00)
227 >>> turtle.forward(-75)
228 >>> turtle.position()
229 (-50.00,0.00)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000230
231
Georg Brandla2b34b82008-06-04 11:17:26 +0000232.. function:: back(distance)
233 bk(distance)
234 backward(distance)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000235
Georg Brandla2b34b82008-06-04 11:17:26 +0000236 :param distance: a number
Georg Brandl8ec7f652007-08-15 14:28:01 +0000237
Georg Brandla2b34b82008-06-04 11:17:26 +0000238 Move the turtle backward by *distance*, opposite to the direction the
239 turtle is headed. Do not change the turtle's heading.
240
241 >>> turtle.position()
242 (0.00, 0.00)
243 >>> turtle.backward(30)
244 >>> turtle.position()
245 (-30.00, 0.00)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000246
Georg Brandl8ec7f652007-08-15 14:28:01 +0000247
Georg Brandla2b34b82008-06-04 11:17:26 +0000248.. function:: right(angle)
249 rt(angle)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000250
Georg Brandla2b34b82008-06-04 11:17:26 +0000251 :param angle: a number (integer or float)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000252
Georg Brandla2b34b82008-06-04 11:17:26 +0000253 Turn turtle right by *angle* units. (Units are by default degrees, but
254 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
255 orientation depends on the turtle mode, see :func:`mode`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000256
Georg Brandla2b34b82008-06-04 11:17:26 +0000257 >>> turtle.heading()
258 22.0
259 >>> turtle.right(45)
260 >>> turtle.heading()
261 337.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000262
Georg Brandl8ec7f652007-08-15 14:28:01 +0000263
Georg Brandla2b34b82008-06-04 11:17:26 +0000264.. function:: left(angle)
265 lt(angle)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000266
Georg Brandla2b34b82008-06-04 11:17:26 +0000267 :param angle: a number (integer or float)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000268
Georg Brandla2b34b82008-06-04 11:17:26 +0000269 Turn turtle left by *angle* units. (Units are by default degrees, but
270 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
271 orientation depends on the turtle mode, see :func:`mode`.
272
273 >>> turtle.heading()
274 22.0
275 >>> turtle.left(45)
276 >>> turtle.heading()
277 67.0
278
279.. function:: goto(x, y=None)
280 setpos(x, y=None)
281 setposition(x, y=None)
282
283 :param x: a number or a pair/vector of numbers
284 :param y: a number or ``None``
285
286 If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D`
287 (e.g. as returned by :func:`pos`).
288
289 Move turtle to an absolute position. If the pen is down, draw line. Do
290 not change the turtle's orientation.
291
292 >>> tp = turtle.pos()
293 >>> tp
294 (0.00, 0.00)
295 >>> turtle.setpos(60,30)
296 >>> turtle.pos()
297 (60.00,30.00)
298 >>> turtle.setpos((20,80))
299 >>> turtle.pos()
300 (20.00,80.00)
301 >>> turtle.setpos(tp)
302 >>> turtle.pos()
303 (0.00,0.00)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000304
Georg Brandl8ec7f652007-08-15 14:28:01 +0000305
Georg Brandla2b34b82008-06-04 11:17:26 +0000306.. function:: setx(x)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000307
Georg Brandla2b34b82008-06-04 11:17:26 +0000308 :param x: a number (integer or float)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000309
Georg Brandla2b34b82008-06-04 11:17:26 +0000310 Set the turtle's first coordinate to *x*, leave second coordinate
311 unchanged.
312
313 >>> turtle.position()
314 (0.00, 240.00)
315 >>> turtle.setx(10)
316 >>> turtle.position()
317 (10.00, 240.00)
318
319
320.. function:: sety(y)
321
322 :param y: a number (integer or float)
323
324 Set the turtle's first coordinate to *y*, leave second coordinate
325 unchanged.
326
327 >>> turtle.position()
328 (0.00, 40.00)
329 >>> turtle.sety(-10)
330 >>> turtle.position()
331 (0.00, -10.00)
332
333
334.. function:: setheading(to_angle)
335 seth(to_angle)
336
337 :param to_angle: a number (integer or float)
338
339 Set the orientation of the turtle to *to_angle*. Here are some common
340 directions in degrees:
341
342 =================== ====================
343 standard mode logo mode
344 =================== ====================
345 0 - east 0 - north
346 90 - north 90 - east
347 180 - west 180 - south
348 270 - south 270 - west
349 =================== ====================
350
351 >>> turtle.setheading(90)
352 >>> turtle.heading()
353 90
354
355
356.. function:: home()
357
358 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
359 its start-orientation (which depends on the mode, see :func:`mode`).
360
361
362.. function:: circle(radius, extent=None, steps=None)
363
364 :param radius: a number
365 :param extent: a number (or ``None``)
366 :param steps: an integer (or ``None``)
367
368 Draw a circle with given *radius*. The center is *radius* units left of
369 the turtle; *extent* -- an angle -- determines which part of the circle
370 is drawn. If *extent* is not given, draw the entire circle. If *extent*
371 is not a full circle, one endpoint of the arc is the current pen
372 position. Draw the arc in counterclockwise direction if *radius* is
373 positive, otherwise in clockwise direction. Finally the direction of the
374 turtle is changed by the amount of *extent*.
375
376 As the circle is approximated by an inscribed regular polygon, *steps*
377 determines the number of steps to use. If not given, it will be
378 calculated automatically. May be used to draw regular polygons.
379
380 >>> turtle.circle(50)
381 >>> turtle.circle(120, 180) # draw a semicircle
382
383
384.. function:: dot(size=None, *color)
385
386 :param size: an integer >= 1 (if given)
387 :param color: a colorstring or a numeric color tuple
388
389 Draw a circular dot with diameter *size*, using *color*. If *size* is
390 not given, the maximum of pensize+4 and 2*pensize is used.
391
392 >>> turtle.dot()
393 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
394
395
396.. function:: stamp()
397
398 Stamp a copy of the turtle shape onto the canvas at the current turtle
399 position. Return a stamp_id for that stamp, which can be used to delete
400 it by calling ``clearstamp(stamp_id)``.
401
402 >>> turtle.color("blue")
403 >>> turtle.stamp()
404 13
405 >>> turtle.fd(50)
406
407
408.. function:: clearstamp(stampid)
409
410 :param stampid: an integer, must be return value of previous
411 :func:`stamp` call
412
413 Delete stamp with given *stampid*.
414
415 >>> turtle.color("blue")
416 >>> astamp = turtle.stamp()
417 >>> turtle.fd(50)
418 >>> turtle.clearstamp(astamp)
419
420
421.. function:: clearstamps(n=None)
422
423 :param n: an integer (or ``None``)
424
425 Delete all or first/last *n* of turtle's stamps. If *n* is None, delete
426 all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete
427 last *n* stamps.
428
429 >>> for i in range(8):
430 ... turtle.stamp(); turtle.fd(30)
431 >>> turtle.clearstamps(2)
432 >>> turtle.clearstamps(-2)
433 >>> turtle.clearstamps()
434
435
436.. function:: undo()
437
438 Undo (repeatedly) the last turtle action(s). Number of available
439 undo actions is determined by the size of the undobuffer.
440
441 >>> for i in range(4):
442 ... turtle.fd(50); turtle.lt(80)
443 ...
444 >>> for i in range(8):
445 ... turtle.undo()
446
447
448.. function:: speed(speed=None)
449
450 :param speed: an integer in the range 0..10 or a speedstring (see below)
451
452 Set the turtle's speed to an integer value in the range 0..10. If no
453 argument is given, return current speed.
454
455 If input is a number greater than 10 or smaller than 0.5, speed is set
456 to 0. Speedstrings are mapped to speedvalues as follows:
457
458 * "fastest": 0
459 * "fast": 10
460 * "normal": 6
461 * "slow": 3
462 * "slowest": 1
463
464 Speeds from 1 to 10 enforce increasingly faster animation of line drawing
465 and turtle turning.
466
467 Attention: *speed* = 0 means that *no* animation takes
468 place. forward/back makes turtle jump and likewise left/right make the
469 turtle turn instantly.
470
471 >>> turtle.speed(3)
472
473
474Tell Turtle's state
Martin v. Löwis87184592008-06-04 06:29:55 +0000475-------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000476
Georg Brandla2b34b82008-06-04 11:17:26 +0000477.. function:: position()
478 pos()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000479
Georg Brandla2b34b82008-06-04 11:17:26 +0000480 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000481
Georg Brandla2b34b82008-06-04 11:17:26 +0000482 >>> turtle.pos()
483 (0.00, 240.00)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000484
Georg Brandl8ec7f652007-08-15 14:28:01 +0000485
Georg Brandla2b34b82008-06-04 11:17:26 +0000486.. function:: towards(x, y=None)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000487
Georg Brandla2b34b82008-06-04 11:17:26 +0000488 :param x: a number or a pair/vector of numbers or a turtle instance
489 :param y: a number if *x* is a number, else ``None``
Georg Brandl8ec7f652007-08-15 14:28:01 +0000490
Georg Brandla2b34b82008-06-04 11:17:26 +0000491 Return the angle between the line from turtle position to position specified
492 by (x,y), the vector or the other turtle. This depends on the turtle's start
493 orientation which depends on the mode - "standard"/"world" or "logo").
Georg Brandl8ec7f652007-08-15 14:28:01 +0000494
Georg Brandla2b34b82008-06-04 11:17:26 +0000495 >>> turtle.pos()
496 (10.00, 10.00)
497 >>> turtle.towards(0,0)
498 225.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000499
500
Georg Brandla2b34b82008-06-04 11:17:26 +0000501.. function:: xcor()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000502
Georg Brandla2b34b82008-06-04 11:17:26 +0000503 Return the turtle's x coordinate.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000504
Georg Brandla2b34b82008-06-04 11:17:26 +0000505 >>> reset()
506 >>> turtle.left(60)
507 >>> turtle.forward(100)
508 >>> print turtle.xcor()
509 50.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000510
Georg Brandl8ec7f652007-08-15 14:28:01 +0000511
Georg Brandla2b34b82008-06-04 11:17:26 +0000512.. function:: ycor()
513
514 Return the turtle's y coordinate.
515
516 >>> reset()
517 >>> turtle.left(60)
518 >>> turtle.forward(100)
519 >>> print turtle.ycor()
520 86.6025403784
Georg Brandl8ec7f652007-08-15 14:28:01 +0000521
Georg Brandl8ec7f652007-08-15 14:28:01 +0000522
Georg Brandla2b34b82008-06-04 11:17:26 +0000523.. function:: heading()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000524
Georg Brandla2b34b82008-06-04 11:17:26 +0000525 Return the turtle's current heading (value depends on the turtle mode, see
526 :func:`mode`).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000527
Georg Brandla2b34b82008-06-04 11:17:26 +0000528 >>> turtle.left(67)
529 >>> turtle.heading()
530 67.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000531
532
Georg Brandla2b34b82008-06-04 11:17:26 +0000533.. function:: distance(x, y=None)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000534
Georg Brandla2b34b82008-06-04 11:17:26 +0000535 :param x: a number or a pair/vector of numbers or a turtle instance
536 :param y: a number if *x* is a number, else ``None``
Georg Brandl8ec7f652007-08-15 14:28:01 +0000537
Georg Brandla2b34b82008-06-04 11:17:26 +0000538 Return the distance from the turtle to (x,y), the given vector, or the given
539 other turtle, in turtle step units.
540
541 >>> turtle.pos()
542 (0.00, 0.00)
543 >>> turtle.distance(30,40)
544 50.0
545 >>> joe = Turtle()
546 >>> joe.forward(77)
547 >>> turtle.distance(joe)
548 77.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000549
Georg Brandl8ec7f652007-08-15 14:28:01 +0000550
Georg Brandla2b34b82008-06-04 11:17:26 +0000551Settings for measurement
552------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000553
Georg Brandla2b34b82008-06-04 11:17:26 +0000554.. function:: degrees(fullcircle=360.0)
555
556 :param fullcircle: a number
557
558 Set angle measurement units, i.e. set number of "degrees" for a full circle.
559 Default value is 360 degrees.
560
561 >>> turtle.left(90)
562 >>> turtle.heading()
563 90
564 >>> turtle.degrees(400.0) # angle measurement in gon
565 >>> turtle.heading()
566 100
Georg Brandl8ec7f652007-08-15 14:28:01 +0000567
Georg Brandl8ec7f652007-08-15 14:28:01 +0000568
Georg Brandla2b34b82008-06-04 11:17:26 +0000569.. function:: radians()
570
571 Set the angle measurement units to radians. Equivalent to
572 ``degrees(2*math.pi)``.
573
574 >>> turtle.heading()
575 90
576 >>> turtle.radians()
577 >>> turtle.heading()
578 1.5707963267948966
Georg Brandl8ec7f652007-08-15 14:28:01 +0000579
580
Georg Brandla2b34b82008-06-04 11:17:26 +0000581Pen control
582-----------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000583
Georg Brandla2b34b82008-06-04 11:17:26 +0000584Drawing state
585~~~~~~~~~~~~~
586
587.. function:: pendown()
588 pd()
589 down()
590
591 Pull the pen down -- drawing when moving.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000592
593
Georg Brandla2b34b82008-06-04 11:17:26 +0000594.. function:: penup()
595 pu()
596 up()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000597
Georg Brandla2b34b82008-06-04 11:17:26 +0000598 Pull the pen up -- no drawing when moving.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000599
Georg Brandl8ec7f652007-08-15 14:28:01 +0000600
Georg Brandla2b34b82008-06-04 11:17:26 +0000601.. function:: pensize(width=None)
602 width(width=None)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000603
Georg Brandla2b34b82008-06-04 11:17:26 +0000604 :param width: a positive number
605
606 Set the line thickness to *width* or return it. If resizemode is set to
607 "auto" and turtleshape is a polygon, that polygon is drawn with the same line
608 thickness. If no argument is given, the current pensize is returned.
609
610 >>> turtle.pensize()
611 1
612 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
Georg Brandl8ec7f652007-08-15 14:28:01 +0000613
Georg Brandl8ec7f652007-08-15 14:28:01 +0000614
Georg Brandla2b34b82008-06-04 11:17:26 +0000615.. function:: pen(pen=None, **pendict)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000616
Georg Brandla2b34b82008-06-04 11:17:26 +0000617 :param pen: a dictionary with some or all of the below listed keys
618 :param pendict: one or more keyword-arguments with the below listed keys as keywords
Georg Brandl8ec7f652007-08-15 14:28:01 +0000619
Georg Brandla2b34b82008-06-04 11:17:26 +0000620 Return or set the pen's attributes in a "pen-dictionary" with the following
621 key/value pairs:
622
623 * "shown": True/False
624 * "pendown": True/False
625 * "pencolor": color-string or color-tuple
626 * "fillcolor": color-string or color-tuple
627 * "pensize": positive number
628 * "speed": number in range 0..10
629 * "resizemode": "auto" or "user" or "noresize"
630 * "stretchfactor": (positive number, positive number)
631 * "outline": positive number
632 * "tilt": number
633
634 This dicionary can be used as argument for a subsequent call to :func:`pen`
635 to restore the former pen-state. Moreover one or more of these attributes
636 can be provided as keyword-arguments. This can be used to set several pen
637 attributes in one statement.
638
639 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
640 >>> turtle.pen()
641 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
642 'pencolor': 'red', 'pendown': True, 'fillcolor': 'black',
643 'stretchfactor': (1,1), 'speed': 3}
644 >>> penstate=turtle.pen()
645 >>> turtle.color("yellow","")
646 >>> turtle.penup()
647 >>> turtle.pen()
648 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
649 'pencolor': 'yellow', 'pendown': False, 'fillcolor': '',
650 'stretchfactor': (1,1), 'speed': 3}
651 >>> p.pen(penstate, fillcolor="green")
652 >>> p.pen()
653 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
654 'pencolor': 'red', 'pendown': True, 'fillcolor': 'green',
655 'stretchfactor': (1,1), 'speed': 3}
656
657
658.. function:: isdown()
659
660 Return ``True`` if pen is down, ``False`` if it's up.
661
662 >>> turtle.penup()
663 >>> turtle.isdown()
664 False
665 >>> turtle.pendown()
666 >>> turtle.isdown()
667 True
668
669
670Color control
671~~~~~~~~~~~~~
672
673.. function:: pencolor(*args)
674
675 Return or set the pencolor.
676
677 Four input formats are allowed:
678
679 ``pencolor()``
680 Return the current pencolor as color specification string, possibly in
681 hex-number format (see example). May be used as input to another
682 color/pencolor/fillcolor call.
683
684 ``pencolor(colorstring)``
685 Set pencolor to *colorstring*, which is a Tk color specification string,
686 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
687
688 ``pencolor((r, g, b))``
689 Set pencolor to the RGB color represented by the tuple of *r*, *g*, and
690 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
691 colormode is either 1.0 or 255 (see :func:`colormode`).
692
693 ``pencolor(r, g, b)``
694 Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of
695 *r*, *g*, and *b* must be in the range 0..colormode.
696
697 If turtleshape is a polygon, the outline of that polygon is drawn with the
698 newly set pencolor.
699
700 >>> turtle.pencolor("brown")
701 >>> tup = (0.2, 0.8, 0.55)
702 >>> turtle.pencolor(tup)
703 >>> turtle.pencolor()
704 "#33cc8c"
705
706
707.. function:: fillcolor(*args)
708
709 Return or set the fillcolor.
710
711 Four input formats are allowed:
712
713 ``fillcolor()``
714 Return the current fillcolor as color specification string, possibly in
715 hex-number format (see example). May be used as input to another
716 color/pencolor/fillcolor call.
717
718 ``fillcolor(colorstring)``
719 Set fillcolor to *colorstring*, which is a Tk color specification string,
720 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
721
722 ``fillcolor((r, g, b))``
723 Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and
724 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
725 colormode is either 1.0 or 255 (see :func:`colormode`).
726
727 ``fillcolor(r, g, b)``
728 Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of
729 *r*, *g*, and *b* must be in the range 0..colormode.
730
731 If turtleshape is a polygon, the interior of that polygon is drawn
732 with the newly set fillcolor.
733
734 >>> turtle.fillcolor("violet")
735 >>> col = turtle.pencolor()
736 >>> turtle.fillcolor(col)
737 >>> turtle.fillcolor(0, .5, 0)
738
739
740.. function:: color(*args)
741
742 Return or set pencolor and fillcolor.
743
744 Several input formats are allowed. They use 0 to 3 arguments as
745 follows:
746
747 ``color()``
748 Return the current pencolor and the current fillcolor as a pair of color
749 specification strings as returned by :func:`pencolor` and
750 :func:`fillcolor`.
751
752 ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)``
753 Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the
754 given value.
755
756 ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))``
757 Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)``
758 and analogously if the other input format is used.
759
760 If turtleshape is a polygon, outline and interior of that polygon is drawn
761 with the newly set colors.
762
763 >>> turtle.color("red", "green")
764 >>> turtle.color()
765 ("red", "green")
766 >>> colormode(255)
767 >>> color((40, 80, 120), (160, 200, 240))
768 >>> color()
769 ("#285078", "#a0c8f0")
770
771
772See also: Screen method :func:`colormode`.
773
774
775Filling
776~~~~~~~
777
778.. function:: fill(flag)
779
780 :param flag: True/False (or 1/0 respectively)
781
782 Call ``fill(True)`` before drawing the shape you want to fill, and
783 ``fill(False)`` when done. When used without argument: return fillstate
784 (``True`` if filling, ``False`` else).
785
786 >>> turtle.fill(True)
787 >>> for _ in range(3):
788 ... turtle.forward(100)
789 ... turtle.left(120)
790 ...
791 >>> turtle.fill(False)
792
793
794.. function:: begin_fill()
795
796 Call just before drawing a shape to be filled. Equivalent to ``fill(True)``.
797
798 >>> turtle.color("black", "red")
799 >>> turtle.begin_fill()
800 >>> turtle.circle(60)
801 >>> turtle.end_fill()
802
803
804.. function:: end_fill()
805
806 Fill the shape drawn after the last call to :func:`begin_fill`. Equivalent
807 to ``fill(False)``.
808
809
810More drawing control
811~~~~~~~~~~~~~~~~~~~~
812
813.. function:: reset()
814
815 Delete the turtle's drawings from the screen, re-center the turtle and set
816 variables to the default values.
817
818 >>> turtle.position()
819 (0.00,-22.00)
820 >>> turtle.heading()
821 100.0
822 >>> turtle.reset()
823 >>> turtle.position()
824 (0.00,0.00)
825 >>> turtle.heading()
826 0.0
827
828
829.. function:: clear()
830
831 Delete the turtle's drawings from the screen. Do not move turtle. State and
832 position of the turtle as well as drawings of other turtles are not affected.
833
834
835.. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal"))
836
837 :param arg: object to be written to the TurtleScreen
838 :param move: True/False
839 :param align: one of the strings "left", "center" or right"
840 :param font: a triple (fontname, fontsize, fonttype)
841
842 Write text - the string representation of *arg* - at the current turtle
843 position according to *align* ("left", "center" or right") and with the given
844 font. If *move* is True, the pen is moved to the bottom-right corner of the
845 text. By default, *move* is False.
846
847 >>> turtle.write("Home = ", True, align="center")
848 >>> turtle.write((0,0), True)
849
850
851Turtle state
852------------
853
854Visibility
855~~~~~~~~~~
856
857.. function:: showturtle()
858 st()
859
860 Make the turtle visible.
861
862 >>> turtle.hideturtle()
863 >>> turtle.showturtle()
864
865
866.. function:: hideturtle()
867 ht()
868
869 Make the turtle invisible. It's a good idea to do this while you're in the
870 middle of doing some complex drawing, because hiding the turtle speeds up the
871 drawing observably.
872
873 >>> turtle.hideturtle()
874
875
876.. function:: isvisible()
877
878 Return True if the Turtle is shown, False if it's hidden.
879
880 >>> turtle.hideturtle()
881 >>> print turtle.isvisible():
882 False
883
884
885Appearance
886~~~~~~~~~~
887
888.. function:: shape(name=None)
889
890 :param name: a string which is a valid shapename
891
892 Set turtle shape to shape with given *name* or, if name is not given, return
893 name of current shape. Shape with *name* must exist in the TurtleScreen's
894 shape dictionary. Initially there are the following polygon shapes: "arrow",
895 "turtle", "circle", "square", "triangle", "classic". To learn about how to
896 deal with shapes see Screen method :func:`register_shape`.
897
898 >>> turtle.shape()
899 "arrow"
900 >>> turtle.shape("turtle")
901 >>> turtle.shape()
902 "turtle"
903
904
905.. function:: resizemode(rmode=None)
906
907 :param rmode: one of the strings "auto", "user", "noresize"
908
909 Set resizemode to one of the values: "auto", "user", "noresize". If *rmode*
910 is not given, return current resizemode. Different resizemodes have the
911 following effects:
912
913 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
914 - "user": adapts the appearance of the turtle according to the values of
915 stretchfactor and outlinewidth (outline), which are set by
916 :func:`shapesize`.
917 - "noresize": no adaption of the turtle's appearance takes place.
918
919 resizemode("user") is called by :func:`shapesize` when used with arguments.
920
921 >>> turtle.resizemode("noresize")
922 >>> turtle.resizemode()
923 "noresize"
924
925
926.. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None)
927
928 :param stretch_wid: positive number
929 :param stretch_len: positive number
930 :param outline: positive number
931
932 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set
933 resizemode to "user". If and only if resizemode is set to "user", the turtle
934 will be displayed stretched according to its stretchfactors: *stretch_wid* is
935 stretchfactor perpendicular to its orientation, *stretch_len* is
936 stretchfactor in direction of its orientation, *outline* determines the width
937 of the shapes's outline.
938
939 >>> turtle.resizemode("user")
940 >>> turtle.shapesize(5, 5, 12)
941 >>> turtle.shapesize(outline=8)
942
943
944.. function:: tilt(angle)
945
946 :param angle: a number
947
948 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
949 change the turtle's heading (direction of movement).
950
951 >>> turtle.shape("circle")
952 >>> turtle.shapesize(5,2)
953 >>> turtle.tilt(30)
954 >>> turtle.fd(50)
955 >>> turtle.tilt(30)
956 >>> turtle.fd(50)
957
958
959.. function:: settiltangle(angle)
960
961 :param angle: a number
962
963 Rotate the turtleshape to point in the direction specified by *angle*,
964 regardless of its current tilt-angle. *Do not* change the turtle's heading
965 (direction of movement).
966
967 >>> turtle.shape("circle")
968 >>> turtle.shapesize(5,2)
969 >>> turtle.settiltangle(45)
970 >>> stamp()
971 >>> turtle.fd(50)
972 >>> turtle.settiltangle(-45)
973 >>> stamp()
974 >>> turtle.fd(50)
975
976
977.. function:: tiltangle()
978
979 Return the current tilt-angle, i.e. the angle between the orientation of the
980 turtleshape and the heading of the turtle (its direction of movement).
981
982 >>> turtle.shape("circle")
983 >>> turtle.shapesize(5,2)
984 >>> turtle.tilt(45)
985 >>> turtle.tiltangle()
986 45
987
988
989Using events
990------------
991
992.. function:: onclick(fun, btn=1, add=None)
993
994 :param fun: a function with two arguments which will be called with the
995 coordinates of the clicked point on the canvas
996 :param num: number of the mouse-button, defaults to 1 (left mouse button)
997 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
998 added, otherwise it will replace a former binding
999
1000 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1001 existing bindings are removed. Example for the anonymous turtle, i.e. the
1002 procedural way:
1003
1004 >>> def turn(x, y):
1005 ... left(180)
1006 ...
1007 >>> onclick(turn) # Now clicking into the turtle will turn it.
1008 >>> onclick(None) # event-binding will be removed
1009
1010
1011.. function:: onrelease(fun, btn=1, add=None)
1012
1013 :param fun: a function with two arguments which will be called with the
1014 coordinates of the clicked point on the canvas
1015 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1016 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1017 added, otherwise it will replace a former binding
1018
1019 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1020 ``None``, existing bindings are removed.
1021
1022 >>> class MyTurtle(Turtle):
1023 ... def glow(self,x,y):
1024 ... self.fillcolor("red")
1025 ... def unglow(self,x,y):
1026 ... self.fillcolor("")
1027 ...
1028 >>> turtle = MyTurtle()
1029 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1030 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
1031
1032
1033.. function:: ondrag(fun, btn=1, add=None)
1034
1035 :param fun: a function with two arguments which will be called with the
1036 coordinates of the clicked point on the canvas
1037 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1038 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1039 added, otherwise it will replace a former binding
1040
1041 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1042 existing bindings are removed.
1043
1044 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1045 mouse-click event on that turtle.
1046
1047 >>> turtle.ondrag(turtle.goto)
1048 # Subsequently, clicking and dragging the Turtle will move it across
1049 # the screen thereby producing handdrawings (if pen is down).
1050
1051
1052Special Turtle methods
1053----------------------
1054
1055.. function:: begin_poly()
1056
1057 Start recording the vertices of a polygon. Current turtle position is first
1058 vertex of polygon.
1059
1060
1061.. function:: end_poly()
1062
1063 Stop recording the vertices of a polygon. Current turtle position is last
1064 vertex of polygon. This will be connected with the first vertex.
1065
1066
1067.. function:: get_poly()
1068
1069 Return the last recorded polygon.
1070
1071 >>> p = turtle.get_poly()
1072 >>> turtle.register_shape("myFavouriteShape", p)
1073
1074
1075.. function:: clone()
1076
1077 Create and return a clone of the turtle with same position, heading and
1078 turtle properties.
1079
1080 >>> mick = Turtle()
1081 >>> joe = mick.clone()
1082
1083
1084.. function:: getturtle()
1085
1086 Return the Turtle object itself. Only reasonable use: as a function to
1087 return the "anonymous turtle":
1088
1089 >>> pet = getturtle()
1090 >>> pet.fd(50)
1091 >>> pet
1092 <turtle.Turtle object at 0x01417350>
1093 >>> turtles()
1094 [<turtle.Turtle object at 0x01417350>]
1095
1096
1097.. function:: getscreen()
1098
1099 Return the :class:`TurtleScreen` object the turtle is drawing on.
1100 TurtleScreen methods can then be called for that object.
1101
1102 >>> ts = turtle.getscreen()
1103 >>> ts
1104 <turtle.Screen object at 0x01417710>
1105 >>> ts.bgcolor("pink")
1106
1107
1108.. function:: setundobuffer(size)
1109
1110 :param size: an integer or ``None``
1111
1112 Set or disable undobuffer. If *size* is an integer an empty undobuffer of
1113 given size is installed. *size* gives the maximum number of turtle actions
1114 that can be undone by the :func:`undo` method/function. If *size* is
1115 ``None``, the undobuffer is disabled.
1116
1117 >>> turtle.setundobuffer(42)
1118
1119
1120.. function:: undobufferentries()
1121
1122 Return number of entries in the undobuffer.
1123
1124 >>> while undobufferentries():
1125 ... undo()
1126
1127
1128.. function:: tracer(flag=None, delay=None)
1129
1130 A replica of the corresponding TurtleScreen method.
1131
1132 .. deprecated:: 2.6
1133
1134
1135.. function:: window_width()
1136 window_height()
1137
1138 Both are replicas of the corresponding TurtleScreen methods.
1139
1140 .. deprecated:: 2.6
1141
1142
1143.. _compoundshapes:
1144
1145Excursus about the use of compound shapes
1146-----------------------------------------
1147
1148To use compound turtle shapes, which consist of several polygons of different
1149color, you must use the helper class :class:`Shape` explicitly as described
1150below:
1151
11521. Create an empty Shape object of type "compound".
11532. Add as many components to this object as desired, using the
1154 :meth:`addcomponent` method.
1155
1156 For example:
1157
1158 >>> s = Shape("compound")
1159 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1160 >>> s.addcomponent(poly1, "red", "blue")
1161 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1162 >>> s.addcomponent(poly2, "blue", "red")
1163
11643. Now add the Shape to the Screen's shapelist and use it:
1165
1166 >>> register_shape("myshape", s)
1167 >>> shape("myshape")
1168
1169
1170.. note::
1171
1172 The :class:`Shape` class is used internally by the :func:`register_shape`
1173 method in different ways. The application programmer has to deal with the
1174 Shape class *only* when using compound shapes like shown above!
1175
1176
1177Methods of TurtleScreen/Screen and corresponding functions
1178==========================================================
1179
1180Most of the examples in this section refer to a TurtleScreen instance called
1181``screen``.
1182
1183
1184Window control
1185--------------
1186
1187.. function:: bgcolor(*args)
1188
1189 :param args: a color string or three numbers in the range 0..colormode or a
1190 3-tuple of such numbers
1191
1192 Set or return background color of the TurtleScreen.
1193
1194 >>> screen.bgcolor("orange")
1195 >>> screen.bgcolor()
1196 "orange"
1197 >>> screen.bgcolor(0.5,0,0.5)
1198 >>> screen.bgcolor()
1199 "#800080"
1200
1201
1202.. function:: bgpic(picname=None)
1203
1204 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
1205
1206 Set background image or return name of current backgroundimage. If *picname*
1207 is a filename, set the corresponding image as background. If *picname* is
1208 ``"nopic"``, delete background image, if present. If *picname* is ``None``,
1209 return the filename of the current backgroundimage.
1210
1211 >>> screen.bgpic()
1212 "nopic"
1213 >>> screen.bgpic("landscape.gif")
1214 >>> screen.bgpic()
1215 "landscape.gif"
1216
1217
1218.. function:: clear()
1219 clearscreen()
1220
1221 Delete all drawings and all turtles from the TurtleScreen. Reset the now
1222 empty TurtleScreen to its initial state: white background, no background
1223 image, no event bindings and tracing on.
1224
1225 .. note::
1226 This TurtleScreen method is available as a global function only under the
1227 name ``clearscreen``. The global function ``clear`` is another one
1228 derived from the Turtle method ``clear``.
1229
1230
1231.. function:: reset()
1232 resetscreen()
1233
1234 Reset all Turtles on the Screen to their initial state.
1235
1236 .. note::
1237 This TurtleScreen method is available as a global function only under the
1238 name ``resetscreen``. The global function ``reset`` is another one
1239 derived from the Turtle method ``reset``.
1240
1241
1242.. function:: screensize(canvwidth=None, canvheight=None, bg=None)
1243
1244 :param canvwidth: positive integer, new width of canvas in pixels
1245 :param canvheight: positive integer, new height of canvas in pixels
1246 :param bg: colorstring or color-tupel, new background color
1247
1248 If no arguments are given, return current (canvaswidth, canvasheight). Else
1249 resize the canvas the turtles are drawing on. Do not alter the drawing
1250 window. To observe hidden parts of the canvas, use the scrollbars. With this
1251 method, one can make visible those parts of a drawing which were outside the
1252 canvas before.
1253
1254 >>> turtle.screensize(2000,1500)
1255 # e.g. to search for an erroneously escaped turtle ;-)
1256
1257
1258.. function:: setworldcoordinates(llx, lly, urx, ury)
1259
1260 :param llx: a number, x-coordinate of lower left corner of canvas
1261 :param lly: a number, y-coordinate of lower left corner of canvas
1262 :param urx: a number, x-coordinate of upper right corner of canvas
1263 :param ury: a number, y-coordinate of upper right corner of canvas
1264
1265 Set up user-defined coordinate system and switch to mode "world" if
1266 necessary. This performs a ``screen.reset()``. If mode "world" is already
1267 active, all drawings are redrawn according to the new coordinates.
1268
1269 **ATTENTION**: in user-defined coordinate systems angles may appear
1270 distorted.
1271
1272 >>> screen.reset()
1273 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1274 >>> for _ in range(72):
1275 ... left(10)
1276 ...
1277 >>> for _ in range(8):
1278 ... left(45); fd(2) # a regular octogon
1279
1280
1281Animation control
1282-----------------
1283
1284.. function:: delay(delay=None)
1285
1286 :param delay: positive integer
1287
1288 Set or return the drawing *delay* in milliseconds. (This is approximately
1289 the time interval between two consecutived canvas updates.) The longer the
1290 drawing delay, the slower the animation.
1291
1292 Optional argument:
1293
1294 >>> screen.delay(15)
1295 >>> screen.delay()
1296 15
1297
1298
1299.. function:: tracer(n=None, delay=None)
1300
1301 :param n: nonnegative integer
1302 :param delay: nonnegative integer
1303
1304 Turn turtle animation on/off and set delay for update drawings. If *n* is
1305 given, only each n-th regular screen update is really performed. (Can be
1306 used to accelerate the drawing of complex graphics.) Second argument sets
1307 delay value (see :func:`delay`).
1308
1309 >>> screen.tracer(8, 25)
1310 >>> dist = 2
1311 >>> for i in range(200):
1312 ... fd(dist)
1313 ... rt(90)
1314 ... dist += 2
1315
1316
1317.. function:: update()
1318
1319 Perform a TurtleScreen update. To be used when tracer is turned off.
1320
1321See also the RawTurtle/Turtle method :func:`speed`.
1322
1323
1324Using screen events
1325-------------------
1326
1327.. function:: listen(xdummy=None, ydummy=None)
1328
1329 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1330 are provided in order to be able to pass :func:`listen` to the onclick method.
1331
1332
1333.. function:: onkey(fun, key)
1334
1335 :param fun: a function with no arguments or ``None``
1336 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1337
1338 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1339 are removed. Remark: in order to be able to register key-events, TurtleScreen
1340 must have the focus. (See method :func:`listen`.)
1341
1342 >>> def f():
1343 ... fd(50)
1344 ... lt(60)
1345 ...
1346 >>> screen.onkey(f, "Up")
1347 >>> screen.listen()
1348
1349
1350.. function:: onclick(fun, btn=1, add=None)
1351 onscreenclick(fun, btn=1, add=None)
1352
1353 :param fun: a function with two arguments which will be called with the
1354 coordinates of the clicked point on the canvas
1355 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1356 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1357 added, otherwise it will replace a former binding
1358
1359 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1360 existing bindings are removed.
1361
1362 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1363 named turtle:
1364
1365 >>> screen.onclick(turtle.goto)
1366 # Subsequently clicking into the TurtleScreen will
1367 # make the turtle move to the clicked point.
1368 >>> screen.onclick(None) # remove event binding again
1369
1370 .. note::
1371 This TurtleScreen method is available as a global function only under the
1372 name ``onscreenclick``. The global function ``onclick`` is another one
1373 derived from the Turtle method ``onclick``.
1374
1375
1376.. function:: ontimer(fun, t=0)
1377
1378 :param fun: a function with no arguments
1379 :param t: a number >= 0
1380
1381 Install a timer that calls *fun* after *t* milliseconds.
1382
1383 >>> running = True
1384 >>> def f():
1385 if running:
1386 fd(50)
1387 lt(60)
1388 screen.ontimer(f, 250)
1389 >>> f() ### makes the turtle marching around
1390 >>> running = False
1391
1392
1393Settings and special methods
1394----------------------------
1395
1396.. function:: mode(mode=None)
1397
1398 :param mode: one of the strings "standard", "logo" or "world"
1399
1400 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1401 is not given, current mode is returned.
1402
1403 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1404 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1405 "world coordinates". **Attention**: in this mode angles appear distorted if
1406 ``x/y`` unit-ratio doesn't equal 1.
1407
1408 ============ ========================= ===================
1409 Mode Initial turtle heading positive angles
1410 ============ ========================= ===================
1411 "standard" to the right (east) counterclockwise
1412 "logo" upward (north) clockwise
1413 ============ ========================= ===================
1414
1415 >>> mode("logo") # resets turtle heading to north
1416 >>> mode()
1417 "logo"
1418
1419
1420.. function:: colormode(cmode=None)
1421
1422 :param cmode: one of the values 1.0 or 255
1423
1424 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1425 values of color triples have to be in the range 0..\ *cmode*.
1426
1427 >>> screen.colormode()
1428 1.0
1429 >>> screen.colormode(255)
1430 >>> turtle.pencolor(240,160,80)
1431
1432
1433.. function:: getcanvas()
1434
1435 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1436 do with a Tkinter Canvas.
1437
1438 >>> cv = screen.getcanvas()
1439 >>> cv
1440 <turtle.ScrolledCanvas instance at 0x010742D8>
1441
1442
1443.. function:: getshapes()
1444
1445 Return a list of names of all currently available turtle shapes.
1446
1447 >>> screen.getshapes()
1448 ["arrow", "blank", "circle", ..., "turtle"]
1449
1450
1451.. function:: register_shape(name, shape=None)
1452 addshape(name, shape=None)
1453
1454 There are three different ways to call this function:
1455
1456 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
1457 corresponding image shape.
1458
1459 .. note::
1460 Image shapes *do not* rotate when turning the turtle, so they do not
1461 display the heading of the turtle!
1462
1463 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1464 coordinates: Install the corresponding polygon shape.
1465
1466 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
1467 object: Install the corresponding compound shape.
1468
1469 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1470 shapes can be used by issuing the command ``shape(shapename)``.
1471
1472 >>> screen.register_shape("turtle.gif")
1473 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
1474
1475
1476.. function:: turtles()
1477
1478 Return the list of turtles on the screen.
1479
1480 >>> for turtle in screen.turtles()
1481 ... turtle.color("red")
1482
1483
1484.. function:: window_height()
1485
1486 Return the height of the turtle window.
1487
1488 >>> screen.window_height()
1489 480
1490
1491
1492.. function:: window_width()
1493
1494 Return the width of the turtle window.
1495
1496 >>> screen.window_width()
1497 640
1498
1499
1500.. _screenspecific:
1501
1502Methods specific to Screen, not inherited from TurtleScreen
Martin v. Löwis87184592008-06-04 06:29:55 +00001503-----------------------------------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001504
Georg Brandla2b34b82008-06-04 11:17:26 +00001505.. function:: bye()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001506
Georg Brandla2b34b82008-06-04 11:17:26 +00001507 Shut the turtlegraphics window.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001508
Georg Brandl8ec7f652007-08-15 14:28:01 +00001509
Georg Brandla2b34b82008-06-04 11:17:26 +00001510.. function:: exitonclick()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001511
Georg Brandla2b34b82008-06-04 11:17:26 +00001512 Bind bye() method to mouse clicks on the Screen.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001513
Georg Brandl8ec7f652007-08-15 14:28:01 +00001514
Georg Brandla2b34b82008-06-04 11:17:26 +00001515 If the value "using_IDLE" in the configuration dictionary is ``False``
1516 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
1517 (no subprocess) is used, this value should be set to ``True`` in
1518 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1519 client script.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001520
Georg Brandl8ec7f652007-08-15 14:28:01 +00001521
Georg Brandla2b34b82008-06-04 11:17:26 +00001522.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
Georg Brandl8ec7f652007-08-15 14:28:01 +00001523
Georg Brandla2b34b82008-06-04 11:17:26 +00001524 Set the size and position of the main window. Default values of arguments
1525 are stored in the configuration dicionary and can be changed via a
1526 :file:`turtle.cfg` file.
1527
1528 :param width: if an integer, a size in pixels, if a float, a fraction of the
1529 screen; default is 50% of screen
1530 :param height: if an integer, the height in pixels, if a float, a fraction of
1531 the screen; default is 75% of screen
1532 :param startx: if positive, starting position in pixels from the left
1533 edge of the screen, if negative from the right edge, if None,
1534 center window horizontally
1535 :param startx: if positive, starting position in pixels from the top
1536 edge of the screen, if negative from the bottom edge, if None,
1537 center window vertically
1538
1539 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1540 # sets window to 200x200 pixels, in upper left of screen
1541 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1542 # sets window to 75% of screen by 50% of screen and centers
Georg Brandl8ec7f652007-08-15 14:28:01 +00001543
Georg Brandl8ec7f652007-08-15 14:28:01 +00001544
Georg Brandla2b34b82008-06-04 11:17:26 +00001545.. function:: title(titlestring)
1546
1547 :param titlestring: a string that is shown in the titlebar of the turtle
1548 graphics window
1549
1550 Set title of turtle window to *titlestring*.
1551
1552 >>> screen.title("Welcome to the turtle zoo!")
1553
1554
1555The public classes of the module :mod:`turtle`
1556==============================================
1557
1558
1559.. class:: RawTurtle(canvas)
1560 RawPen(canvas)
1561
1562 :param canvas: a :class:`Tkinter.Canvas`, a :class:`ScrolledCanvas` or a
1563 :class:`TurtleScreen`
1564
1565 Create a turtle. The turtle has all methods described above as "methods of
1566 Turtle/RawTurtle".
1567
1568
1569.. class:: Turtle()
1570
1571 Subclass of RawTurtle, has the same interface but draws on a default
1572 :class:`Screen` object created automatically when needed for the first time.
1573
1574
1575.. class:: TurtleScreen(cv)
1576
1577 :param cv: a :class:`Tkinter.Canvas`
1578
1579 Provides screen oriented methods like :func:`setbg` etc. that are described
1580 above.
1581
1582.. class:: Screen()
1583
1584 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
1585
1586
1587.. class:: ScrolledCavas(master)
1588
1589 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
1590 a Tkinter-canvas with scrollbars added
1591
1592 Used by class Screen, which thus automatically provides a ScrolledCanvas as
1593 playground for the turtles.
1594
1595.. class:: Shape(type_, data)
1596
1597 :param type\_: one of the strings "polygon", "image", "compound"
1598
1599 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
1600 specification:
1601
1602
1603 =========== ===========
1604 *type_* *data*
1605 =========== ===========
1606 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
1607 "image" an image (in this form only used internally!)
1608 "compound" ``None`` (a compund shape has to be constructed using the
1609 :meth:`addcomponent` method)
1610 =========== ===========
1611
1612 .. method:: addcomponent(poly, fill, outline=None)
1613
1614 :param poly: a polygon, i.e. a tuple of pairs of numbers
1615 :param fill: a color the *poly* will be filled with
1616 :param outline: a color for the poly's outline (if given)
1617
1618 Example:
1619
1620 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
1621 >>> s = Shape("compound")
1622 >>> s.addcomponent(poly, "red", "blue")
1623 # .. add more components and then use register_shape()
1624
1625 See :ref:`compoundshapes`.
1626
1627
1628.. class:: Vec2D(x, y)
1629
1630 A two-dimensional vector class, used as a helper class for implementing
1631 turtle graphics. May be useful for turtle graphics programs too. Derived
1632 from tuple, so a vector is a tuple!
1633
1634 Provides (for *a*, *b* vectors, *k* number):
1635
1636 * ``a + b`` vector addition
1637 * ``a - b`` vector subtraction
1638 * ``a * b`` inner product
1639 * ``k * a`` and ``a * k`` multiplication with scalar
1640 * ``abs(a)`` absolute value of a
1641 * ``a.rotate(angle)`` rotation
1642
1643
1644Help and configuration
1645======================
1646
1647How to use help
1648---------------
1649
1650The public methods of the Screen and Turtle classes are documented extensively
1651via docstrings. So these can be used as online-help via the Python help
1652facilities:
1653
1654- When using IDLE, tooltips show the signatures and first lines of the
1655 docstrings of typed in function-/method calls.
1656
1657- Calling :func:`help` on methods or functions displays the docstrings::
1658
1659 >>> help(Screen.bgcolor)
1660 Help on method bgcolor in module turtle:
Martin v. Löwis87184592008-06-04 06:29:55 +00001661
Georg Brandla2b34b82008-06-04 11:17:26 +00001662 bgcolor(self, *args) unbound turtle.Screen method
1663 Set or return backgroundcolor of the TurtleScreen.
Martin v. Löwis87184592008-06-04 06:29:55 +00001664
Georg Brandla2b34b82008-06-04 11:17:26 +00001665 Arguments (if given): a color string or three numbers
1666 in the range 0..colormode or a 3-tuple of such numbers.
Martin v. Löwis87184592008-06-04 06:29:55 +00001667
Martin v. Löwis87184592008-06-04 06:29:55 +00001668
Georg Brandla2b34b82008-06-04 11:17:26 +00001669 >>> screen.bgcolor("orange")
1670 >>> screen.bgcolor()
1671 "orange"
1672 >>> screen.bgcolor(0.5,0,0.5)
1673 >>> screen.bgcolor()
1674 "#800080"
Martin v. Löwis87184592008-06-04 06:29:55 +00001675
Georg Brandla2b34b82008-06-04 11:17:26 +00001676 >>> help(Turtle.penup)
1677 Help on method penup in module turtle:
Martin v. Löwis87184592008-06-04 06:29:55 +00001678
Georg Brandla2b34b82008-06-04 11:17:26 +00001679 penup(self) unbound turtle.Turtle method
1680 Pull the pen up -- no drawing when moving.
Martin v. Löwis87184592008-06-04 06:29:55 +00001681
Georg Brandla2b34b82008-06-04 11:17:26 +00001682 Aliases: penup | pu | up
Martin v. Löwis87184592008-06-04 06:29:55 +00001683
Georg Brandla2b34b82008-06-04 11:17:26 +00001684 No argument
Martin v. Löwis87184592008-06-04 06:29:55 +00001685
Georg Brandla2b34b82008-06-04 11:17:26 +00001686 >>> turtle.penup()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001687
Georg Brandla2b34b82008-06-04 11:17:26 +00001688- The docstrings of the functions which are derived from methods have a modified
1689 form::
Georg Brandl8ec7f652007-08-15 14:28:01 +00001690
Georg Brandla2b34b82008-06-04 11:17:26 +00001691 >>> help(bgcolor)
1692 Help on function bgcolor in module turtle:
Martin v. Löwis87184592008-06-04 06:29:55 +00001693
Georg Brandla2b34b82008-06-04 11:17:26 +00001694 bgcolor(*args)
1695 Set or return backgroundcolor of the TurtleScreen.
Martin v. Löwis87184592008-06-04 06:29:55 +00001696
Georg Brandla2b34b82008-06-04 11:17:26 +00001697 Arguments (if given): a color string or three numbers
1698 in the range 0..colormode or a 3-tuple of such numbers.
Martin v. Löwis87184592008-06-04 06:29:55 +00001699
Georg Brandla2b34b82008-06-04 11:17:26 +00001700 Example::
Martin v. Löwis87184592008-06-04 06:29:55 +00001701
Georg Brandla2b34b82008-06-04 11:17:26 +00001702 >>> bgcolor("orange")
1703 >>> bgcolor()
1704 "orange"
1705 >>> bgcolor(0.5,0,0.5)
1706 >>> bgcolor()
1707 "#800080"
1708
1709 >>> help(penup)
1710 Help on function penup in module turtle:
1711
1712 penup()
1713 Pull the pen up -- no drawing when moving.
1714
1715 Aliases: penup | pu | up
1716
1717 No argument
1718
1719 Example:
1720 >>> penup()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001721
Georg Brandla2b34b82008-06-04 11:17:26 +00001722These modified docstrings are created automatically together with the function
1723definitions that are derived from the methods at import time.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001724
1725
Georg Brandla2b34b82008-06-04 11:17:26 +00001726Translation of docstrings into different languages
Martin v. Löwis87184592008-06-04 06:29:55 +00001727--------------------------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001728
Georg Brandla2b34b82008-06-04 11:17:26 +00001729There is a utility to create a dictionary the keys of which are the method names
1730and the values of which are the docstrings of the public methods of the classes
1731Screen and Turtle.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001732
Georg Brandla2b34b82008-06-04 11:17:26 +00001733.. function:: write_docstringdict(filename="turtle_docstringdict")
Georg Brandl8ec7f652007-08-15 14:28:01 +00001734
Georg Brandla2b34b82008-06-04 11:17:26 +00001735 :param filename: a string, used as filename
Georg Brandl8ec7f652007-08-15 14:28:01 +00001736
Georg Brandla2b34b82008-06-04 11:17:26 +00001737 Create and write docstring-dictionary to a Python script with the given
1738 filename. This function has to be called explicitly (it is not used by the
1739 turtle graphics classes). The docstring dictionary will be written to the
1740 Python script :file:`{filename}.py`. It is intended to serve as a template
1741 for translation of the docstrings into different languages.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001742
Georg Brandla2b34b82008-06-04 11:17:26 +00001743If you (or your students) want to use :mod:`turtle` with online help in your
1744native language, you have to translate the docstrings and save the resulting
1745file as e.g. :file:`turtle_docstringdict_german.py`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001746
Georg Brandla2b34b82008-06-04 11:17:26 +00001747If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
1748will be read in at import time and will replace the original English docstrings.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001749
Georg Brandla2b34b82008-06-04 11:17:26 +00001750At the time of this writing there are docstring dictionaries in German and in
1751Italian. (Requests please to glingl@aon.at.)
1752
1753
1754
1755How to configure Screen and Turtles
Martin v. Löwis87184592008-06-04 06:29:55 +00001756-----------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001757
Georg Brandla2b34b82008-06-04 11:17:26 +00001758The built-in default configuration mimics the appearance and behaviour of the
1759old turtle module in order to retain best possible compatibility with it.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001760
Georg Brandla2b34b82008-06-04 11:17:26 +00001761If you want to use a different configuration which better reflects the features
1762of this module or which better fits to your needs, e.g. for use in a classroom,
1763you can prepare a configuration file ``turtle.cfg`` which will be read at import
1764time and modify the configuration according to its settings.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001765
Georg Brandla2b34b82008-06-04 11:17:26 +00001766The built in configuration would correspond to the following turtle.cfg::
Georg Brandl8ec7f652007-08-15 14:28:01 +00001767
Georg Brandla2b34b82008-06-04 11:17:26 +00001768 width = 0.5
1769 height = 0.75
1770 leftright = None
1771 topbottom = None
1772 canvwidth = 400
1773 canvheight = 300
1774 mode = standard
1775 colormode = 1.0
1776 delay = 10
1777 undobuffersize = 1000
1778 shape = classic
1779 pencolor = black
1780 fillcolor = black
1781 resizemode = noresize
1782 visible = True
1783 language = english
1784 exampleturtle = turtle
1785 examplescreen = screen
1786 title = Python Turtle Graphics
1787 using_IDLE = False
Georg Brandl8ec7f652007-08-15 14:28:01 +00001788
Martin v. Löwis87184592008-06-04 06:29:55 +00001789Short explanation of selected entries:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001790
Georg Brandla2b34b82008-06-04 11:17:26 +00001791- The first four lines correspond to the arguments of the :meth:`Screen.setup`
1792 method.
1793- Line 5 and 6 correspond to the arguments of the method
1794 :meth:`Screen.screensize`.
1795- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
1796 info try ``help(shape)``.
1797- If you want to use no fillcolor (i.e. make the turtle transparent), you have
1798 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
1799 the cfg-file).
1800- If you want to reflect the turtle its state, you have to use ``resizemode =
1801 auto``.
1802- If you set e.g. ``language = italian`` the docstringdict
1803 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
1804 present on the import path, e.g. in the same directory as :mod:`turtle`.
1805- The entries *exampleturtle* and *examplescreen* define the names of these
1806 objects as they occur in the docstrings. The transformation of
1807 method-docstrings to function-docstrings will delete these names from the
1808 docstrings.
1809- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
1810 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
1811 mainloop.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001812
Georg Brandla2b34b82008-06-04 11:17:26 +00001813There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
1814stored and an additional one in the current working directory. The latter will
1815override the settings of the first one.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001816
Georg Brandla2b34b82008-06-04 11:17:26 +00001817The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can
1818study it as an example and see its effects when running the demos (preferably
1819not from within the demo-viewer).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001820
Georg Brandla2b34b82008-06-04 11:17:26 +00001821
1822Demo scripts
1823============
1824
1825There is a set of demo scripts in the turtledemo directory located in the
1826:file:`Demo/turtle` directory in the source distribution.
1827
Martin v. Löwis87184592008-06-04 06:29:55 +00001828It contains:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001829
Georg Brandla2b34b82008-06-04 11:17:26 +00001830- a set of 15 demo scripts demonstrating differet features of the new module
1831 :mod:`turtle`
1832- a demo viewer :file:`turtleDemo.py` which can be used to view the sourcecode
1833 of the scripts and run them at the same time. 14 of the examples can be
1834 accessed via the Examples menu; all of them can also be run standalone.
1835- The example :file:`turtledemo_two_canvases.py` demonstrates the simultaneous
1836 use of two canvases with the turtle module. Therefore it only can be run
1837 standalone.
1838- There is a :file:`turtle.cfg` file in this directory, which also serves as an
1839 example for how to write and use such files.
1840
Martin v. Löwis87184592008-06-04 06:29:55 +00001841The demoscripts are:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001842
Martin v. Löwis87184592008-06-04 06:29:55 +00001843+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001844| Name | Description | Features |
Martin v. Löwis87184592008-06-04 06:29:55 +00001845+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001846| bytedesign | complex classical | :func:`tracer`, delay,|
1847| | turtlegraphics pattern | :func:`update` |
Martin v. Löwis87184592008-06-04 06:29:55 +00001848+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001849| chaos | graphs verhust dynamics, | world coordinates |
1850| | proves that you must not | |
1851| | trust computers' computations| |
Martin v. Löwis87184592008-06-04 06:29:55 +00001852+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001853| clock | analog clock showing time | turtles as clock's |
1854| | of your computer | hands, ontimer |
Martin v. Löwis87184592008-06-04 06:29:55 +00001855+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001856| colormixer | experiment with r, g, b | :func:`ondrag` |
Martin v. Löwis87184592008-06-04 06:29:55 +00001857+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001858| fractalcurves | Hilbert & Koch curves | recursion |
Martin v. Löwis87184592008-06-04 06:29:55 +00001859+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001860| lindenmayer | ethnomathematics | L-System |
1861| | (indian kolams) | |
Martin v. Löwis87184592008-06-04 06:29:55 +00001862+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001863| minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
1864| | | as Hanoi discs |
1865| | | (shape, shapesize) |
Martin v. Löwis87184592008-06-04 06:29:55 +00001866+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001867| paint | super minimalistic | :func:`onclick` |
1868| | drawing program | |
Martin v. Löwis87184592008-06-04 06:29:55 +00001869+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001870| peace | elementary | turtle: appearance |
1871| | | and animation |
Martin v. Löwis87184592008-06-04 06:29:55 +00001872+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001873| penrose | aperiodic tiling with | :func:`stamp` |
1874| | kites and darts | |
Martin v. Löwis87184592008-06-04 06:29:55 +00001875+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001876| planet_and_moon| simulation of | compound shapes, |
1877| | gravitational system | :class:`Vec2D` |
Martin v. Löwis87184592008-06-04 06:29:55 +00001878+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001879| tree | a (graphical) breadth | :func:`clone` |
Martin v. Löwis87184592008-06-04 06:29:55 +00001880| | first tree (using generators)| |
1881+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001882| wikipedia | a pattern from the wikipedia | :func:`clone`, |
1883| | article on turtle graphics | :func:`undo` |
Martin v. Löwis87184592008-06-04 06:29:55 +00001884+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00001885| yingyang | another elementary example | :func:`circle` |
Martin v. Löwis87184592008-06-04 06:29:55 +00001886+----------------+------------------------------+-----------------------+
Georg Brandl8ec7f652007-08-15 14:28:01 +00001887
Georg Brandla2b34b82008-06-04 11:17:26 +00001888Have fun!