blob: 98c07ca96fa1ce18c0c4aa83dfbfcf663cce69f6 [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öwis060cd1e2008-07-13 20:31:49 +00005.. module:: turtle
6 :synopsis: Turtle graphics for Tk
7.. sectionauthor:: Gregor Lingl <gregor.lingl@aon.at>
8
R. David Murrayb01c6e52009-04-30 12:42:32 +00009.. testsetup:: default
10
11 from turtle import *
12 turtle = Turtle()
13
Martin v. Löwis87184592008-06-04 06:29:55 +000014Introduction
Georg Brandla2b34b82008-06-04 11:17:26 +000015============
Martin v. Löwis87184592008-06-04 06:29:55 +000016
Georg Brandla2b34b82008-06-04 11:17:26 +000017Turtle graphics is a popular way for introducing programming to kids. It was
18part of the original Logo programming language developed by Wally Feurzig and
19Seymour Papert in 1966.
Martin v. Löwis87184592008-06-04 06:29:55 +000020
Sandro Tosi1381a312011-08-07 17:09:15 +020021Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it the
Georg Brandla2b34b82008-06-04 11:17:26 +000022command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
23direction it is facing, drawing a line as it moves. Give it the command
Sandro Tosi1381a312011-08-07 17:09:15 +020024``turtle.right(25)``, and it rotates in-place 25 degrees clockwise.
Martin v. Löwis87184592008-06-04 06:29:55 +000025
Georg Brandla2b34b82008-06-04 11:17:26 +000026By combining together these and similar commands, intricate shapes and pictures
27can easily be drawn.
Martin v. Löwis87184592008-06-04 06:29:55 +000028
Georg Brandla2b34b82008-06-04 11:17:26 +000029The :mod:`turtle` module is an extended reimplementation of the same-named
30module from the Python standard distribution up to version Python 2.5.
Martin v. Löwis87184592008-06-04 06:29:55 +000031
Georg Brandla2b34b82008-06-04 11:17:26 +000032It tries to keep the merits of the old turtle module and to be (nearly) 100%
33compatible with it. This means in the first place to enable the learning
34programmer to use all the commands, classes and methods interactively when using
35the module from within IDLE run with the ``-n`` switch.
Martin v. Löwis87184592008-06-04 06:29:55 +000036
Georg Brandla2b34b82008-06-04 11:17:26 +000037The turtle module provides turtle graphics primitives, in both object-oriented
38and procedure-oriented ways. Because it uses :mod:`Tkinter` for the underlying
Ezio Melotti062d2b52009-12-19 22:41:49 +000039graphics, it needs a version of Python installed with Tk support.
Martin v. Löwis87184592008-06-04 06:29:55 +000040
Georg Brandla2b34b82008-06-04 11:17:26 +000041The object-oriented interface uses essentially two+two classes:
Martin v. Löwis87184592008-06-04 06:29:55 +000042
Georg Brandla2b34b82008-06-04 11:17:26 +0000431. The :class:`TurtleScreen` class defines graphics windows as a playground for
44 the drawing turtles. Its constructor needs a :class:`Tkinter.Canvas` or a
45 :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is
46 used as part of some application.
Martin v. Löwis87184592008-06-04 06:29:55 +000047
Martin v. Löwise563aa42008-09-29 22:09:07 +000048 The function :func:`Screen` returns a singleton object of a
49 :class:`TurtleScreen` subclass. This function should be used when
50 :mod:`turtle` is used as a standalone tool for doing graphics.
51 As a singleton object, inheriting from its class is not possible.
Martin v. Löwis87184592008-06-04 06:29:55 +000052
Georg Brandla2b34b82008-06-04 11:17:26 +000053 All methods of TurtleScreen/Screen also exist as functions, i.e. as part of
54 the procedure-oriented interface.
Martin v. Löwis87184592008-06-04 06:29:55 +000055
Georg Brandla2b34b82008-06-04 11:17:26 +0000562. :class:`RawTurtle` (alias: :class:`RawPen`) defines Turtle objects which draw
57 on a :class:`TurtleScreen`. Its constructor needs a Canvas, ScrolledCanvas
58 or TurtleScreen as argument, so the RawTurtle objects know where to draw.
Martin v. Löwis87184592008-06-04 06:29:55 +000059
Georg Brandla2b34b82008-06-04 11:17:26 +000060 Derived from RawTurtle is the subclass :class:`Turtle` (alias: :class:`Pen`),
61 which draws on "the" :class:`Screen` - instance which is automatically
62 created, if not already present.
Martin v. Löwis87184592008-06-04 06:29:55 +000063
Georg Brandla2b34b82008-06-04 11:17:26 +000064 All methods of RawTurtle/Turtle also exist as functions, i.e. part of the
65 procedure-oriented interface.
Martin v. Löwis87184592008-06-04 06:29:55 +000066
Georg Brandla2b34b82008-06-04 11:17:26 +000067The procedural interface provides functions which are derived from the methods
68of the classes :class:`Screen` and :class:`Turtle`. They have the same names as
Georg Brandle83a4ad2009-03-13 19:03:58 +000069the corresponding methods. A screen object is automatically created whenever a
Georg Brandla2b34b82008-06-04 11:17:26 +000070function derived from a Screen method is called. An (unnamed) turtle object is
71automatically created whenever any of the functions derived from a Turtle method
72is called.
Martin v. Löwis87184592008-06-04 06:29:55 +000073
Georg Brandla2b34b82008-06-04 11:17:26 +000074To use multiple turtles an a screen one has to use the object-oriented interface.
Martin v. Löwis87184592008-06-04 06:29:55 +000075
Georg Brandla2b34b82008-06-04 11:17:26 +000076.. note::
77 In the following documentation the argument list for functions is given.
78 Methods, of course, have the additional first argument *self* which is
79 omitted here.
Martin v. Löwis87184592008-06-04 06:29:55 +000080
Martin v. Löwis87184592008-06-04 06:29:55 +000081
Georg Brandla2b34b82008-06-04 11:17:26 +000082Overview over available Turtle and Screen methods
83=================================================
Martin v. Löwis87184592008-06-04 06:29:55 +000084
Georg Brandla2b34b82008-06-04 11:17:26 +000085Turtle methods
Martin v. Löwis87184592008-06-04 06:29:55 +000086--------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000087
Georg Brandla2b34b82008-06-04 11:17:26 +000088Turtle motion
89 Move and draw
90 | :func:`forward` | :func:`fd`
91 | :func:`backward` | :func:`bk` | :func:`back`
92 | :func:`right` | :func:`rt`
93 | :func:`left` | :func:`lt`
94 | :func:`goto` | :func:`setpos` | :func:`setposition`
95 | :func:`setx`
96 | :func:`sety`
97 | :func:`setheading` | :func:`seth`
98 | :func:`home`
99 | :func:`circle`
100 | :func:`dot`
101 | :func:`stamp`
102 | :func:`clearstamp`
103 | :func:`clearstamps`
104 | :func:`undo`
105 | :func:`speed`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000106
Georg Brandla2b34b82008-06-04 11:17:26 +0000107 Tell Turtle's state
108 | :func:`position` | :func:`pos`
109 | :func:`towards`
110 | :func:`xcor`
111 | :func:`ycor`
112 | :func:`heading`
113 | :func:`distance`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114
Georg Brandla2b34b82008-06-04 11:17:26 +0000115 Setting and measurement
116 | :func:`degrees`
117 | :func:`radians`
118
119Pen control
120 Drawing state
121 | :func:`pendown` | :func:`pd` | :func:`down`
122 | :func:`penup` | :func:`pu` | :func:`up`
123 | :func:`pensize` | :func:`width`
124 | :func:`pen`
125 | :func:`isdown`
126
127 Color control
128 | :func:`color`
129 | :func:`pencolor`
130 | :func:`fillcolor`
131
132 Filling
133 | :func:`fill`
134 | :func:`begin_fill`
135 | :func:`end_fill`
136
137 More drawing control
138 | :func:`reset`
139 | :func:`clear`
140 | :func:`write`
141
142Turtle state
143 Visibility
144 | :func:`showturtle` | :func:`st`
145 | :func:`hideturtle` | :func:`ht`
146 | :func:`isvisible`
147
148 Appearance
149 | :func:`shape`
150 | :func:`resizemode`
151 | :func:`shapesize` | :func:`turtlesize`
152 | :func:`settiltangle`
153 | :func:`tiltangle`
154 | :func:`tilt`
155
156Using events
157 | :func:`onclick`
158 | :func:`onrelease`
159 | :func:`ondrag`
160
161Special Turtle methods
162 | :func:`begin_poly`
163 | :func:`end_poly`
164 | :func:`get_poly`
165 | :func:`clone`
166 | :func:`getturtle` | :func:`getpen`
167 | :func:`getscreen`
168 | :func:`setundobuffer`
169 | :func:`undobufferentries`
170 | :func:`tracer`
171 | :func:`window_width`
172 | :func:`window_height`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000173
Georg Brandl8ec7f652007-08-15 14:28:01 +0000174
Georg Brandla2b34b82008-06-04 11:17:26 +0000175Methods of TurtleScreen/Screen
176------------------------------
177
178Window control
179 | :func:`bgcolor`
180 | :func:`bgpic`
181 | :func:`clear` | :func:`clearscreen`
182 | :func:`reset` | :func:`resetscreen`
183 | :func:`screensize`
184 | :func:`setworldcoordinates`
185
186Animation control
187 | :func:`delay`
188 | :func:`tracer`
189 | :func:`update`
190
191Using screen events
192 | :func:`listen`
193 | :func:`onkey`
194 | :func:`onclick` | :func:`onscreenclick`
195 | :func:`ontimer`
196
197Settings and special methods
198 | :func:`mode`
199 | :func:`colormode`
200 | :func:`getcanvas`
201 | :func:`getshapes`
202 | :func:`register_shape` | :func:`addshape`
203 | :func:`turtles`
204 | :func:`window_height`
205 | :func:`window_width`
206
207Methods specific to Screen
208 | :func:`bye`
209 | :func:`exitonclick`
210 | :func:`setup`
211 | :func:`title`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000212
Georg Brandl8ec7f652007-08-15 14:28:01 +0000213
Georg Brandla2b34b82008-06-04 11:17:26 +0000214Methods of RawTurtle/Turtle and corresponding functions
215=======================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000216
Georg Brandla2b34b82008-06-04 11:17:26 +0000217Most of the examples in this section refer to a Turtle instance called
218``turtle``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000219
Georg Brandla2b34b82008-06-04 11:17:26 +0000220Turtle motion
221-------------
222
223.. function:: forward(distance)
224 fd(distance)
225
226 :param distance: a number (integer or float)
227
228 Move the turtle forward by the specified *distance*, in the direction the
229 turtle is headed.
230
R. David Murrayb01c6e52009-04-30 12:42:32 +0000231 .. doctest::
232
233 >>> turtle.position()
234 (0.00,0.00)
235 >>> turtle.forward(25)
236 >>> turtle.position()
237 (25.00,0.00)
238 >>> turtle.forward(-75)
239 >>> turtle.position()
240 (-50.00,0.00)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000241
242
Georg Brandla2b34b82008-06-04 11:17:26 +0000243.. function:: back(distance)
244 bk(distance)
245 backward(distance)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000246
Georg Brandla2b34b82008-06-04 11:17:26 +0000247 :param distance: a number
Georg Brandl8ec7f652007-08-15 14:28:01 +0000248
Georg Brandla2b34b82008-06-04 11:17:26 +0000249 Move the turtle backward by *distance*, opposite to the direction the
250 turtle is headed. Do not change the turtle's heading.
251
R. David Murrayb01c6e52009-04-30 12:42:32 +0000252 .. doctest::
253 :hide:
254
255 >>> turtle.goto(0, 0)
256
257 .. doctest::
258
259 >>> turtle.position()
260 (0.00,0.00)
261 >>> turtle.backward(30)
262 >>> turtle.position()
263 (-30.00,0.00)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000264
Georg Brandl8ec7f652007-08-15 14:28:01 +0000265
Georg Brandla2b34b82008-06-04 11:17:26 +0000266.. function:: right(angle)
267 rt(angle)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000268
Georg Brandla2b34b82008-06-04 11:17:26 +0000269 :param angle: a number (integer or float)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000270
Georg Brandla2b34b82008-06-04 11:17:26 +0000271 Turn turtle right by *angle* units. (Units are by default degrees, but
272 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
273 orientation depends on the turtle mode, see :func:`mode`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000274
R. David Murrayb01c6e52009-04-30 12:42:32 +0000275 .. doctest::
276 :hide:
277
278 >>> turtle.setheading(22)
279
280 .. doctest::
281
282 >>> turtle.heading()
283 22.0
284 >>> turtle.right(45)
285 >>> turtle.heading()
286 337.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000287
Georg Brandl8ec7f652007-08-15 14:28:01 +0000288
Georg Brandla2b34b82008-06-04 11:17:26 +0000289.. function:: left(angle)
290 lt(angle)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000291
Georg Brandla2b34b82008-06-04 11:17:26 +0000292 :param angle: a number (integer or float)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000293
Georg Brandla2b34b82008-06-04 11:17:26 +0000294 Turn turtle left by *angle* units. (Units are by default degrees, but
295 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
296 orientation depends on the turtle mode, see :func:`mode`.
297
R. David Murrayb01c6e52009-04-30 12:42:32 +0000298 .. doctest::
299 :hide:
300
301 >>> turtle.setheading(22)
302
303 .. doctest::
304
305 >>> turtle.heading()
306 22.0
307 >>> turtle.left(45)
308 >>> turtle.heading()
309 67.0
310
Georg Brandla2b34b82008-06-04 11:17:26 +0000311
312.. function:: goto(x, y=None)
313 setpos(x, y=None)
314 setposition(x, y=None)
315
R. David Murrayb01c6e52009-04-30 12:42:32 +0000316 :param x: a number or a pair/vector of numbers
317 :param y: a number or ``None``
Georg Brandla2b34b82008-06-04 11:17:26 +0000318
R. David Murrayb01c6e52009-04-30 12:42:32 +0000319 If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D`
320 (e.g. as returned by :func:`pos`).
Georg Brandla2b34b82008-06-04 11:17:26 +0000321
R. David Murrayb01c6e52009-04-30 12:42:32 +0000322 Move turtle to an absolute position. If the pen is down, draw line. Do
323 not change the turtle's orientation.
Georg Brandla2b34b82008-06-04 11:17:26 +0000324
R. David Murrayb01c6e52009-04-30 12:42:32 +0000325 .. doctest::
326 :hide:
327
328 >>> turtle.goto(0, 0)
329
330 .. doctest::
331
332 >>> tp = turtle.pos()
333 >>> tp
334 (0.00,0.00)
335 >>> turtle.setpos(60,30)
336 >>> turtle.pos()
337 (60.00,30.00)
338 >>> turtle.setpos((20,80))
339 >>> turtle.pos()
340 (20.00,80.00)
341 >>> turtle.setpos(tp)
342 >>> turtle.pos()
343 (0.00,0.00)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000344
Georg Brandl8ec7f652007-08-15 14:28:01 +0000345
Georg Brandla2b34b82008-06-04 11:17:26 +0000346.. function:: setx(x)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000347
Georg Brandla2b34b82008-06-04 11:17:26 +0000348 :param x: a number (integer or float)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000349
Georg Brandla2b34b82008-06-04 11:17:26 +0000350 Set the turtle's first coordinate to *x*, leave second coordinate
351 unchanged.
352
R. David Murrayb01c6e52009-04-30 12:42:32 +0000353 .. doctest::
354 :hide:
355
356 >>> turtle.goto(0, 240)
357
358 .. doctest::
359
360 >>> turtle.position()
361 (0.00,240.00)
362 >>> turtle.setx(10)
363 >>> turtle.position()
364 (10.00,240.00)
Georg Brandla2b34b82008-06-04 11:17:26 +0000365
366
367.. function:: sety(y)
368
369 :param y: a number (integer or float)
370
Andrew M. Kuchling847c43a2009-01-13 13:40:54 +0000371 Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
Georg Brandla2b34b82008-06-04 11:17:26 +0000372
R. David Murrayb01c6e52009-04-30 12:42:32 +0000373 .. doctest::
374 :hide:
375
376 >>> turtle.goto(0, 40)
377
378 .. doctest::
379
380 >>> turtle.position()
381 (0.00,40.00)
382 >>> turtle.sety(-10)
383 >>> turtle.position()
384 (0.00,-10.00)
Georg Brandla2b34b82008-06-04 11:17:26 +0000385
386
387.. function:: setheading(to_angle)
388 seth(to_angle)
389
390 :param to_angle: a number (integer or float)
391
392 Set the orientation of the turtle to *to_angle*. Here are some common
393 directions in degrees:
394
395 =================== ====================
396 standard mode logo mode
397 =================== ====================
398 0 - east 0 - north
399 90 - north 90 - east
400 180 - west 180 - south
401 270 - south 270 - west
402 =================== ====================
403
R. David Murrayb01c6e52009-04-30 12:42:32 +0000404 .. doctest::
405
406 >>> turtle.setheading(90)
407 >>> turtle.heading()
408 90.0
Georg Brandla2b34b82008-06-04 11:17:26 +0000409
410
411.. function:: home()
412
413 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
414 its start-orientation (which depends on the mode, see :func:`mode`).
415
R. David Murrayb01c6e52009-04-30 12:42:32 +0000416 .. doctest::
417 :hide:
418
419 >>> turtle.setheading(90)
420 >>> turtle.goto(0, -10)
421
422 .. doctest::
423
424 >>> turtle.heading()
425 90.0
426 >>> turtle.position()
427 (0.00,-10.00)
428 >>> turtle.home()
429 >>> turtle.position()
430 (0.00,0.00)
431 >>> turtle.heading()
432 0.0
433
Georg Brandla2b34b82008-06-04 11:17:26 +0000434
435.. function:: circle(radius, extent=None, steps=None)
436
437 :param radius: a number
438 :param extent: a number (or ``None``)
439 :param steps: an integer (or ``None``)
440
441 Draw a circle with given *radius*. The center is *radius* units left of
442 the turtle; *extent* -- an angle -- determines which part of the circle
443 is drawn. If *extent* is not given, draw the entire circle. If *extent*
444 is not a full circle, one endpoint of the arc is the current pen
445 position. Draw the arc in counterclockwise direction if *radius* is
446 positive, otherwise in clockwise direction. Finally the direction of the
447 turtle is changed by the amount of *extent*.
448
449 As the circle is approximated by an inscribed regular polygon, *steps*
450 determines the number of steps to use. If not given, it will be
451 calculated automatically. May be used to draw regular polygons.
452
R. David Murrayb01c6e52009-04-30 12:42:32 +0000453 .. doctest::
454
455 >>> turtle.home()
456 >>> turtle.position()
457 (0.00,0.00)
458 >>> turtle.heading()
459 0.0
460 >>> turtle.circle(50)
461 >>> turtle.position()
462 (-0.00,0.00)
463 >>> turtle.heading()
464 0.0
465 >>> turtle.circle(120, 180) # draw a semicircle
466 >>> turtle.position()
467 (0.00,240.00)
468 >>> turtle.heading()
469 180.0
Georg Brandla2b34b82008-06-04 11:17:26 +0000470
471
472.. function:: dot(size=None, *color)
473
474 :param size: an integer >= 1 (if given)
475 :param color: a colorstring or a numeric color tuple
476
477 Draw a circular dot with diameter *size*, using *color*. If *size* is
478 not given, the maximum of pensize+4 and 2*pensize is used.
479
R. David Murrayb01c6e52009-04-30 12:42:32 +0000480
481 .. doctest::
482
483 >>> turtle.home()
484 >>> turtle.dot()
485 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
486 >>> turtle.position()
487 (100.00,-0.00)
488 >>> turtle.heading()
489 0.0
Georg Brandla2b34b82008-06-04 11:17:26 +0000490
491
492.. function:: stamp()
493
494 Stamp a copy of the turtle shape onto the canvas at the current turtle
495 position. Return a stamp_id for that stamp, which can be used to delete
496 it by calling ``clearstamp(stamp_id)``.
497
R. David Murrayb01c6e52009-04-30 12:42:32 +0000498 .. doctest::
499
500 >>> turtle.color("blue")
501 >>> turtle.stamp()
502 11
503 >>> turtle.fd(50)
Georg Brandla2b34b82008-06-04 11:17:26 +0000504
505
506.. function:: clearstamp(stampid)
507
508 :param stampid: an integer, must be return value of previous
509 :func:`stamp` call
510
511 Delete stamp with given *stampid*.
512
R. David Murrayb01c6e52009-04-30 12:42:32 +0000513 .. doctest::
514
515 >>> turtle.position()
516 (150.00,-0.00)
517 >>> turtle.color("blue")
518 >>> astamp = turtle.stamp()
519 >>> turtle.fd(50)
520 >>> turtle.position()
521 (200.00,-0.00)
522 >>> turtle.clearstamp(astamp)
523 >>> turtle.position()
524 (200.00,-0.00)
Georg Brandla2b34b82008-06-04 11:17:26 +0000525
526
527.. function:: clearstamps(n=None)
528
529 :param n: an integer (or ``None``)
530
531 Delete all or first/last *n* of turtle's stamps. If *n* is None, delete
532 all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete
533 last *n* stamps.
534
R. David Murrayb01c6e52009-04-30 12:42:32 +0000535 .. doctest::
536
537 >>> for i in range(8):
538 ... turtle.stamp(); turtle.fd(30)
539 13
540 14
541 15
542 16
543 17
544 18
545 19
546 20
547 >>> turtle.clearstamps(2)
548 >>> turtle.clearstamps(-2)
549 >>> turtle.clearstamps()
Georg Brandla2b34b82008-06-04 11:17:26 +0000550
551
552.. function:: undo()
553
554 Undo (repeatedly) the last turtle action(s). Number of available
555 undo actions is determined by the size of the undobuffer.
556
R. David Murrayb01c6e52009-04-30 12:42:32 +0000557 .. doctest::
558
559 >>> for i in range(4):
560 ... turtle.fd(50); turtle.lt(80)
561 ...
562 >>> for i in range(8):
563 ... turtle.undo()
Georg Brandla2b34b82008-06-04 11:17:26 +0000564
565
566.. function:: speed(speed=None)
567
568 :param speed: an integer in the range 0..10 or a speedstring (see below)
569
570 Set the turtle's speed to an integer value in the range 0..10. If no
571 argument is given, return current speed.
572
573 If input is a number greater than 10 or smaller than 0.5, speed is set
574 to 0. Speedstrings are mapped to speedvalues as follows:
575
576 * "fastest": 0
577 * "fast": 10
578 * "normal": 6
579 * "slow": 3
580 * "slowest": 1
581
582 Speeds from 1 to 10 enforce increasingly faster animation of line drawing
583 and turtle turning.
584
585 Attention: *speed* = 0 means that *no* animation takes
586 place. forward/back makes turtle jump and likewise left/right make the
587 turtle turn instantly.
588
R. David Murrayb01c6e52009-04-30 12:42:32 +0000589 .. doctest::
590
591 >>> turtle.speed()
592 3
593 >>> turtle.speed('normal')
594 >>> turtle.speed()
595 6
596 >>> turtle.speed(9)
597 >>> turtle.speed()
598 9
Georg Brandla2b34b82008-06-04 11:17:26 +0000599
600
601Tell Turtle's state
Martin v. Löwis87184592008-06-04 06:29:55 +0000602-------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000603
Georg Brandla2b34b82008-06-04 11:17:26 +0000604.. function:: position()
605 pos()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000606
Georg Brandla2b34b82008-06-04 11:17:26 +0000607 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000608
R. David Murrayb01c6e52009-04-30 12:42:32 +0000609 .. doctest::
610
611 >>> turtle.pos()
612 (440.00,-0.00)
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:: towards(x, y=None)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000616
Georg Brandla2b34b82008-06-04 11:17:26 +0000617 :param x: a number or a pair/vector of numbers or a turtle instance
618 :param y: a number if *x* is a number, else ``None``
Georg Brandl8ec7f652007-08-15 14:28:01 +0000619
Georg Brandla2b34b82008-06-04 11:17:26 +0000620 Return the angle between the line from turtle position to position specified
621 by (x,y), the vector or the other turtle. This depends on the turtle's start
622 orientation which depends on the mode - "standard"/"world" or "logo").
Georg Brandl8ec7f652007-08-15 14:28:01 +0000623
R. David Murrayb01c6e52009-04-30 12:42:32 +0000624 .. doctest::
625
626 >>> turtle.goto(10, 10)
627 >>> turtle.towards(0,0)
628 225.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000629
630
Georg Brandla2b34b82008-06-04 11:17:26 +0000631.. function:: xcor()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000632
Georg Brandla2b34b82008-06-04 11:17:26 +0000633 Return the turtle's x coordinate.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000634
R. David Murrayb01c6e52009-04-30 12:42:32 +0000635 .. doctest::
636
637 >>> turtle.home()
638 >>> turtle.left(50)
639 >>> turtle.forward(100)
640 >>> turtle.pos()
641 (64.28,76.60)
642 >>> print turtle.xcor()
643 64.2787609687
Georg Brandl8ec7f652007-08-15 14:28:01 +0000644
Georg Brandl8ec7f652007-08-15 14:28:01 +0000645
Georg Brandla2b34b82008-06-04 11:17:26 +0000646.. function:: ycor()
647
648 Return the turtle's y coordinate.
649
R. David Murrayb01c6e52009-04-30 12:42:32 +0000650 .. doctest::
651
652 >>> turtle.home()
653 >>> turtle.left(60)
654 >>> turtle.forward(100)
655 >>> print turtle.pos()
656 (50.00,86.60)
657 >>> print turtle.ycor()
658 86.6025403784
Georg Brandl8ec7f652007-08-15 14:28:01 +0000659
Georg Brandl8ec7f652007-08-15 14:28:01 +0000660
Georg Brandla2b34b82008-06-04 11:17:26 +0000661.. function:: heading()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000662
Georg Brandla2b34b82008-06-04 11:17:26 +0000663 Return the turtle's current heading (value depends on the turtle mode, see
664 :func:`mode`).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000665
R. David Murrayb01c6e52009-04-30 12:42:32 +0000666 .. doctest::
667
668 >>> turtle.home()
669 >>> turtle.left(67)
670 >>> turtle.heading()
671 67.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000672
673
Georg Brandla2b34b82008-06-04 11:17:26 +0000674.. function:: distance(x, y=None)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000675
Georg Brandla2b34b82008-06-04 11:17:26 +0000676 :param x: a number or a pair/vector of numbers or a turtle instance
677 :param y: a number if *x* is a number, else ``None``
Georg Brandl8ec7f652007-08-15 14:28:01 +0000678
Georg Brandla2b34b82008-06-04 11:17:26 +0000679 Return the distance from the turtle to (x,y), the given vector, or the given
680 other turtle, in turtle step units.
681
R. David Murrayb01c6e52009-04-30 12:42:32 +0000682 .. doctest::
683
684 >>> turtle.home()
685 >>> turtle.distance(30,40)
686 50.0
687 >>> turtle.distance((30,40))
688 50.0
689 >>> joe = Turtle()
690 >>> joe.forward(77)
691 >>> turtle.distance(joe)
692 77.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000693
Georg Brandl8ec7f652007-08-15 14:28:01 +0000694
Georg Brandla2b34b82008-06-04 11:17:26 +0000695Settings for measurement
696------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000697
Georg Brandla2b34b82008-06-04 11:17:26 +0000698.. function:: degrees(fullcircle=360.0)
699
700 :param fullcircle: a number
701
702 Set angle measurement units, i.e. set number of "degrees" for a full circle.
703 Default value is 360 degrees.
704
R. David Murrayb01c6e52009-04-30 12:42:32 +0000705 .. doctest::
706
707 >>> turtle.home()
708 >>> turtle.left(90)
709 >>> turtle.heading()
710 90.0
Alexander Belopolskyab016d22010-11-05 01:56:24 +0000711
712 Change angle measurement unit to grad (also known as gon,
713 grade, or gradian and equals 1/100-th of the right angle.)
714 >>> turtle.degrees(400.0)
R. David Murrayb01c6e52009-04-30 12:42:32 +0000715 >>> turtle.heading()
716 100.0
717 >>> turtle.degrees(360)
718 >>> turtle.heading()
719 90.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000720
Georg Brandl8ec7f652007-08-15 14:28:01 +0000721
Georg Brandla2b34b82008-06-04 11:17:26 +0000722.. function:: radians()
723
724 Set the angle measurement units to radians. Equivalent to
725 ``degrees(2*math.pi)``.
726
R. David Murrayb01c6e52009-04-30 12:42:32 +0000727 .. doctest::
728
729 >>> turtle.home()
730 >>> turtle.left(90)
731 >>> turtle.heading()
732 90.0
733 >>> turtle.radians()
734 >>> turtle.heading()
735 1.5707963267948966
736
737 .. doctest::
738 :hide:
739
740 >>> turtle.degrees(360)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000741
742
Georg Brandla2b34b82008-06-04 11:17:26 +0000743Pen control
744-----------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000745
Georg Brandla2b34b82008-06-04 11:17:26 +0000746Drawing state
747~~~~~~~~~~~~~
748
749.. function:: pendown()
750 pd()
751 down()
752
753 Pull the pen down -- drawing when moving.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000754
755
Georg Brandla2b34b82008-06-04 11:17:26 +0000756.. function:: penup()
757 pu()
758 up()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000759
Georg Brandla2b34b82008-06-04 11:17:26 +0000760 Pull the pen up -- no drawing when moving.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000761
Georg Brandl8ec7f652007-08-15 14:28:01 +0000762
Georg Brandla2b34b82008-06-04 11:17:26 +0000763.. function:: pensize(width=None)
764 width(width=None)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000765
Georg Brandla2b34b82008-06-04 11:17:26 +0000766 :param width: a positive number
767
768 Set the line thickness to *width* or return it. If resizemode is set to
769 "auto" and turtleshape is a polygon, that polygon is drawn with the same line
770 thickness. If no argument is given, the current pensize is returned.
771
R. David Murrayb01c6e52009-04-30 12:42:32 +0000772 .. doctest::
773
774 >>> turtle.pensize()
775 1
776 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
Georg Brandl8ec7f652007-08-15 14:28:01 +0000777
Georg Brandl8ec7f652007-08-15 14:28:01 +0000778
Georg Brandla2b34b82008-06-04 11:17:26 +0000779.. function:: pen(pen=None, **pendict)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000780
Georg Brandla2b34b82008-06-04 11:17:26 +0000781 :param pen: a dictionary with some or all of the below listed keys
782 :param pendict: one or more keyword-arguments with the below listed keys as keywords
Georg Brandl8ec7f652007-08-15 14:28:01 +0000783
Georg Brandla2b34b82008-06-04 11:17:26 +0000784 Return or set the pen's attributes in a "pen-dictionary" with the following
785 key/value pairs:
786
787 * "shown": True/False
788 * "pendown": True/False
789 * "pencolor": color-string or color-tuple
790 * "fillcolor": color-string or color-tuple
791 * "pensize": positive number
792 * "speed": number in range 0..10
793 * "resizemode": "auto" or "user" or "noresize"
794 * "stretchfactor": (positive number, positive number)
795 * "outline": positive number
796 * "tilt": number
797
R. David Murrayb01c6e52009-04-30 12:42:32 +0000798 This dictionary can be used as argument for a subsequent call to :func:`pen`
Georg Brandla2b34b82008-06-04 11:17:26 +0000799 to restore the former pen-state. Moreover one or more of these attributes
800 can be provided as keyword-arguments. This can be used to set several pen
801 attributes in one statement.
802
R. David Murrayb01c6e52009-04-30 12:42:32 +0000803 .. doctest::
804 :options: +NORMALIZE_WHITESPACE
805
806 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
807 >>> sorted(turtle.pen().items())
808 [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
809 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
810 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
811 >>> penstate=turtle.pen()
812 >>> turtle.color("yellow", "")
813 >>> turtle.penup()
814 >>> sorted(turtle.pen().items())
815 [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow'),
816 ('pendown', False), ('pensize', 10), ('resizemode', 'noresize'),
817 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
818 >>> turtle.pen(penstate, fillcolor="green")
819 >>> sorted(turtle.pen().items())
820 [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red'),
821 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
822 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
Georg Brandla2b34b82008-06-04 11:17:26 +0000823
824
825.. function:: isdown()
826
827 Return ``True`` if pen is down, ``False`` if it's up.
828
R. David Murrayb01c6e52009-04-30 12:42:32 +0000829 .. doctest::
830
831 >>> turtle.penup()
832 >>> turtle.isdown()
833 False
834 >>> turtle.pendown()
835 >>> turtle.isdown()
836 True
Georg Brandla2b34b82008-06-04 11:17:26 +0000837
838
839Color control
840~~~~~~~~~~~~~
841
842.. function:: pencolor(*args)
843
844 Return or set the pencolor.
845
846 Four input formats are allowed:
847
848 ``pencolor()``
R. David Murrayb01c6e52009-04-30 12:42:32 +0000849 Return the current pencolor as color specification string or
850 as a tuple (see example). May be used as input to another
Georg Brandla2b34b82008-06-04 11:17:26 +0000851 color/pencolor/fillcolor call.
852
853 ``pencolor(colorstring)``
854 Set pencolor to *colorstring*, which is a Tk color specification string,
855 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
856
857 ``pencolor((r, g, b))``
858 Set pencolor to the RGB color represented by the tuple of *r*, *g*, and
859 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
860 colormode is either 1.0 or 255 (see :func:`colormode`).
861
862 ``pencolor(r, g, b)``
863 Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of
864 *r*, *g*, and *b* must be in the range 0..colormode.
865
866 If turtleshape is a polygon, the outline of that polygon is drawn with the
867 newly set pencolor.
868
R. David Murrayb01c6e52009-04-30 12:42:32 +0000869 .. doctest::
870
871 >>> colormode()
872 1.0
873 >>> turtle.pencolor()
874 'red'
875 >>> turtle.pencolor("brown")
876 >>> turtle.pencolor()
877 'brown'
878 >>> tup = (0.2, 0.8, 0.55)
879 >>> turtle.pencolor(tup)
880 >>> turtle.pencolor()
Mark Dickinson6b87f112009-11-24 14:27:02 +0000881 (0.2, 0.8, 0.5490196078431373)
R. David Murrayb01c6e52009-04-30 12:42:32 +0000882 >>> colormode(255)
883 >>> turtle.pencolor()
884 (51, 204, 140)
885 >>> turtle.pencolor('#32c18f')
886 >>> turtle.pencolor()
887 (50, 193, 143)
Georg Brandla2b34b82008-06-04 11:17:26 +0000888
889
890.. function:: fillcolor(*args)
891
892 Return or set the fillcolor.
893
894 Four input formats are allowed:
895
896 ``fillcolor()``
R. David Murrayb01c6e52009-04-30 12:42:32 +0000897 Return the current fillcolor as color specification string, possibly
898 in tuple format (see example). May be used as input to another
Georg Brandla2b34b82008-06-04 11:17:26 +0000899 color/pencolor/fillcolor call.
900
901 ``fillcolor(colorstring)``
902 Set fillcolor to *colorstring*, which is a Tk color specification string,
903 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
904
905 ``fillcolor((r, g, b))``
906 Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and
907 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
908 colormode is either 1.0 or 255 (see :func:`colormode`).
909
910 ``fillcolor(r, g, b)``
911 Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of
912 *r*, *g*, and *b* must be in the range 0..colormode.
913
914 If turtleshape is a polygon, the interior of that polygon is drawn
915 with the newly set fillcolor.
916
R. David Murrayb01c6e52009-04-30 12:42:32 +0000917 .. doctest::
918
919 >>> turtle.fillcolor("violet")
920 >>> turtle.fillcolor()
921 'violet'
922 >>> col = turtle.pencolor()
923 >>> col
924 (50, 193, 143)
925 >>> turtle.fillcolor(col)
926 >>> turtle.fillcolor()
927 (50, 193, 143)
928 >>> turtle.fillcolor('#ffffff')
929 >>> turtle.fillcolor()
930 (255, 255, 255)
Georg Brandla2b34b82008-06-04 11:17:26 +0000931
932
933.. function:: color(*args)
934
935 Return or set pencolor and fillcolor.
936
937 Several input formats are allowed. They use 0 to 3 arguments as
938 follows:
939
940 ``color()``
941 Return the current pencolor and the current fillcolor as a pair of color
R. David Murrayb01c6e52009-04-30 12:42:32 +0000942 specification strings or tuples as returned by :func:`pencolor` and
Georg Brandla2b34b82008-06-04 11:17:26 +0000943 :func:`fillcolor`.
944
945 ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)``
946 Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the
947 given value.
948
949 ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))``
950 Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)``
951 and analogously if the other input format is used.
952
953 If turtleshape is a polygon, outline and interior of that polygon is drawn
954 with the newly set colors.
955
R. David Murrayb01c6e52009-04-30 12:42:32 +0000956 .. doctest::
957
958 >>> turtle.color("red", "green")
959 >>> turtle.color()
960 ('red', 'green')
961 >>> color("#285078", "#a0c8f0")
962 >>> color()
963 ((40, 80, 120), (160, 200, 240))
Georg Brandla2b34b82008-06-04 11:17:26 +0000964
965
966See also: Screen method :func:`colormode`.
967
968
969Filling
970~~~~~~~
971
R. David Murrayb01c6e52009-04-30 12:42:32 +0000972.. doctest::
973 :hide:
974
975 >>> turtle.home()
976
Georg Brandla2b34b82008-06-04 11:17:26 +0000977.. function:: fill(flag)
978
979 :param flag: True/False (or 1/0 respectively)
980
981 Call ``fill(True)`` before drawing the shape you want to fill, and
982 ``fill(False)`` when done. When used without argument: return fillstate
983 (``True`` if filling, ``False`` else).
984
R. David Murrayb01c6e52009-04-30 12:42:32 +0000985 .. doctest::
986
987 >>> turtle.fill(True)
988 >>> for _ in range(3):
989 ... turtle.forward(100)
990 ... turtle.left(120)
991 ...
992 >>> turtle.fill(False)
Georg Brandla2b34b82008-06-04 11:17:26 +0000993
994
995.. function:: begin_fill()
996
997 Call just before drawing a shape to be filled. Equivalent to ``fill(True)``.
998
Georg Brandla2b34b82008-06-04 11:17:26 +0000999
1000.. function:: end_fill()
1001
1002 Fill the shape drawn after the last call to :func:`begin_fill`. Equivalent
1003 to ``fill(False)``.
1004
R. David Murrayb01c6e52009-04-30 12:42:32 +00001005 .. doctest::
1006
1007 >>> turtle.color("black", "red")
1008 >>> turtle.begin_fill()
1009 >>> turtle.circle(80)
1010 >>> turtle.end_fill()
1011
Georg Brandla2b34b82008-06-04 11:17:26 +00001012
1013More drawing control
1014~~~~~~~~~~~~~~~~~~~~
1015
1016.. function:: reset()
1017
1018 Delete the turtle's drawings from the screen, re-center the turtle and set
1019 variables to the default values.
1020
R. David Murrayb01c6e52009-04-30 12:42:32 +00001021 .. doctest::
1022
1023 >>> turtle.goto(0,-22)
1024 >>> turtle.left(100)
1025 >>> turtle.position()
1026 (0.00,-22.00)
1027 >>> turtle.heading()
1028 100.0
1029 >>> turtle.reset()
1030 >>> turtle.position()
1031 (0.00,0.00)
1032 >>> turtle.heading()
1033 0.0
Georg Brandla2b34b82008-06-04 11:17:26 +00001034
1035
1036.. function:: clear()
1037
1038 Delete the turtle's drawings from the screen. Do not move turtle. State and
1039 position of the turtle as well as drawings of other turtles are not affected.
1040
1041
1042.. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal"))
1043
1044 :param arg: object to be written to the TurtleScreen
1045 :param move: True/False
1046 :param align: one of the strings "left", "center" or right"
1047 :param font: a triple (fontname, fontsize, fonttype)
1048
1049 Write text - the string representation of *arg* - at the current turtle
1050 position according to *align* ("left", "center" or right") and with the given
1051 font. If *move* is True, the pen is moved to the bottom-right corner of the
1052 text. By default, *move* is False.
1053
1054 >>> turtle.write("Home = ", True, align="center")
1055 >>> turtle.write((0,0), True)
1056
1057
1058Turtle state
1059------------
1060
1061Visibility
1062~~~~~~~~~~
1063
Georg Brandla2b34b82008-06-04 11:17:26 +00001064.. function:: hideturtle()
1065 ht()
1066
1067 Make the turtle invisible. It's a good idea to do this while you're in the
1068 middle of doing some complex drawing, because hiding the turtle speeds up the
1069 drawing observably.
1070
R. David Murrayb01c6e52009-04-30 12:42:32 +00001071 .. doctest::
1072
1073 >>> turtle.hideturtle()
1074
1075
1076.. function:: showturtle()
1077 st()
1078
1079 Make the turtle visible.
1080
1081 .. doctest::
1082
1083 >>> turtle.showturtle()
Georg Brandla2b34b82008-06-04 11:17:26 +00001084
1085
1086.. function:: isvisible()
1087
1088 Return True if the Turtle is shown, False if it's hidden.
1089
1090 >>> turtle.hideturtle()
R. David Murrayb01c6e52009-04-30 12:42:32 +00001091 >>> turtle.isvisible()
Georg Brandla2b34b82008-06-04 11:17:26 +00001092 False
R. David Murrayb01c6e52009-04-30 12:42:32 +00001093 >>> turtle.showturtle()
1094 >>> turtle.isvisible()
1095 True
Georg Brandla2b34b82008-06-04 11:17:26 +00001096
1097
1098Appearance
1099~~~~~~~~~~
1100
1101.. function:: shape(name=None)
1102
1103 :param name: a string which is a valid shapename
1104
1105 Set turtle shape to shape with given *name* or, if name is not given, return
1106 name of current shape. Shape with *name* must exist in the TurtleScreen's
1107 shape dictionary. Initially there are the following polygon shapes: "arrow",
1108 "turtle", "circle", "square", "triangle", "classic". To learn about how to
1109 deal with shapes see Screen method :func:`register_shape`.
1110
R. David Murrayb01c6e52009-04-30 12:42:32 +00001111 .. doctest::
1112
1113 >>> turtle.shape()
1114 'classic'
1115 >>> turtle.shape("turtle")
1116 >>> turtle.shape()
1117 'turtle'
Georg Brandla2b34b82008-06-04 11:17:26 +00001118
1119
1120.. function:: resizemode(rmode=None)
1121
1122 :param rmode: one of the strings "auto", "user", "noresize"
1123
1124 Set resizemode to one of the values: "auto", "user", "noresize". If *rmode*
1125 is not given, return current resizemode. Different resizemodes have the
1126 following effects:
1127
1128 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
1129 - "user": adapts the appearance of the turtle according to the values of
1130 stretchfactor and outlinewidth (outline), which are set by
1131 :func:`shapesize`.
1132 - "noresize": no adaption of the turtle's appearance takes place.
1133
1134 resizemode("user") is called by :func:`shapesize` when used with arguments.
1135
R. David Murrayb01c6e52009-04-30 12:42:32 +00001136 .. doctest::
1137
1138 >>> turtle.resizemode()
1139 'noresize'
1140 >>> turtle.resizemode("auto")
1141 >>> turtle.resizemode()
1142 'auto'
Georg Brandla2b34b82008-06-04 11:17:26 +00001143
1144
1145.. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None)
R. David Murray5c3d40e2009-06-25 14:21:06 +00001146 turtlesize(stretch_wid=None, stretch_len=None, outline=None)
Georg Brandla2b34b82008-06-04 11:17:26 +00001147
1148 :param stretch_wid: positive number
1149 :param stretch_len: positive number
1150 :param outline: positive number
1151
1152 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set
1153 resizemode to "user". If and only if resizemode is set to "user", the turtle
1154 will be displayed stretched according to its stretchfactors: *stretch_wid* is
1155 stretchfactor perpendicular to its orientation, *stretch_len* is
1156 stretchfactor in direction of its orientation, *outline* determines the width
1157 of the shapes's outline.
1158
R. David Murrayb01c6e52009-04-30 12:42:32 +00001159 .. doctest::
1160
1161 >>> turtle.shapesize()
1162 (1, 1, 1)
1163 >>> turtle.resizemode("user")
1164 >>> turtle.shapesize(5, 5, 12)
1165 >>> turtle.shapesize()
1166 (5, 5, 12)
1167 >>> turtle.shapesize(outline=8)
1168 >>> turtle.shapesize()
1169 (5, 5, 8)
Georg Brandla2b34b82008-06-04 11:17:26 +00001170
1171
1172.. function:: tilt(angle)
1173
1174 :param angle: a number
1175
1176 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
1177 change the turtle's heading (direction of movement).
1178
R. David Murrayb01c6e52009-04-30 12:42:32 +00001179 .. doctest::
1180
1181 >>> turtle.reset()
1182 >>> turtle.shape("circle")
1183 >>> turtle.shapesize(5,2)
1184 >>> turtle.tilt(30)
1185 >>> turtle.fd(50)
1186 >>> turtle.tilt(30)
1187 >>> turtle.fd(50)
Georg Brandla2b34b82008-06-04 11:17:26 +00001188
1189
1190.. function:: settiltangle(angle)
1191
1192 :param angle: a number
1193
1194 Rotate the turtleshape to point in the direction specified by *angle*,
1195 regardless of its current tilt-angle. *Do not* change the turtle's heading
1196 (direction of movement).
1197
R. David Murrayb01c6e52009-04-30 12:42:32 +00001198 .. doctest::
1199
1200 >>> turtle.reset()
1201 >>> turtle.shape("circle")
1202 >>> turtle.shapesize(5,2)
1203 >>> turtle.settiltangle(45)
1204 >>> turtle.fd(50)
1205 >>> turtle.settiltangle(-45)
1206 >>> turtle.fd(50)
Georg Brandla2b34b82008-06-04 11:17:26 +00001207
1208
1209.. function:: tiltangle()
1210
1211 Return the current tilt-angle, i.e. the angle between the orientation of the
1212 turtleshape and the heading of the turtle (its direction of movement).
1213
R. David Murrayb01c6e52009-04-30 12:42:32 +00001214 .. doctest::
1215
1216 >>> turtle.reset()
1217 >>> turtle.shape("circle")
1218 >>> turtle.shapesize(5,2)
1219 >>> turtle.tilt(45)
1220 >>> turtle.tiltangle()
1221 45.0
Georg Brandla2b34b82008-06-04 11:17:26 +00001222
1223
1224Using events
1225------------
1226
1227.. function:: onclick(fun, btn=1, add=None)
1228
1229 :param fun: a function with two arguments which will be called with the
1230 coordinates of the clicked point on the canvas
1231 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1232 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1233 added, otherwise it will replace a former binding
1234
1235 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1236 existing bindings are removed. Example for the anonymous turtle, i.e. the
1237 procedural way:
1238
R. David Murrayb01c6e52009-04-30 12:42:32 +00001239 .. doctest::
1240
1241 >>> def turn(x, y):
1242 ... left(180)
1243 ...
1244 >>> onclick(turn) # Now clicking into the turtle will turn it.
1245 >>> onclick(None) # event-binding will be removed
Georg Brandla2b34b82008-06-04 11:17:26 +00001246
1247
1248.. function:: onrelease(fun, btn=1, add=None)
1249
1250 :param fun: a function with two arguments which will be called with the
1251 coordinates of the clicked point on the canvas
1252 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1253 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1254 added, otherwise it will replace a former binding
1255
1256 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1257 ``None``, existing bindings are removed.
1258
R. David Murrayb01c6e52009-04-30 12:42:32 +00001259 .. doctest::
1260
1261 >>> class MyTurtle(Turtle):
1262 ... def glow(self,x,y):
1263 ... self.fillcolor("red")
1264 ... def unglow(self,x,y):
1265 ... self.fillcolor("")
1266 ...
1267 >>> turtle = MyTurtle()
1268 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1269 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
Georg Brandla2b34b82008-06-04 11:17:26 +00001270
1271
1272.. function:: ondrag(fun, btn=1, add=None)
1273
1274 :param fun: a function with two arguments which will be called with the
1275 coordinates of the clicked point on the canvas
1276 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1277 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1278 added, otherwise it will replace a former binding
1279
1280 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1281 existing bindings are removed.
1282
1283 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1284 mouse-click event on that turtle.
1285
R. David Murrayb01c6e52009-04-30 12:42:32 +00001286 .. doctest::
1287
1288 >>> turtle.ondrag(turtle.goto)
1289
1290 Subsequently, clicking and dragging the Turtle will move it across
1291 the screen thereby producing handdrawings (if pen is down).
Georg Brandla2b34b82008-06-04 11:17:26 +00001292
1293
1294Special Turtle methods
1295----------------------
1296
1297.. function:: begin_poly()
1298
1299 Start recording the vertices of a polygon. Current turtle position is first
1300 vertex of polygon.
1301
1302
1303.. function:: end_poly()
1304
1305 Stop recording the vertices of a polygon. Current turtle position is last
1306 vertex of polygon. This will be connected with the first vertex.
1307
1308
1309.. function:: get_poly()
1310
1311 Return the last recorded polygon.
1312
R. David Murrayb01c6e52009-04-30 12:42:32 +00001313 .. doctest::
1314
1315 >>> turtle.home()
1316 >>> turtle.begin_poly()
1317 >>> turtle.fd(100)
1318 >>> turtle.left(20)
1319 >>> turtle.fd(30)
1320 >>> turtle.left(60)
1321 >>> turtle.fd(50)
1322 >>> turtle.end_poly()
1323 >>> p = turtle.get_poly()
1324 >>> register_shape("myFavouriteShape", p)
Georg Brandla2b34b82008-06-04 11:17:26 +00001325
1326
1327.. function:: clone()
1328
1329 Create and return a clone of the turtle with same position, heading and
1330 turtle properties.
1331
R. David Murrayb01c6e52009-04-30 12:42:32 +00001332 .. doctest::
1333
1334 >>> mick = Turtle()
1335 >>> joe = mick.clone()
Georg Brandla2b34b82008-06-04 11:17:26 +00001336
1337
1338.. function:: getturtle()
R. David Murray5c3d40e2009-06-25 14:21:06 +00001339 getpen()
Georg Brandla2b34b82008-06-04 11:17:26 +00001340
1341 Return the Turtle object itself. Only reasonable use: as a function to
1342 return the "anonymous turtle":
1343
R. David Murrayb01c6e52009-04-30 12:42:32 +00001344 .. doctest::
1345
1346 >>> pet = getturtle()
1347 >>> pet.fd(50)
1348 >>> pet
1349 <turtle.Turtle object at 0x...>
Georg Brandla2b34b82008-06-04 11:17:26 +00001350
1351
1352.. function:: getscreen()
1353
1354 Return the :class:`TurtleScreen` object the turtle is drawing on.
1355 TurtleScreen methods can then be called for that object.
1356
R. David Murrayb01c6e52009-04-30 12:42:32 +00001357 .. doctest::
1358
1359 >>> ts = turtle.getscreen()
1360 >>> ts
1361 <turtle._Screen object at 0x...>
1362 >>> ts.bgcolor("pink")
Georg Brandla2b34b82008-06-04 11:17:26 +00001363
1364
1365.. function:: setundobuffer(size)
1366
1367 :param size: an integer or ``None``
1368
1369 Set or disable undobuffer. If *size* is an integer an empty undobuffer of
1370 given size is installed. *size* gives the maximum number of turtle actions
1371 that can be undone by the :func:`undo` method/function. If *size* is
1372 ``None``, the undobuffer is disabled.
1373
R. David Murrayb01c6e52009-04-30 12:42:32 +00001374 .. doctest::
1375
1376 >>> turtle.setundobuffer(42)
Georg Brandla2b34b82008-06-04 11:17:26 +00001377
1378
1379.. function:: undobufferentries()
1380
1381 Return number of entries in the undobuffer.
1382
R. David Murrayb01c6e52009-04-30 12:42:32 +00001383 .. doctest::
1384
1385 >>> while undobufferentries():
1386 ... undo()
Georg Brandla2b34b82008-06-04 11:17:26 +00001387
1388
1389.. function:: tracer(flag=None, delay=None)
1390
1391 A replica of the corresponding TurtleScreen method.
1392
1393 .. deprecated:: 2.6
1394
1395
1396.. function:: window_width()
1397 window_height()
1398
1399 Both are replicas of the corresponding TurtleScreen methods.
1400
1401 .. deprecated:: 2.6
1402
1403
1404.. _compoundshapes:
1405
1406Excursus about the use of compound shapes
1407-----------------------------------------
1408
1409To use compound turtle shapes, which consist of several polygons of different
1410color, you must use the helper class :class:`Shape` explicitly as described
1411below:
1412
14131. Create an empty Shape object of type "compound".
14142. Add as many components to this object as desired, using the
1415 :meth:`addcomponent` method.
1416
1417 For example:
1418
R. David Murrayb01c6e52009-04-30 12:42:32 +00001419 .. doctest::
1420
1421 >>> s = Shape("compound")
1422 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1423 >>> s.addcomponent(poly1, "red", "blue")
1424 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1425 >>> s.addcomponent(poly2, "blue", "red")
Georg Brandla2b34b82008-06-04 11:17:26 +00001426
14273. Now add the Shape to the Screen's shapelist and use it:
1428
R. David Murrayb01c6e52009-04-30 12:42:32 +00001429 .. doctest::
1430
1431 >>> register_shape("myshape", s)
1432 >>> shape("myshape")
Georg Brandla2b34b82008-06-04 11:17:26 +00001433
1434
1435.. note::
1436
1437 The :class:`Shape` class is used internally by the :func:`register_shape`
1438 method in different ways. The application programmer has to deal with the
1439 Shape class *only* when using compound shapes like shown above!
1440
1441
1442Methods of TurtleScreen/Screen and corresponding functions
1443==========================================================
1444
1445Most of the examples in this section refer to a TurtleScreen instance called
1446``screen``.
1447
R. David Murrayb01c6e52009-04-30 12:42:32 +00001448.. doctest::
1449 :hide:
1450
1451 >>> screen = Screen()
Georg Brandla2b34b82008-06-04 11:17:26 +00001452
1453Window control
1454--------------
1455
1456.. function:: bgcolor(*args)
1457
1458 :param args: a color string or three numbers in the range 0..colormode or a
1459 3-tuple of such numbers
1460
Alexander Belopolskyab016d22010-11-05 01:56:24 +00001461
Georg Brandla2b34b82008-06-04 11:17:26 +00001462 Set or return background color of the TurtleScreen.
1463
R. David Murrayb01c6e52009-04-30 12:42:32 +00001464 .. doctest::
1465
1466 >>> screen.bgcolor("orange")
1467 >>> screen.bgcolor()
1468 'orange'
1469 >>> screen.bgcolor("#800080")
1470 >>> screen.bgcolor()
1471 (128, 0, 128)
Georg Brandla2b34b82008-06-04 11:17:26 +00001472
1473
1474.. function:: bgpic(picname=None)
1475
1476 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
1477
1478 Set background image or return name of current backgroundimage. If *picname*
1479 is a filename, set the corresponding image as background. If *picname* is
1480 ``"nopic"``, delete background image, if present. If *picname* is ``None``,
R. David Murrayb01c6e52009-04-30 12:42:32 +00001481 return the filename of the current backgroundimage. ::
Georg Brandla2b34b82008-06-04 11:17:26 +00001482
R. David Murrayb01c6e52009-04-30 12:42:32 +00001483 >>> screen.bgpic()
1484 'nopic'
1485 >>> screen.bgpic("landscape.gif")
1486 >>> screen.bgpic()
1487 "landscape.gif"
Georg Brandla2b34b82008-06-04 11:17:26 +00001488
1489
1490.. function:: clear()
1491 clearscreen()
1492
1493 Delete all drawings and all turtles from the TurtleScreen. Reset the now
1494 empty TurtleScreen to its initial state: white background, no background
1495 image, no event bindings and tracing on.
1496
1497 .. note::
1498 This TurtleScreen method is available as a global function only under the
1499 name ``clearscreen``. The global function ``clear`` is another one
1500 derived from the Turtle method ``clear``.
1501
1502
1503.. function:: reset()
1504 resetscreen()
1505
1506 Reset all Turtles on the Screen to their initial state.
1507
1508 .. note::
1509 This TurtleScreen method is available as a global function only under the
1510 name ``resetscreen``. The global function ``reset`` is another one
1511 derived from the Turtle method ``reset``.
1512
1513
1514.. function:: screensize(canvwidth=None, canvheight=None, bg=None)
1515
Georg Brandl95089bc2009-04-23 08:49:39 +00001516 :param canvwidth: positive integer, new width of canvas in pixels
1517 :param canvheight: positive integer, new height of canvas in pixels
1518 :param bg: colorstring or color-tuple, new background color
Georg Brandla2b34b82008-06-04 11:17:26 +00001519
1520 If no arguments are given, return current (canvaswidth, canvasheight). Else
1521 resize the canvas the turtles are drawing on. Do not alter the drawing
1522 window. To observe hidden parts of the canvas, use the scrollbars. With this
1523 method, one can make visible those parts of a drawing which were outside the
1524 canvas before.
1525
R. David Murrayb01c6e52009-04-30 12:42:32 +00001526 >>> screen.screensize()
1527 (400, 300)
1528 >>> screen.screensize(2000,1500)
1529 >>> screen.screensize()
1530 (2000, 1500)
1531
1532 e.g. to search for an erroneously escaped turtle ;-)
Georg Brandla2b34b82008-06-04 11:17:26 +00001533
1534
1535.. function:: setworldcoordinates(llx, lly, urx, ury)
1536
1537 :param llx: a number, x-coordinate of lower left corner of canvas
1538 :param lly: a number, y-coordinate of lower left corner of canvas
1539 :param urx: a number, x-coordinate of upper right corner of canvas
1540 :param ury: a number, y-coordinate of upper right corner of canvas
1541
1542 Set up user-defined coordinate system and switch to mode "world" if
1543 necessary. This performs a ``screen.reset()``. If mode "world" is already
1544 active, all drawings are redrawn according to the new coordinates.
1545
1546 **ATTENTION**: in user-defined coordinate systems angles may appear
1547 distorted.
1548
R. David Murrayb01c6e52009-04-30 12:42:32 +00001549 .. doctest::
1550
1551 >>> screen.reset()
1552 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1553 >>> for _ in range(72):
1554 ... left(10)
1555 ...
1556 >>> for _ in range(8):
1557 ... left(45); fd(2) # a regular octagon
1558
1559 .. doctest::
1560 :hide:
1561
1562 >>> screen.reset()
1563 >>> for t in turtles():
1564 ... t.reset()
Georg Brandla2b34b82008-06-04 11:17:26 +00001565
1566
1567Animation control
1568-----------------
1569
1570.. function:: delay(delay=None)
1571
1572 :param delay: positive integer
1573
1574 Set or return the drawing *delay* in milliseconds. (This is approximately
Benjamin Peterson90f36732008-07-12 20:16:19 +00001575 the time interval between two consecutive canvas updates.) The longer the
Georg Brandla2b34b82008-06-04 11:17:26 +00001576 drawing delay, the slower the animation.
1577
1578 Optional argument:
1579
R. David Murrayb01c6e52009-04-30 12:42:32 +00001580 .. doctest::
1581
1582 >>> screen.delay()
1583 10
1584 >>> screen.delay(5)
1585 >>> screen.delay()
1586 5
Georg Brandla2b34b82008-06-04 11:17:26 +00001587
1588
1589.. function:: tracer(n=None, delay=None)
1590
1591 :param n: nonnegative integer
1592 :param delay: nonnegative integer
1593
1594 Turn turtle animation on/off and set delay for update drawings. If *n* is
1595 given, only each n-th regular screen update is really performed. (Can be
1596 used to accelerate the drawing of complex graphics.) Second argument sets
1597 delay value (see :func:`delay`).
1598
R. David Murrayb01c6e52009-04-30 12:42:32 +00001599 .. doctest::
1600
1601 >>> screen.tracer(8, 25)
1602 >>> dist = 2
1603 >>> for i in range(200):
1604 ... fd(dist)
1605 ... rt(90)
1606 ... dist += 2
Georg Brandla2b34b82008-06-04 11:17:26 +00001607
1608
1609.. function:: update()
1610
1611 Perform a TurtleScreen update. To be used when tracer is turned off.
1612
1613See also the RawTurtle/Turtle method :func:`speed`.
1614
1615
1616Using screen events
1617-------------------
1618
1619.. function:: listen(xdummy=None, ydummy=None)
1620
1621 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1622 are provided in order to be able to pass :func:`listen` to the onclick method.
1623
1624
1625.. function:: onkey(fun, key)
1626
1627 :param fun: a function with no arguments or ``None``
1628 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1629
1630 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1631 are removed. Remark: in order to be able to register key-events, TurtleScreen
1632 must have the focus. (See method :func:`listen`.)
1633
R. David Murrayb01c6e52009-04-30 12:42:32 +00001634 .. doctest::
1635
1636 >>> def f():
1637 ... fd(50)
1638 ... lt(60)
1639 ...
1640 >>> screen.onkey(f, "Up")
1641 >>> screen.listen()
Georg Brandla2b34b82008-06-04 11:17:26 +00001642
1643
1644.. function:: onclick(fun, btn=1, add=None)
1645 onscreenclick(fun, btn=1, add=None)
1646
1647 :param fun: a function with two arguments which will be called with the
1648 coordinates of the clicked point on the canvas
1649 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1650 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1651 added, otherwise it will replace a former binding
1652
1653 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1654 existing bindings are removed.
1655
1656 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1657 named turtle:
1658
R. David Murrayb01c6e52009-04-30 12:42:32 +00001659 .. doctest::
1660
1661 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1662 >>> # make the turtle move to the clicked point.
1663 >>> screen.onclick(None) # remove event binding again
Georg Brandla2b34b82008-06-04 11:17:26 +00001664
1665 .. note::
1666 This TurtleScreen method is available as a global function only under the
1667 name ``onscreenclick``. The global function ``onclick`` is another one
1668 derived from the Turtle method ``onclick``.
1669
1670
1671.. function:: ontimer(fun, t=0)
1672
1673 :param fun: a function with no arguments
1674 :param t: a number >= 0
1675
1676 Install a timer that calls *fun* after *t* milliseconds.
1677
R. David Murrayb01c6e52009-04-30 12:42:32 +00001678 .. doctest::
1679
1680 >>> running = True
1681 >>> def f():
1682 ... if running:
1683 ... fd(50)
1684 ... lt(60)
1685 ... screen.ontimer(f, 250)
1686 >>> f() ### makes the turtle march around
1687 >>> running = False
Georg Brandla2b34b82008-06-04 11:17:26 +00001688
1689
1690Settings and special methods
1691----------------------------
1692
1693.. function:: mode(mode=None)
1694
1695 :param mode: one of the strings "standard", "logo" or "world"
1696
1697 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1698 is not given, current mode is returned.
1699
1700 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1701 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1702 "world coordinates". **Attention**: in this mode angles appear distorted if
1703 ``x/y`` unit-ratio doesn't equal 1.
1704
1705 ============ ========================= ===================
1706 Mode Initial turtle heading positive angles
1707 ============ ========================= ===================
1708 "standard" to the right (east) counterclockwise
1709 "logo" upward (north) clockwise
1710 ============ ========================= ===================
1711
R. David Murrayb01c6e52009-04-30 12:42:32 +00001712 .. doctest::
1713
1714 >>> mode("logo") # resets turtle heading to north
1715 >>> mode()
1716 'logo'
Georg Brandla2b34b82008-06-04 11:17:26 +00001717
1718
1719.. function:: colormode(cmode=None)
1720
1721 :param cmode: one of the values 1.0 or 255
1722
1723 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1724 values of color triples have to be in the range 0..\ *cmode*.
1725
R. David Murrayb01c6e52009-04-30 12:42:32 +00001726 .. doctest::
1727
1728 >>> screen.colormode(1)
1729 >>> turtle.pencolor(240, 160, 80)
1730 Traceback (most recent call last):
1731 ...
1732 TurtleGraphicsError: bad color sequence: (240, 160, 80)
1733 >>> screen.colormode()
1734 1.0
1735 >>> screen.colormode(255)
1736 >>> screen.colormode()
1737 255
1738 >>> turtle.pencolor(240,160,80)
Georg Brandla2b34b82008-06-04 11:17:26 +00001739
1740
1741.. function:: getcanvas()
1742
1743 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1744 do with a Tkinter Canvas.
1745
R. David Murrayb01c6e52009-04-30 12:42:32 +00001746 .. doctest::
1747
1748 >>> cv = screen.getcanvas()
1749 >>> cv
1750 <turtle.ScrolledCanvas instance at 0x...>
Georg Brandla2b34b82008-06-04 11:17:26 +00001751
1752
1753.. function:: getshapes()
1754
1755 Return a list of names of all currently available turtle shapes.
1756
R. David Murrayb01c6e52009-04-30 12:42:32 +00001757 .. doctest::
1758
1759 >>> screen.getshapes()
1760 ['arrow', 'blank', 'circle', ..., 'turtle']
Georg Brandla2b34b82008-06-04 11:17:26 +00001761
1762
1763.. function:: register_shape(name, shape=None)
1764 addshape(name, shape=None)
1765
1766 There are three different ways to call this function:
1767
1768 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
R. David Murrayb01c6e52009-04-30 12:42:32 +00001769 corresponding image shape. ::
1770
1771 >>> screen.register_shape("turtle.gif")
Georg Brandla2b34b82008-06-04 11:17:26 +00001772
1773 .. note::
1774 Image shapes *do not* rotate when turning the turtle, so they do not
1775 display the heading of the turtle!
1776
1777 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1778 coordinates: Install the corresponding polygon shape.
1779
R. David Murrayb01c6e52009-04-30 12:42:32 +00001780 .. doctest::
1781
1782 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
1783
Georg Brandla2b34b82008-06-04 11:17:26 +00001784 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
1785 object: Install the corresponding compound shape.
1786
1787 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1788 shapes can be used by issuing the command ``shape(shapename)``.
1789
Georg Brandla2b34b82008-06-04 11:17:26 +00001790
1791.. function:: turtles()
1792
1793 Return the list of turtles on the screen.
1794
R. David Murrayb01c6e52009-04-30 12:42:32 +00001795 .. doctest::
1796
1797 >>> for turtle in screen.turtles():
1798 ... turtle.color("red")
Georg Brandla2b34b82008-06-04 11:17:26 +00001799
1800
1801.. function:: window_height()
1802
R. David Murrayb01c6e52009-04-30 12:42:32 +00001803 Return the height of the turtle window. ::
Georg Brandla2b34b82008-06-04 11:17:26 +00001804
R. David Murrayb01c6e52009-04-30 12:42:32 +00001805 >>> screen.window_height()
1806 480
Georg Brandla2b34b82008-06-04 11:17:26 +00001807
1808
1809.. function:: window_width()
1810
R. David Murrayb01c6e52009-04-30 12:42:32 +00001811 Return the width of the turtle window. ::
Georg Brandla2b34b82008-06-04 11:17:26 +00001812
R. David Murrayb01c6e52009-04-30 12:42:32 +00001813 >>> screen.window_width()
1814 640
Georg Brandla2b34b82008-06-04 11:17:26 +00001815
1816
1817.. _screenspecific:
1818
1819Methods specific to Screen, not inherited from TurtleScreen
Martin v. Löwis87184592008-06-04 06:29:55 +00001820-----------------------------------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001821
Georg Brandla2b34b82008-06-04 11:17:26 +00001822.. function:: bye()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001823
Georg Brandla2b34b82008-06-04 11:17:26 +00001824 Shut the turtlegraphics window.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001825
Georg Brandl8ec7f652007-08-15 14:28:01 +00001826
Georg Brandla2b34b82008-06-04 11:17:26 +00001827.. function:: exitonclick()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001828
Georg Brandla2b34b82008-06-04 11:17:26 +00001829 Bind bye() method to mouse clicks on the Screen.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001830
Georg Brandl8ec7f652007-08-15 14:28:01 +00001831
Georg Brandla2b34b82008-06-04 11:17:26 +00001832 If the value "using_IDLE" in the configuration dictionary is ``False``
1833 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
1834 (no subprocess) is used, this value should be set to ``True`` in
1835 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1836 client script.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001837
Georg Brandl8ec7f652007-08-15 14:28:01 +00001838
Georg Brandla2b34b82008-06-04 11:17:26 +00001839.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
Georg Brandl8ec7f652007-08-15 14:28:01 +00001840
Georg Brandla2b34b82008-06-04 11:17:26 +00001841 Set the size and position of the main window. Default values of arguments
Georg Brandl09302282010-10-06 09:32:48 +00001842 are stored in the configuration dictionary and can be changed via a
Georg Brandla2b34b82008-06-04 11:17:26 +00001843 :file:`turtle.cfg` file.
1844
1845 :param width: if an integer, a size in pixels, if a float, a fraction of the
1846 screen; default is 50% of screen
1847 :param height: if an integer, the height in pixels, if a float, a fraction of
1848 the screen; default is 75% of screen
1849 :param startx: if positive, starting position in pixels from the left
1850 edge of the screen, if negative from the right edge, if None,
1851 center window horizontally
1852 :param startx: if positive, starting position in pixels from the top
1853 edge of the screen, if negative from the bottom edge, if None,
1854 center window vertically
1855
R. David Murrayb01c6e52009-04-30 12:42:32 +00001856 .. doctest::
1857
1858 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1859 >>> # sets window to 200x200 pixels, in upper left of screen
1860 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1861 >>> # sets window to 75% of screen by 50% of screen and centers
Georg Brandl8ec7f652007-08-15 14:28:01 +00001862
Georg Brandl8ec7f652007-08-15 14:28:01 +00001863
Georg Brandla2b34b82008-06-04 11:17:26 +00001864.. function:: title(titlestring)
1865
1866 :param titlestring: a string that is shown in the titlebar of the turtle
1867 graphics window
1868
1869 Set title of turtle window to *titlestring*.
1870
R. David Murrayb01c6e52009-04-30 12:42:32 +00001871 .. doctest::
1872
1873 >>> screen.title("Welcome to the turtle zoo!")
Georg Brandla2b34b82008-06-04 11:17:26 +00001874
1875
1876The public classes of the module :mod:`turtle`
1877==============================================
1878
1879
1880.. class:: RawTurtle(canvas)
1881 RawPen(canvas)
1882
1883 :param canvas: a :class:`Tkinter.Canvas`, a :class:`ScrolledCanvas` or a
1884 :class:`TurtleScreen`
1885
R. David Murrayb01c6e52009-04-30 12:42:32 +00001886 Create a turtle. The turtle has all methods described above as "methods of
1887 Turtle/RawTurtle".
Georg Brandla2b34b82008-06-04 11:17:26 +00001888
1889
1890.. class:: Turtle()
1891
R. David Murrayb01c6e52009-04-30 12:42:32 +00001892 Subclass of RawTurtle, has the same interface but draws on a default
1893 :class:`Screen` object created automatically when needed for the first time.
Georg Brandla2b34b82008-06-04 11:17:26 +00001894
1895
1896.. class:: TurtleScreen(cv)
1897
1898 :param cv: a :class:`Tkinter.Canvas`
1899
1900 Provides screen oriented methods like :func:`setbg` etc. that are described
1901 above.
1902
1903.. class:: Screen()
1904
1905 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
1906
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001907
Georg Brandl1a22e872009-11-07 08:26:07 +00001908.. class:: ScrolledCanvas(master)
Georg Brandla2b34b82008-06-04 11:17:26 +00001909
1910 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
1911 a Tkinter-canvas with scrollbars added
1912
1913 Used by class Screen, which thus automatically provides a ScrolledCanvas as
1914 playground for the turtles.
1915
1916.. class:: Shape(type_, data)
1917
1918 :param type\_: one of the strings "polygon", "image", "compound"
1919
1920 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
1921 specification:
1922
1923
1924 =========== ===========
1925 *type_* *data*
1926 =========== ===========
1927 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
1928 "image" an image (in this form only used internally!)
Georg Brandle83a4ad2009-03-13 19:03:58 +00001929 "compound" ``None`` (a compound shape has to be constructed using the
Georg Brandla2b34b82008-06-04 11:17:26 +00001930 :meth:`addcomponent` method)
1931 =========== ===========
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001932
Georg Brandla2b34b82008-06-04 11:17:26 +00001933 .. method:: addcomponent(poly, fill, outline=None)
1934
1935 :param poly: a polygon, i.e. a tuple of pairs of numbers
1936 :param fill: a color the *poly* will be filled with
1937 :param outline: a color for the poly's outline (if given)
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001938
Georg Brandla2b34b82008-06-04 11:17:26 +00001939 Example:
1940
R. David Murrayb01c6e52009-04-30 12:42:32 +00001941 .. doctest::
1942
1943 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
1944 >>> s = Shape("compound")
1945 >>> s.addcomponent(poly, "red", "blue")
1946 >>> # ... add more components and then use register_shape()
Georg Brandla2b34b82008-06-04 11:17:26 +00001947
1948 See :ref:`compoundshapes`.
1949
1950
1951.. class:: Vec2D(x, y)
1952
1953 A two-dimensional vector class, used as a helper class for implementing
1954 turtle graphics. May be useful for turtle graphics programs too. Derived
1955 from tuple, so a vector is a tuple!
1956
1957 Provides (for *a*, *b* vectors, *k* number):
1958
1959 * ``a + b`` vector addition
1960 * ``a - b`` vector subtraction
1961 * ``a * b`` inner product
1962 * ``k * a`` and ``a * k`` multiplication with scalar
1963 * ``abs(a)`` absolute value of a
1964 * ``a.rotate(angle)`` rotation
1965
1966
1967Help and configuration
1968======================
1969
1970How to use help
1971---------------
1972
1973The public methods of the Screen and Turtle classes are documented extensively
1974via docstrings. So these can be used as online-help via the Python help
1975facilities:
1976
1977- When using IDLE, tooltips show the signatures and first lines of the
1978 docstrings of typed in function-/method calls.
1979
1980- Calling :func:`help` on methods or functions displays the docstrings::
1981
1982 >>> help(Screen.bgcolor)
1983 Help on method bgcolor in module turtle:
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001984
Georg Brandla2b34b82008-06-04 11:17:26 +00001985 bgcolor(self, *args) unbound turtle.Screen method
1986 Set or return backgroundcolor of the TurtleScreen.
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001987
Georg Brandla2b34b82008-06-04 11:17:26 +00001988 Arguments (if given): a color string or three numbers
1989 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001990
1991
Georg Brandla2b34b82008-06-04 11:17:26 +00001992 >>> screen.bgcolor("orange")
1993 >>> screen.bgcolor()
1994 "orange"
1995 >>> screen.bgcolor(0.5,0,0.5)
1996 >>> screen.bgcolor()
1997 "#800080"
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001998
Georg Brandla2b34b82008-06-04 11:17:26 +00001999 >>> help(Turtle.penup)
2000 Help on method penup in module turtle:
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002001
Georg Brandla2b34b82008-06-04 11:17:26 +00002002 penup(self) unbound turtle.Turtle method
2003 Pull the pen up -- no drawing when moving.
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002004
Georg Brandla2b34b82008-06-04 11:17:26 +00002005 Aliases: penup | pu | up
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002006
Georg Brandla2b34b82008-06-04 11:17:26 +00002007 No argument
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002008
Georg Brandla2b34b82008-06-04 11:17:26 +00002009 >>> turtle.penup()
Georg Brandl8ec7f652007-08-15 14:28:01 +00002010
Georg Brandla2b34b82008-06-04 11:17:26 +00002011- The docstrings of the functions which are derived from methods have a modified
2012 form::
Georg Brandl8ec7f652007-08-15 14:28:01 +00002013
Georg Brandla2b34b82008-06-04 11:17:26 +00002014 >>> help(bgcolor)
2015 Help on function bgcolor in module turtle:
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002016
Georg Brandla2b34b82008-06-04 11:17:26 +00002017 bgcolor(*args)
2018 Set or return backgroundcolor of the TurtleScreen.
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002019
Georg Brandla2b34b82008-06-04 11:17:26 +00002020 Arguments (if given): a color string or three numbers
2021 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002022
Georg Brandla2b34b82008-06-04 11:17:26 +00002023 Example::
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002024
Georg Brandla2b34b82008-06-04 11:17:26 +00002025 >>> bgcolor("orange")
2026 >>> bgcolor()
2027 "orange"
2028 >>> bgcolor(0.5,0,0.5)
2029 >>> bgcolor()
2030 "#800080"
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002031
Georg Brandla2b34b82008-06-04 11:17:26 +00002032 >>> help(penup)
2033 Help on function penup in module turtle:
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002034
Georg Brandla2b34b82008-06-04 11:17:26 +00002035 penup()
2036 Pull the pen up -- no drawing when moving.
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002037
Georg Brandla2b34b82008-06-04 11:17:26 +00002038 Aliases: penup | pu | up
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002039
Georg Brandla2b34b82008-06-04 11:17:26 +00002040 No argument
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002041
Georg Brandla2b34b82008-06-04 11:17:26 +00002042 Example:
2043 >>> penup()
Georg Brandl8ec7f652007-08-15 14:28:01 +00002044
Georg Brandla2b34b82008-06-04 11:17:26 +00002045These modified docstrings are created automatically together with the function
2046definitions that are derived from the methods at import time.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002047
2048
Georg Brandla2b34b82008-06-04 11:17:26 +00002049Translation of docstrings into different languages
Martin v. Löwis87184592008-06-04 06:29:55 +00002050--------------------------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00002051
Georg Brandla2b34b82008-06-04 11:17:26 +00002052There is a utility to create a dictionary the keys of which are the method names
2053and the values of which are the docstrings of the public methods of the classes
2054Screen and Turtle.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002055
Georg Brandla2b34b82008-06-04 11:17:26 +00002056.. function:: write_docstringdict(filename="turtle_docstringdict")
Georg Brandl8ec7f652007-08-15 14:28:01 +00002057
Georg Brandla2b34b82008-06-04 11:17:26 +00002058 :param filename: a string, used as filename
Georg Brandl8ec7f652007-08-15 14:28:01 +00002059
Georg Brandla2b34b82008-06-04 11:17:26 +00002060 Create and write docstring-dictionary to a Python script with the given
2061 filename. This function has to be called explicitly (it is not used by the
2062 turtle graphics classes). The docstring dictionary will be written to the
2063 Python script :file:`{filename}.py`. It is intended to serve as a template
2064 for translation of the docstrings into different languages.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002065
Georg Brandla2b34b82008-06-04 11:17:26 +00002066If you (or your students) want to use :mod:`turtle` with online help in your
2067native language, you have to translate the docstrings and save the resulting
2068file as e.g. :file:`turtle_docstringdict_german.py`.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002069
Georg Brandla2b34b82008-06-04 11:17:26 +00002070If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2071will be read in at import time and will replace the original English docstrings.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002072
Georg Brandla2b34b82008-06-04 11:17:26 +00002073At the time of this writing there are docstring dictionaries in German and in
2074Italian. (Requests please to glingl@aon.at.)
2075
2076
2077
2078How to configure Screen and Turtles
Martin v. Löwis87184592008-06-04 06:29:55 +00002079-----------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00002080
Georg Brandla2b34b82008-06-04 11:17:26 +00002081The built-in default configuration mimics the appearance and behaviour of the
2082old turtle module in order to retain best possible compatibility with it.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002083
Georg Brandla2b34b82008-06-04 11:17:26 +00002084If you want to use a different configuration which better reflects the features
2085of this module or which better fits to your needs, e.g. for use in a classroom,
2086you can prepare a configuration file ``turtle.cfg`` which will be read at import
2087time and modify the configuration according to its settings.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002088
Georg Brandla2b34b82008-06-04 11:17:26 +00002089The built in configuration would correspond to the following turtle.cfg::
Georg Brandl8ec7f652007-08-15 14:28:01 +00002090
Georg Brandla2b34b82008-06-04 11:17:26 +00002091 width = 0.5
2092 height = 0.75
2093 leftright = None
2094 topbottom = None
2095 canvwidth = 400
2096 canvheight = 300
2097 mode = standard
2098 colormode = 1.0
2099 delay = 10
2100 undobuffersize = 1000
2101 shape = classic
2102 pencolor = black
2103 fillcolor = black
2104 resizemode = noresize
2105 visible = True
2106 language = english
2107 exampleturtle = turtle
2108 examplescreen = screen
2109 title = Python Turtle Graphics
2110 using_IDLE = False
Georg Brandl8ec7f652007-08-15 14:28:01 +00002111
Martin v. Löwis87184592008-06-04 06:29:55 +00002112Short explanation of selected entries:
Georg Brandl8ec7f652007-08-15 14:28:01 +00002113
Georg Brandla2b34b82008-06-04 11:17:26 +00002114- The first four lines correspond to the arguments of the :meth:`Screen.setup`
2115 method.
2116- Line 5 and 6 correspond to the arguments of the method
2117 :meth:`Screen.screensize`.
2118- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2119 info try ``help(shape)``.
2120- If you want to use no fillcolor (i.e. make the turtle transparent), you have
2121 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
2122 the cfg-file).
2123- If you want to reflect the turtle its state, you have to use ``resizemode =
2124 auto``.
2125- If you set e.g. ``language = italian`` the docstringdict
2126 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
2127 present on the import path, e.g. in the same directory as :mod:`turtle`.
2128- The entries *exampleturtle* and *examplescreen* define the names of these
2129 objects as they occur in the docstrings. The transformation of
2130 method-docstrings to function-docstrings will delete these names from the
2131 docstrings.
2132- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
2133 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
2134 mainloop.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002135
Georg Brandla2b34b82008-06-04 11:17:26 +00002136There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2137stored and an additional one in the current working directory. The latter will
2138override the settings of the first one.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002139
Georg Brandla2b34b82008-06-04 11:17:26 +00002140The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can
2141study it as an example and see its effects when running the demos (preferably
2142not from within the demo-viewer).
Georg Brandl8ec7f652007-08-15 14:28:01 +00002143
Georg Brandla2b34b82008-06-04 11:17:26 +00002144
2145Demo scripts
2146============
2147
2148There is a set of demo scripts in the turtledemo directory located in the
2149:file:`Demo/turtle` directory in the source distribution.
2150
Martin v. Löwis87184592008-06-04 06:29:55 +00002151It contains:
Georg Brandl8ec7f652007-08-15 14:28:01 +00002152
Georg Brandle83a4ad2009-03-13 19:03:58 +00002153- a set of 15 demo scripts demonstrating different features of the new module
Georg Brandla2b34b82008-06-04 11:17:26 +00002154 :mod:`turtle`
2155- a demo viewer :file:`turtleDemo.py` which can be used to view the sourcecode
2156 of the scripts and run them at the same time. 14 of the examples can be
2157 accessed via the Examples menu; all of them can also be run standalone.
2158- The example :file:`turtledemo_two_canvases.py` demonstrates the simultaneous
2159 use of two canvases with the turtle module. Therefore it only can be run
2160 standalone.
2161- There is a :file:`turtle.cfg` file in this directory, which also serves as an
2162 example for how to write and use such files.
2163
Martin v. Löwis87184592008-06-04 06:29:55 +00002164The demoscripts are:
Georg Brandl8ec7f652007-08-15 14:28:01 +00002165
Martin v. Löwis87184592008-06-04 06:29:55 +00002166+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002167| Name | Description | Features |
Martin v. Löwis87184592008-06-04 06:29:55 +00002168+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002169| bytedesign | complex classical | :func:`tracer`, delay,|
2170| | turtlegraphics pattern | :func:`update` |
Martin v. Löwis87184592008-06-04 06:29:55 +00002171+----------------+------------------------------+-----------------------+
Georg Brandl21b832e2011-03-06 10:53:55 +01002172| chaos | graphs Verhulst dynamics, | world coordinates |
2173| | shows that computer's | |
2174| | computations can generate | |
2175| | results sometimes against the| |
2176| | common sense expectations | |
Martin v. Löwis87184592008-06-04 06:29:55 +00002177+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002178| clock | analog clock showing time | turtles as clock's |
2179| | of your computer | hands, ontimer |
Martin v. Löwis87184592008-06-04 06:29:55 +00002180+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002181| colormixer | experiment with r, g, b | :func:`ondrag` |
Martin v. Löwis87184592008-06-04 06:29:55 +00002182+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002183| fractalcurves | Hilbert & Koch curves | recursion |
Martin v. Löwis87184592008-06-04 06:29:55 +00002184+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002185| lindenmayer | ethnomathematics | L-System |
2186| | (indian kolams) | |
Martin v. Löwis87184592008-06-04 06:29:55 +00002187+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002188| minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
2189| | | as Hanoi discs |
2190| | | (shape, shapesize) |
Martin v. Löwis87184592008-06-04 06:29:55 +00002191+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002192| paint | super minimalistic | :func:`onclick` |
2193| | drawing program | |
Martin v. Löwis87184592008-06-04 06:29:55 +00002194+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002195| peace | elementary | turtle: appearance |
2196| | | and animation |
Martin v. Löwis87184592008-06-04 06:29:55 +00002197+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002198| penrose | aperiodic tiling with | :func:`stamp` |
2199| | kites and darts | |
Martin v. Löwis87184592008-06-04 06:29:55 +00002200+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002201| planet_and_moon| simulation of | compound shapes, |
2202| | gravitational system | :class:`Vec2D` |
Martin v. Löwis87184592008-06-04 06:29:55 +00002203+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002204| tree | a (graphical) breadth | :func:`clone` |
Martin v. Löwis87184592008-06-04 06:29:55 +00002205| | first tree (using generators)| |
2206+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002207| wikipedia | a pattern from the wikipedia | :func:`clone`, |
2208| | article on turtle graphics | :func:`undo` |
Martin v. Löwis87184592008-06-04 06:29:55 +00002209+----------------+------------------------------+-----------------------+
Georg Brandla2b34b82008-06-04 11:17:26 +00002210| yingyang | another elementary example | :func:`circle` |
Martin v. Löwis87184592008-06-04 06:29:55 +00002211+----------------+------------------------------+-----------------------+
Georg Brandl8ec7f652007-08-15 14:28:01 +00002212
Georg Brandla2b34b82008-06-04 11:17:26 +00002213Have fun!
R. David Murrayb01c6e52009-04-30 12:42:32 +00002214
2215.. doctest::
2216 :hide:
2217
2218 >>> for turtle in turtles():
2219 ... turtle.reset()
2220 >>> turtle.penup()
2221 >>> turtle.goto(-200,25)
2222 >>> turtle.pendown()
2223 >>> turtle.write("No one expects the Spanish Inquisition!",
2224 ... font=("Arial", 20, "normal"))
2225 >>> turtle.penup()
2226 >>> turtle.goto(-100,-50)
2227 >>> turtle.pendown()
2228 >>> turtle.write("Our two chief Turtles are...",
2229 ... font=("Arial", 16, "normal"))
2230 >>> turtle.penup()
2231 >>> turtle.goto(-450,-75)
2232 >>> turtle.write(str(turtles()))