blob: 001f349fec8652a3e0be390d286edb4c6c47c10f [file] [log] [blame]
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001========================================
2:mod:`turtle` --- Turtle graphics for Tk
3========================================
Georg Brandl116aa622007-08-15 14:28:22 +00004
Georg Brandl23d11d32008-09-21 07:50:52 +00005.. module:: turtle
Georg Brandl2ee470f2008-07-16 12:55:28 +00006 :synopsis: Turtle graphics for Tk
7.. sectionauthor:: Gregor Lingl <gregor.lingl@aon.at>
8
R. David Murrayf877feb2009-05-05 02:08:52 +00009.. testsetup:: default
10
11 from turtle import *
12 turtle = Turtle()
13
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000014Introduction
15============
16
17Turtle 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.
20
21Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it the
22command ``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
24``turtle.left(25)``, and it rotates in-place 25 degrees clockwise.
25
26By combining together these and similar commands, intricate shapes and pictures
27can easily be drawn.
28
29The :mod:`turtle` module is an extended reimplementation of the same-named
30module from the Python standard distribution up to version Python 2.5.
31
32It 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.
36
37The turtle module provides turtle graphics primitives, in both object-oriented
38and procedure-oriented ways. Because it uses :mod:`Tkinter` for the underlying
39graphics, it needs a version of python installed with Tk support.
40
41The object-oriented interface uses essentially two+two classes:
42
431. 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.
47
Martin v. Löwis601149b2008-09-29 22:19:08 +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öwis97cf99f2008-06-10 04:44:07 +000052
53 All methods of TurtleScreen/Screen also exist as functions, i.e. as part of
54 the procedure-oriented interface.
55
562. :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.
59
60 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.
63
64 All methods of RawTurtle/Turtle also exist as functions, i.e. part of the
65 procedure-oriented interface.
66
67The 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 Brandlae2dbe22009-03-13 19:04:40 +000069the corresponding methods. A screen object is automatically created whenever a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +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.
73
74To use multiple turtles an a screen one has to use the object-oriented interface.
75
76.. 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.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000082Overview over available Turtle and Screen methods
83=================================================
84
85Turtle methods
86--------------
87
88Turtle 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`
106
107 Tell Turtle's state
108 | :func:`position` | :func:`pos`
109 | :func:`towards`
110 | :func:`xcor`
111 | :func:`ycor`
112 | :func:`heading`
113 | :func:`distance`
114
115 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:`filling`
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`
Georg Brandl116aa622007-08-15 14:28:22 +0000170
171
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000172Methods of TurtleScreen/Screen
173------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000175Window control
176 | :func:`bgcolor`
177 | :func:`bgpic`
178 | :func:`clear` | :func:`clearscreen`
179 | :func:`reset` | :func:`resetscreen`
180 | :func:`screensize`
181 | :func:`setworldcoordinates`
Georg Brandl116aa622007-08-15 14:28:22 +0000182
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000183Animation control
184 | :func:`delay`
185 | :func:`tracer`
186 | :func:`update`
187
188Using screen events
189 | :func:`listen`
190 | :func:`onkey`
191 | :func:`onclick` | :func:`onscreenclick`
192 | :func:`ontimer`
193
194Settings and special methods
195 | :func:`mode`
196 | :func:`colormode`
197 | :func:`getcanvas`
198 | :func:`getshapes`
199 | :func:`register_shape` | :func:`addshape`
200 | :func:`turtles`
201 | :func:`window_height`
202 | :func:`window_width`
203
204Methods specific to Screen
205 | :func:`bye`
206 | :func:`exitonclick`
207 | :func:`setup`
208 | :func:`title`
Georg Brandl116aa622007-08-15 14:28:22 +0000209
210
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000211Methods of RawTurtle/Turtle and corresponding functions
212=======================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000213
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000214Most of the examples in this section refer to a Turtle instance called
215``turtle``.
Georg Brandl116aa622007-08-15 14:28:22 +0000216
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000217Turtle motion
218-------------
Georg Brandl116aa622007-08-15 14:28:22 +0000219
220.. function:: forward(distance)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000221 fd(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000222
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000223 :param distance: a number (integer or float)
224
225 Move the turtle forward by the specified *distance*, in the direction the
226 turtle is headed.
227
R. David Murrayf877feb2009-05-05 02:08:52 +0000228 .. doctest::
229
230 >>> turtle.position()
231 (0.00,0.00)
232 >>> turtle.forward(25)
233 >>> turtle.position()
234 (25.00,0.00)
235 >>> turtle.forward(-75)
236 >>> turtle.position()
237 (-50.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000238
239
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000240.. function:: back(distance)
241 bk(distance)
242 backward(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000243
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000244 :param distance: a number
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000246 Move the turtle backward by *distance*, opposite to the direction the
247 turtle is headed. Do not change the turtle's heading.
Georg Brandl116aa622007-08-15 14:28:22 +0000248
R. David Murrayf877feb2009-05-05 02:08:52 +0000249 .. doctest::
250 :hide:
251
252 >>> turtle.goto(0, 0)
253
254 .. doctest::
255
256 >>> turtle.position()
257 (0.00,0.00)
258 >>> turtle.backward(30)
259 >>> turtle.position()
260 (-30.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000261
262
263.. function:: right(angle)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000264 rt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000265
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000266 :param angle: a number (integer or float)
267
268 Turn turtle right by *angle* units. (Units are by default degrees, but
269 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
270 orientation depends on the turtle mode, see :func:`mode`.
271
R. David Murrayf877feb2009-05-05 02:08:52 +0000272 .. doctest::
273 :hide:
274
275 >>> turtle.setheading(22)
276
277 .. doctest::
278
279 >>> turtle.heading()
280 22.0
281 >>> turtle.right(45)
282 >>> turtle.heading()
283 337.0
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000286.. function:: left(angle)
287 lt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000288
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000289 :param angle: a number (integer or float)
Georg Brandl116aa622007-08-15 14:28:22 +0000290
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000291 Turn turtle left by *angle* units. (Units are by default degrees, but
292 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
293 orientation depends on the turtle mode, see :func:`mode`.
Georg Brandl116aa622007-08-15 14:28:22 +0000294
R. David Murrayf877feb2009-05-05 02:08:52 +0000295 .. doctest::
296 :hide:
297
298 >>> turtle.setheading(22)
299
300 .. doctest::
301
302 >>> turtle.heading()
303 22.0
304 >>> turtle.left(45)
305 >>> turtle.heading()
306 67.0
307
Georg Brandl116aa622007-08-15 14:28:22 +0000308
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000309.. function:: goto(x, y=None)
310 setpos(x, y=None)
311 setposition(x, y=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000312
R. David Murrayf877feb2009-05-05 02:08:52 +0000313 :param x: a number or a pair/vector of numbers
314 :param y: a number or ``None``
Georg Brandl116aa622007-08-15 14:28:22 +0000315
R. David Murrayf877feb2009-05-05 02:08:52 +0000316 If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D`
317 (e.g. as returned by :func:`pos`).
Georg Brandl116aa622007-08-15 14:28:22 +0000318
R. David Murrayf877feb2009-05-05 02:08:52 +0000319 Move turtle to an absolute position. If the pen is down, draw line. Do
320 not change the turtle's orientation.
Georg Brandl116aa622007-08-15 14:28:22 +0000321
R. David Murrayf877feb2009-05-05 02:08:52 +0000322 .. doctest::
323 :hide:
324
325 >>> turtle.goto(0, 0)
326
327 .. doctest::
328
329 >>> tp = turtle.pos()
330 >>> tp
331 (0.00,0.00)
332 >>> turtle.setpos(60,30)
333 >>> turtle.pos()
334 (60.00,30.00)
335 >>> turtle.setpos((20,80))
336 >>> turtle.pos()
337 (20.00,80.00)
338 >>> turtle.setpos(tp)
339 >>> turtle.pos()
340 (0.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000341
Georg Brandl116aa622007-08-15 14:28:22 +0000342
343.. function:: setx(x)
344
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000345 :param x: a number (integer or float)
346
347 Set the turtle's first coordinate to *x*, leave second coordinate
348 unchanged.
349
R. David Murrayf877feb2009-05-05 02:08:52 +0000350 .. doctest::
351 :hide:
352
353 >>> turtle.goto(0, 240)
354
355 .. doctest::
356
357 >>> turtle.position()
358 (0.00,240.00)
359 >>> turtle.setx(10)
360 >>> turtle.position()
361 (10.00,240.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000362
Georg Brandl116aa622007-08-15 14:28:22 +0000363
364.. function:: sety(y)
365
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000366 :param y: a number (integer or float)
367
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000368 Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000369
R. David Murrayf877feb2009-05-05 02:08:52 +0000370 .. doctest::
371 :hide:
372
373 >>> turtle.goto(0, 40)
374
375 .. doctest::
376
377 >>> turtle.position()
378 (0.00,40.00)
379 >>> turtle.sety(-10)
380 >>> turtle.position()
381 (0.00,-10.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000382
Georg Brandl116aa622007-08-15 14:28:22 +0000383
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000384.. function:: setheading(to_angle)
385 seth(to_angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000387 :param to_angle: a number (integer or float)
388
389 Set the orientation of the turtle to *to_angle*. Here are some common
390 directions in degrees:
391
392 =================== ====================
393 standard mode logo mode
394 =================== ====================
395 0 - east 0 - north
396 90 - north 90 - east
397 180 - west 180 - south
398 270 - south 270 - west
399 =================== ====================
400
R. David Murrayf877feb2009-05-05 02:08:52 +0000401 .. doctest::
402
403 >>> turtle.setheading(90)
404 >>> turtle.heading()
405 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000406
407
408.. function:: home()
409
410 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
411 its start-orientation (which depends on the mode, see :func:`mode`).
412
R. David Murrayf877feb2009-05-05 02:08:52 +0000413 .. doctest::
414 :hide:
415
416 >>> turtle.setheading(90)
417 >>> turtle.goto(0, -10)
418
419 .. doctest::
420
421 >>> turtle.heading()
422 90.0
423 >>> turtle.position()
424 (0.00,-10.00)
425 >>> turtle.home()
426 >>> turtle.position()
427 (0.00,0.00)
428 >>> turtle.heading()
429 0.0
430
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000431
432.. function:: circle(radius, extent=None, steps=None)
433
434 :param radius: a number
435 :param extent: a number (or ``None``)
436 :param steps: an integer (or ``None``)
437
438 Draw a circle with given *radius*. The center is *radius* units left of
439 the turtle; *extent* -- an angle -- determines which part of the circle
440 is drawn. If *extent* is not given, draw the entire circle. If *extent*
441 is not a full circle, one endpoint of the arc is the current pen
442 position. Draw the arc in counterclockwise direction if *radius* is
443 positive, otherwise in clockwise direction. Finally the direction of the
444 turtle is changed by the amount of *extent*.
445
446 As the circle is approximated by an inscribed regular polygon, *steps*
447 determines the number of steps to use. If not given, it will be
448 calculated automatically. May be used to draw regular polygons.
449
R. David Murrayf877feb2009-05-05 02:08:52 +0000450 .. doctest::
451
452 >>> turtle.home()
453 >>> turtle.position()
454 (0.00,0.00)
455 >>> turtle.heading()
456 0.0
457 >>> turtle.circle(50)
458 >>> turtle.position()
459 (-0.00,0.00)
460 >>> turtle.heading()
461 0.0
462 >>> turtle.circle(120, 180) # draw a semicircle
463 >>> turtle.position()
464 (0.00,240.00)
465 >>> turtle.heading()
466 180.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000467
468
469.. function:: dot(size=None, *color)
470
471 :param size: an integer >= 1 (if given)
472 :param color: a colorstring or a numeric color tuple
473
474 Draw a circular dot with diameter *size*, using *color*. If *size* is
475 not given, the maximum of pensize+4 and 2*pensize is used.
476
R. David Murrayf877feb2009-05-05 02:08:52 +0000477
478 .. doctest::
479
480 >>> turtle.home()
481 >>> turtle.dot()
482 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
483 >>> turtle.position()
484 (100.00,-0.00)
485 >>> turtle.heading()
486 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000487
488
489.. function:: stamp()
490
491 Stamp a copy of the turtle shape onto the canvas at the current turtle
492 position. Return a stamp_id for that stamp, which can be used to delete
493 it by calling ``clearstamp(stamp_id)``.
494
R. David Murrayf877feb2009-05-05 02:08:52 +0000495 .. doctest::
496
497 >>> turtle.color("blue")
498 >>> turtle.stamp()
499 11
500 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000501
502
503.. function:: clearstamp(stampid)
504
505 :param stampid: an integer, must be return value of previous
506 :func:`stamp` call
507
508 Delete stamp with given *stampid*.
509
R. David Murrayf877feb2009-05-05 02:08:52 +0000510 .. doctest::
511
512 >>> turtle.position()
513 (150.00,-0.00)
514 >>> turtle.color("blue")
515 >>> astamp = turtle.stamp()
516 >>> turtle.fd(50)
517 >>> turtle.position()
518 (200.00,-0.00)
519 >>> turtle.clearstamp(astamp)
520 >>> turtle.position()
521 (200.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000522
523
524.. function:: clearstamps(n=None)
525
526 :param n: an integer (or ``None``)
527
528 Delete all or first/last *n* of turtle's stamps. If *n* is None, delete
529 all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete
530 last *n* stamps.
531
R. David Murrayf877feb2009-05-05 02:08:52 +0000532 .. doctest::
533
534 >>> for i in range(8):
535 ... turtle.stamp(); turtle.fd(30)
536 13
537 14
538 15
539 16
540 17
541 18
542 19
543 20
544 >>> turtle.clearstamps(2)
545 >>> turtle.clearstamps(-2)
546 >>> turtle.clearstamps()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000547
548
549.. function:: undo()
550
551 Undo (repeatedly) the last turtle action(s). Number of available
552 undo actions is determined by the size of the undobuffer.
553
R. David Murrayf877feb2009-05-05 02:08:52 +0000554 .. doctest::
555
556 >>> for i in range(4):
557 ... turtle.fd(50); turtle.lt(80)
558 ...
559 >>> for i in range(8):
560 ... turtle.undo()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000561
562
563.. function:: speed(speed=None)
564
565 :param speed: an integer in the range 0..10 or a speedstring (see below)
566
567 Set the turtle's speed to an integer value in the range 0..10. If no
568 argument is given, return current speed.
569
570 If input is a number greater than 10 or smaller than 0.5, speed is set
571 to 0. Speedstrings are mapped to speedvalues as follows:
572
573 * "fastest": 0
574 * "fast": 10
575 * "normal": 6
576 * "slow": 3
577 * "slowest": 1
578
579 Speeds from 1 to 10 enforce increasingly faster animation of line drawing
580 and turtle turning.
581
582 Attention: *speed* = 0 means that *no* animation takes
583 place. forward/back makes turtle jump and likewise left/right make the
584 turtle turn instantly.
585
R. David Murrayf877feb2009-05-05 02:08:52 +0000586 .. doctest::
587
588 >>> turtle.speed()
589 3
590 >>> turtle.speed('normal')
591 >>> turtle.speed()
592 6
593 >>> turtle.speed(9)
594 >>> turtle.speed()
595 9
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000596
597
598Tell Turtle's state
599-------------------
600
601.. function:: position()
602 pos()
603
604 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
605
R. David Murrayf877feb2009-05-05 02:08:52 +0000606 .. doctest::
607
608 >>> turtle.pos()
609 (440.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000610
611
612.. function:: towards(x, y=None)
613
614 :param x: a number or a pair/vector of numbers or a turtle instance
615 :param y: a number if *x* is a number, else ``None``
616
617 Return the angle between the line from turtle position to position specified
618 by (x,y), the vector or the other turtle. This depends on the turtle's start
619 orientation which depends on the mode - "standard"/"world" or "logo").
620
R. David Murrayf877feb2009-05-05 02:08:52 +0000621 .. doctest::
622
623 >>> turtle.goto(10, 10)
624 >>> turtle.towards(0,0)
625 225.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000626
627
628.. function:: xcor()
629
630 Return the turtle's x coordinate.
631
R. David Murrayf877feb2009-05-05 02:08:52 +0000632 .. doctest::
633
634 >>> turtle.home()
635 >>> turtle.left(50)
636 >>> turtle.forward(100)
637 >>> turtle.pos()
638 (64.28,76.60)
639 >>> print turtle.xcor()
640 64.2787609687
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000641
642
643.. function:: ycor()
644
645 Return the turtle's y coordinate.
646
R. David Murrayf877feb2009-05-05 02:08:52 +0000647 .. doctest::
648
649 >>> turtle.home()
650 >>> turtle.left(60)
651 >>> turtle.forward(100)
652 >>> print turtle.pos()
653 (50.00,86.60)
654 >>> print turtle.ycor()
655 86.6025403784
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000656
657
658.. function:: heading()
659
660 Return the turtle's current heading (value depends on the turtle mode, see
661 :func:`mode`).
662
R. David Murrayf877feb2009-05-05 02:08:52 +0000663 .. doctest::
664
665 >>> turtle.home()
666 >>> turtle.left(67)
667 >>> turtle.heading()
668 67.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000669
670
671.. function:: distance(x, y=None)
672
673 :param x: a number or a pair/vector of numbers or a turtle instance
674 :param y: a number if *x* is a number, else ``None``
675
676 Return the distance from the turtle to (x,y), the given vector, or the given
677 other turtle, in turtle step units.
678
R. David Murrayf877feb2009-05-05 02:08:52 +0000679 .. doctest::
680
681 >>> turtle.home()
682 >>> turtle.distance(30,40)
683 50.0
684 >>> turtle.distance((30,40))
685 50.0
686 >>> joe = Turtle()
687 >>> joe.forward(77)
688 >>> turtle.distance(joe)
689 77.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000690
691
692Settings for measurement
693------------------------
694
695.. function:: degrees(fullcircle=360.0)
696
697 :param fullcircle: a number
698
699 Set angle measurement units, i.e. set number of "degrees" for a full circle.
700 Default value is 360 degrees.
701
R. David Murrayf877feb2009-05-05 02:08:52 +0000702 .. doctest::
703
704 >>> turtle.home()
705 >>> turtle.left(90)
706 >>> turtle.heading()
707 90.0
708 >>> turtle.degrees(400.0) # angle measurement in gon
709 >>> turtle.heading()
710 100.0
711 >>> turtle.degrees(360)
712 >>> turtle.heading()
713 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000714
715
716.. function:: radians()
717
718 Set the angle measurement units to radians. Equivalent to
719 ``degrees(2*math.pi)``.
720
R. David Murrayf877feb2009-05-05 02:08:52 +0000721 .. doctest::
722
723 >>> turtle.home()
724 >>> turtle.left(90)
725 >>> turtle.heading()
726 90.0
727 >>> turtle.radians()
728 >>> turtle.heading()
729 1.5707963267948966
730
731 .. doctest::
732 :hide:
733
734 >>> turtle.degrees(360)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000735
736
737Pen control
738-----------
739
740Drawing state
741~~~~~~~~~~~~~
742
743.. function:: pendown()
744 pd()
745 down()
746
747 Pull the pen down -- drawing when moving.
748
749
750.. function:: penup()
751 pu()
752 up()
753
754 Pull the pen up -- no drawing when moving.
755
756
757.. function:: pensize(width=None)
758 width(width=None)
759
760 :param width: a positive number
761
762 Set the line thickness to *width* or return it. If resizemode is set to
763 "auto" and turtleshape is a polygon, that polygon is drawn with the same line
764 thickness. If no argument is given, the current pensize is returned.
765
R. David Murrayf877feb2009-05-05 02:08:52 +0000766 .. doctest::
767
768 >>> turtle.pensize()
769 1
770 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000771
772
773.. function:: pen(pen=None, **pendict)
774
775 :param pen: a dictionary with some or all of the below listed keys
776 :param pendict: one or more keyword-arguments with the below listed keys as keywords
777
778 Return or set the pen's attributes in a "pen-dictionary" with the following
779 key/value pairs:
780
781 * "shown": True/False
782 * "pendown": True/False
783 * "pencolor": color-string or color-tuple
784 * "fillcolor": color-string or color-tuple
785 * "pensize": positive number
786 * "speed": number in range 0..10
787 * "resizemode": "auto" or "user" or "noresize"
788 * "stretchfactor": (positive number, positive number)
789 * "outline": positive number
790 * "tilt": number
791
R. David Murrayf877feb2009-05-05 02:08:52 +0000792 This dictionary can be used as argument for a subsequent call to :func:`pen`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000793 to restore the former pen-state. Moreover one or more of these attributes
794 can be provided as keyword-arguments. This can be used to set several pen
795 attributes in one statement.
796
R. David Murrayf877feb2009-05-05 02:08:52 +0000797 .. doctest::
798 :options: +NORMALIZE_WHITESPACE
799
800 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
801 >>> sorted(turtle.pen().items())
802 [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
803 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
804 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
805 >>> penstate=turtle.pen()
806 >>> turtle.color("yellow", "")
807 >>> turtle.penup()
808 >>> sorted(turtle.pen().items())
809 [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow'),
810 ('pendown', False), ('pensize', 10), ('resizemode', 'noresize'),
811 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
812 >>> turtle.pen(penstate, fillcolor="green")
813 >>> sorted(turtle.pen().items())
814 [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red'),
815 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
816 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000817
818
819.. function:: isdown()
820
821 Return ``True`` if pen is down, ``False`` if it's up.
822
R. David Murrayf877feb2009-05-05 02:08:52 +0000823 .. doctest::
824
825 >>> turtle.penup()
826 >>> turtle.isdown()
827 False
828 >>> turtle.pendown()
829 >>> turtle.isdown()
830 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000831
832
833Color control
834~~~~~~~~~~~~~
835
836.. function:: pencolor(*args)
837
838 Return or set the pencolor.
839
840 Four input formats are allowed:
841
842 ``pencolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000843 Return the current pencolor as color specification string or
844 as a tuple (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000845 color/pencolor/fillcolor call.
846
847 ``pencolor(colorstring)``
848 Set pencolor to *colorstring*, which is a Tk color specification string,
849 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
850
851 ``pencolor((r, g, b))``
852 Set pencolor to the RGB color represented by the tuple of *r*, *g*, and
853 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
854 colormode is either 1.0 or 255 (see :func:`colormode`).
855
856 ``pencolor(r, g, b)``
857 Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of
858 *r*, *g*, and *b* must be in the range 0..colormode.
859
860 If turtleshape is a polygon, the outline of that polygon is drawn with the
861 newly set pencolor.
862
R. David Murrayf877feb2009-05-05 02:08:52 +0000863 .. doctest::
864
865 >>> colormode()
866 1.0
867 >>> turtle.pencolor()
868 'red'
869 >>> turtle.pencolor("brown")
870 >>> turtle.pencolor()
871 'brown'
872 >>> tup = (0.2, 0.8, 0.55)
873 >>> turtle.pencolor(tup)
874 >>> turtle.pencolor()
875 (0.20000000000000001, 0.80000000000000004, 0.5490196078431373)
876 >>> colormode(255)
877 >>> turtle.pencolor()
878 (51, 204, 140)
879 >>> turtle.pencolor('#32c18f')
880 >>> turtle.pencolor()
881 (50, 193, 143)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000882
883
884.. function:: fillcolor(*args)
885
886 Return or set the fillcolor.
887
888 Four input formats are allowed:
889
890 ``fillcolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000891 Return the current fillcolor as color specification string, possibly
892 in tuple format (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000893 color/pencolor/fillcolor call.
894
895 ``fillcolor(colorstring)``
896 Set fillcolor to *colorstring*, which is a Tk color specification string,
897 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
898
899 ``fillcolor((r, g, b))``
900 Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and
901 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
902 colormode is either 1.0 or 255 (see :func:`colormode`).
903
904 ``fillcolor(r, g, b)``
905 Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of
906 *r*, *g*, and *b* must be in the range 0..colormode.
907
908 If turtleshape is a polygon, the interior of that polygon is drawn
909 with the newly set fillcolor.
910
R. David Murrayf877feb2009-05-05 02:08:52 +0000911 .. doctest::
912
913 >>> turtle.fillcolor("violet")
914 >>> turtle.fillcolor()
915 'violet'
916 >>> col = turtle.pencolor()
917 >>> col
918 (50, 193, 143)
919 >>> turtle.fillcolor(col)
920 >>> turtle.fillcolor()
921 (50, 193, 143)
922 >>> turtle.fillcolor('#ffffff')
923 >>> turtle.fillcolor()
924 (255, 255, 255)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000925
926
927.. function:: color(*args)
928
929 Return or set pencolor and fillcolor.
930
931 Several input formats are allowed. They use 0 to 3 arguments as
932 follows:
933
934 ``color()``
935 Return the current pencolor and the current fillcolor as a pair of color
R. David Murrayf877feb2009-05-05 02:08:52 +0000936 specification strings or tuples as returned by :func:`pencolor` and
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000937 :func:`fillcolor`.
938
939 ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)``
940 Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the
941 given value.
942
943 ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))``
944 Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)``
945 and analogously if the other input format is used.
946
947 If turtleshape is a polygon, outline and interior of that polygon is drawn
948 with the newly set colors.
949
R. David Murrayf877feb2009-05-05 02:08:52 +0000950 .. doctest::
951
952 >>> turtle.color("red", "green")
953 >>> turtle.color()
954 ('red', 'green')
955 >>> color("#285078", "#a0c8f0")
956 >>> color()
957 ((40, 80, 120), (160, 200, 240))
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000958
959
960See also: Screen method :func:`colormode`.
961
962
963Filling
964~~~~~~~
965
R. David Murrayf877feb2009-05-05 02:08:52 +0000966.. doctest::
967 :hide:
968
969 >>> turtle.home()
970
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000971.. function:: filling()
972
973 Return fillstate (``True`` if filling, ``False`` else).
974
R. David Murrayf877feb2009-05-05 02:08:52 +0000975 .. doctest::
976
977 >>> turtle.begin_fill()
978 >>> if turtle.filling():
979 ... turtle.pensize(5)
980 ... else:
981 ... turtle.pensize(3)
982
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000983
984
985.. function:: begin_fill()
986
987 To be called just before drawing a shape to be filled.
988
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000989
990.. function:: end_fill()
991
992 Fill the shape drawn after the last call to :func:`begin_fill`.
993
R. David Murrayf877feb2009-05-05 02:08:52 +0000994 .. doctest::
995
996 >>> turtle.color("black", "red")
997 >>> turtle.begin_fill()
998 >>> turtle.circle(80)
999 >>> turtle.end_fill()
1000
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001001
1002More drawing control
1003~~~~~~~~~~~~~~~~~~~~
1004
1005.. function:: reset()
1006
1007 Delete the turtle's drawings from the screen, re-center the turtle and set
1008 variables to the default values.
1009
R. David Murrayf877feb2009-05-05 02:08:52 +00001010 .. doctest::
1011
1012 >>> turtle.goto(0,-22)
1013 >>> turtle.left(100)
1014 >>> turtle.position()
1015 (0.00,-22.00)
1016 >>> turtle.heading()
1017 100.0
1018 >>> turtle.reset()
1019 >>> turtle.position()
1020 (0.00,0.00)
1021 >>> turtle.heading()
1022 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001023
1024
1025.. function:: clear()
1026
1027 Delete the turtle's drawings from the screen. Do not move turtle. State and
1028 position of the turtle as well as drawings of other turtles are not affected.
1029
1030
1031.. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal"))
1032
1033 :param arg: object to be written to the TurtleScreen
1034 :param move: True/False
1035 :param align: one of the strings "left", "center" or right"
1036 :param font: a triple (fontname, fontsize, fonttype)
1037
1038 Write text - the string representation of *arg* - at the current turtle
1039 position according to *align* ("left", "center" or right") and with the given
1040 font. If *move* is True, the pen is moved to the bottom-right corner of the
1041 text. By default, *move* is False.
1042
1043 >>> turtle.write("Home = ", True, align="center")
1044 >>> turtle.write((0,0), True)
1045
1046
1047Turtle state
1048------------
1049
1050Visibility
1051~~~~~~~~~~
1052
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001053.. function:: hideturtle()
1054 ht()
1055
1056 Make the turtle invisible. It's a good idea to do this while you're in the
1057 middle of doing some complex drawing, because hiding the turtle speeds up the
1058 drawing observably.
1059
R. David Murrayf877feb2009-05-05 02:08:52 +00001060 .. doctest::
1061
1062 >>> turtle.hideturtle()
1063
1064
1065.. function:: showturtle()
1066 st()
1067
1068 Make the turtle visible.
1069
1070 .. doctest::
1071
1072 >>> turtle.showturtle()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001073
1074
1075.. function:: isvisible()
1076
1077 Return True if the Turtle is shown, False if it's hidden.
1078
1079 >>> turtle.hideturtle()
R. David Murrayf877feb2009-05-05 02:08:52 +00001080 >>> turtle.isvisible()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001081 False
R. David Murrayf877feb2009-05-05 02:08:52 +00001082 >>> turtle.showturtle()
1083 >>> turtle.isvisible()
1084 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001085
1086
1087Appearance
1088~~~~~~~~~~
1089
1090.. function:: shape(name=None)
1091
1092 :param name: a string which is a valid shapename
1093
1094 Set turtle shape to shape with given *name* or, if name is not given, return
1095 name of current shape. Shape with *name* must exist in the TurtleScreen's
1096 shape dictionary. Initially there are the following polygon shapes: "arrow",
1097 "turtle", "circle", "square", "triangle", "classic". To learn about how to
1098 deal with shapes see Screen method :func:`register_shape`.
1099
R. David Murrayf877feb2009-05-05 02:08:52 +00001100 .. doctest::
1101
1102 >>> turtle.shape()
1103 'classic'
1104 >>> turtle.shape("turtle")
1105 >>> turtle.shape()
1106 'turtle'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001107
1108
1109.. function:: resizemode(rmode=None)
1110
1111 :param rmode: one of the strings "auto", "user", "noresize"
1112
1113 Set resizemode to one of the values: "auto", "user", "noresize". If *rmode*
1114 is not given, return current resizemode. Different resizemodes have the
1115 following effects:
1116
1117 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
1118 - "user": adapts the appearance of the turtle according to the values of
1119 stretchfactor and outlinewidth (outline), which are set by
1120 :func:`shapesize`.
1121 - "noresize": no adaption of the turtle's appearance takes place.
1122
1123 resizemode("user") is called by :func:`shapesize` when used with arguments.
1124
R. David Murrayf877feb2009-05-05 02:08:52 +00001125 .. doctest::
1126
1127 >>> turtle.resizemode()
1128 'noresize'
1129 >>> turtle.resizemode("auto")
1130 >>> turtle.resizemode()
1131 'auto'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001132
1133
1134.. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None)
1135
1136 :param stretch_wid: positive number
1137 :param stretch_len: positive number
1138 :param outline: positive number
1139
1140 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set
1141 resizemode to "user". If and only if resizemode is set to "user", the turtle
1142 will be displayed stretched according to its stretchfactors: *stretch_wid* is
1143 stretchfactor perpendicular to its orientation, *stretch_len* is
1144 stretchfactor in direction of its orientation, *outline* determines the width
1145 of the shapes's outline.
1146
R. David Murrayf877feb2009-05-05 02:08:52 +00001147 .. doctest::
1148
1149 >>> turtle.shapesize()
1150 (1, 1, 1)
1151 >>> turtle.resizemode("user")
1152 >>> turtle.shapesize(5, 5, 12)
1153 >>> turtle.shapesize()
1154 (5, 5, 12)
1155 >>> turtle.shapesize(outline=8)
1156 >>> turtle.shapesize()
1157 (5, 5, 8)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001158
1159
1160.. function:: tilt(angle)
1161
1162 :param angle: a number
1163
1164 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
1165 change the turtle's heading (direction of movement).
1166
R. David Murrayf877feb2009-05-05 02:08:52 +00001167 .. doctest::
1168
1169 >>> turtle.reset()
1170 >>> turtle.shape("circle")
1171 >>> turtle.shapesize(5,2)
1172 >>> turtle.tilt(30)
1173 >>> turtle.fd(50)
1174 >>> turtle.tilt(30)
1175 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001176
1177
1178.. function:: settiltangle(angle)
1179
1180 :param angle: a number
1181
1182 Rotate the turtleshape to point in the direction specified by *angle*,
1183 regardless of its current tilt-angle. *Do not* change the turtle's heading
1184 (direction of movement).
1185
R. David Murrayf877feb2009-05-05 02:08:52 +00001186 .. doctest::
1187
1188 >>> turtle.reset()
1189 >>> turtle.shape("circle")
1190 >>> turtle.shapesize(5,2)
1191 >>> turtle.settiltangle(45)
1192 >>> turtle.fd(50)
1193 >>> turtle.settiltangle(-45)
1194 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001195
1196
1197.. function:: tiltangle()
1198
1199 Return the current tilt-angle, i.e. the angle between the orientation of the
1200 turtleshape and the heading of the turtle (its direction of movement).
1201
R. David Murrayf877feb2009-05-05 02:08:52 +00001202 .. doctest::
1203
1204 >>> turtle.reset()
1205 >>> turtle.shape("circle")
1206 >>> turtle.shapesize(5,2)
1207 >>> turtle.tilt(45)
1208 >>> turtle.tiltangle()
1209 45.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001210
1211
1212Using events
1213------------
1214
1215.. function:: onclick(fun, btn=1, add=None)
1216
1217 :param fun: a function with two arguments which will be called with the
1218 coordinates of the clicked point on the canvas
1219 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1220 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1221 added, otherwise it will replace a former binding
1222
1223 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1224 existing bindings are removed. Example for the anonymous turtle, i.e. the
1225 procedural way:
1226
R. David Murrayf877feb2009-05-05 02:08:52 +00001227 .. doctest::
1228
1229 >>> def turn(x, y):
1230 ... left(180)
1231 ...
1232 >>> onclick(turn) # Now clicking into the turtle will turn it.
1233 >>> onclick(None) # event-binding will be removed
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001234
1235
1236.. function:: onrelease(fun, btn=1, add=None)
1237
1238 :param fun: a function with two arguments which will be called with the
1239 coordinates of the clicked point on the canvas
1240 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1241 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1242 added, otherwise it will replace a former binding
1243
1244 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1245 ``None``, existing bindings are removed.
1246
R. David Murrayf877feb2009-05-05 02:08:52 +00001247 .. doctest::
1248
1249 >>> class MyTurtle(Turtle):
1250 ... def glow(self,x,y):
1251 ... self.fillcolor("red")
1252 ... def unglow(self,x,y):
1253 ... self.fillcolor("")
1254 ...
1255 >>> turtle = MyTurtle()
1256 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1257 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001258
1259
1260.. function:: ondrag(fun, btn=1, add=None)
1261
1262 :param fun: a function with two arguments which will be called with the
1263 coordinates of the clicked point on the canvas
1264 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1265 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1266 added, otherwise it will replace a former binding
1267
1268 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1269 existing bindings are removed.
1270
1271 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1272 mouse-click event on that turtle.
1273
R. David Murrayf877feb2009-05-05 02:08:52 +00001274 .. doctest::
1275
1276 >>> turtle.ondrag(turtle.goto)
1277
1278 Subsequently, clicking and dragging the Turtle will move it across
1279 the screen thereby producing handdrawings (if pen is down).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001280
1281
1282Special Turtle methods
1283----------------------
1284
1285.. function:: begin_poly()
1286
1287 Start recording the vertices of a polygon. Current turtle position is first
1288 vertex of polygon.
1289
1290
1291.. function:: end_poly()
1292
1293 Stop recording the vertices of a polygon. Current turtle position is last
1294 vertex of polygon. This will be connected with the first vertex.
1295
1296
1297.. function:: get_poly()
1298
1299 Return the last recorded polygon.
1300
R. David Murrayf877feb2009-05-05 02:08:52 +00001301 .. doctest::
1302
1303 >>> turtle.home()
1304 >>> turtle.begin_poly()
1305 >>> turtle.fd(100)
1306 >>> turtle.left(20)
1307 >>> turtle.fd(30)
1308 >>> turtle.left(60)
1309 >>> turtle.fd(50)
1310 >>> turtle.end_poly()
1311 >>> p = turtle.get_poly()
1312 >>> register_shape("myFavouriteShape", p)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001313
1314
1315.. function:: clone()
1316
1317 Create and return a clone of the turtle with same position, heading and
1318 turtle properties.
1319
R. David Murrayf877feb2009-05-05 02:08:52 +00001320 .. doctest::
1321
1322 >>> mick = Turtle()
1323 >>> joe = mick.clone()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001324
1325
1326.. function:: getturtle()
1327
1328 Return the Turtle object itself. Only reasonable use: as a function to
1329 return the "anonymous turtle":
1330
R. David Murrayf877feb2009-05-05 02:08:52 +00001331 .. doctest::
1332
1333 >>> pet = getturtle()
1334 >>> pet.fd(50)
1335 >>> pet
1336 <turtle.Turtle object at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001337
1338
1339.. function:: getscreen()
1340
1341 Return the :class:`TurtleScreen` object the turtle is drawing on.
1342 TurtleScreen methods can then be called for that object.
1343
R. David Murrayf877feb2009-05-05 02:08:52 +00001344 .. doctest::
1345
1346 >>> ts = turtle.getscreen()
1347 >>> ts
1348 <turtle._Screen object at 0x...>
1349 >>> ts.bgcolor("pink")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001350
1351
1352.. function:: setundobuffer(size)
1353
1354 :param size: an integer or ``None``
1355
1356 Set or disable undobuffer. If *size* is an integer an empty undobuffer of
1357 given size is installed. *size* gives the maximum number of turtle actions
1358 that can be undone by the :func:`undo` method/function. If *size* is
1359 ``None``, the undobuffer is disabled.
1360
R. David Murrayf877feb2009-05-05 02:08:52 +00001361 .. doctest::
1362
1363 >>> turtle.setundobuffer(42)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001364
1365
1366.. function:: undobufferentries()
1367
1368 Return number of entries in the undobuffer.
1369
R. David Murrayf877feb2009-05-05 02:08:52 +00001370 .. doctest::
1371
1372 >>> while undobufferentries():
1373 ... undo()
1374
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001375
1376
1377.. _compoundshapes:
1378
1379Excursus about the use of compound shapes
1380-----------------------------------------
1381
1382To use compound turtle shapes, which consist of several polygons of different
1383color, you must use the helper class :class:`Shape` explicitly as described
1384below:
1385
13861. Create an empty Shape object of type "compound".
13872. Add as many components to this object as desired, using the
1388 :meth:`addcomponent` method.
1389
1390 For example:
1391
R. David Murrayf877feb2009-05-05 02:08:52 +00001392 .. doctest::
1393
1394 >>> s = Shape("compound")
1395 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1396 >>> s.addcomponent(poly1, "red", "blue")
1397 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1398 >>> s.addcomponent(poly2, "blue", "red")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001399
14003. Now add the Shape to the Screen's shapelist and use it:
1401
R. David Murrayf877feb2009-05-05 02:08:52 +00001402 .. doctest::
1403
1404 >>> register_shape("myshape", s)
1405 >>> shape("myshape")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001406
1407
1408.. note::
1409
1410 The :class:`Shape` class is used internally by the :func:`register_shape`
1411 method in different ways. The application programmer has to deal with the
1412 Shape class *only* when using compound shapes like shown above!
1413
1414
1415Methods of TurtleScreen/Screen and corresponding functions
1416==========================================================
1417
1418Most of the examples in this section refer to a TurtleScreen instance called
1419``screen``.
1420
R. David Murrayf877feb2009-05-05 02:08:52 +00001421.. doctest::
1422 :hide:
1423
1424 >>> screen = Screen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001425
1426Window control
1427--------------
1428
1429.. function:: bgcolor(*args)
1430
1431 :param args: a color string or three numbers in the range 0..colormode or a
1432 3-tuple of such numbers
1433
1434 Set or return background color of the TurtleScreen.
1435
R. David Murrayf877feb2009-05-05 02:08:52 +00001436 .. doctest::
1437
1438 >>> screen.bgcolor("orange")
1439 >>> screen.bgcolor()
1440 'orange'
1441 >>> screen.bgcolor("#800080")
1442 >>> screen.bgcolor()
1443 (128, 0, 128)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001444
1445
1446.. function:: bgpic(picname=None)
1447
1448 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
1449
1450 Set background image or return name of current backgroundimage. If *picname*
1451 is a filename, set the corresponding image as background. If *picname* is
1452 ``"nopic"``, delete background image, if present. If *picname* is ``None``,
R. David Murrayf877feb2009-05-05 02:08:52 +00001453 return the filename of the current backgroundimage. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001454
R. David Murrayf877feb2009-05-05 02:08:52 +00001455 >>> screen.bgpic()
1456 'nopic'
1457 >>> screen.bgpic("landscape.gif")
1458 >>> screen.bgpic()
1459 "landscape.gif"
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001460
1461
1462.. function:: clear()
1463 clearscreen()
1464
1465 Delete all drawings and all turtles from the TurtleScreen. Reset the now
1466 empty TurtleScreen to its initial state: white background, no background
1467 image, no event bindings and tracing on.
1468
1469 .. note::
1470 This TurtleScreen method is available as a global function only under the
1471 name ``clearscreen``. The global function ``clear`` is another one
1472 derived from the Turtle method ``clear``.
1473
1474
1475.. function:: reset()
1476 resetscreen()
1477
1478 Reset all Turtles on the Screen to their initial state.
1479
1480 .. note::
1481 This TurtleScreen method is available as a global function only under the
1482 name ``resetscreen``. The global function ``reset`` is another one
1483 derived from the Turtle method ``reset``.
1484
1485
1486.. function:: screensize(canvwidth=None, canvheight=None, bg=None)
1487
Georg Brandlff2ad0e2009-04-27 16:51:45 +00001488 :param canvwidth: positive integer, new width of canvas in pixels
1489 :param canvheight: positive integer, new height of canvas in pixels
1490 :param bg: colorstring or color-tuple, new background color
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001491
1492 If no arguments are given, return current (canvaswidth, canvasheight). Else
1493 resize the canvas the turtles are drawing on. Do not alter the drawing
1494 window. To observe hidden parts of the canvas, use the scrollbars. With this
1495 method, one can make visible those parts of a drawing which were outside the
1496 canvas before.
1497
R. David Murrayf877feb2009-05-05 02:08:52 +00001498 >>> screen.screensize()
1499 (400, 300)
1500 >>> screen.screensize(2000,1500)
1501 >>> screen.screensize()
1502 (2000, 1500)
1503
1504 e.g. to search for an erroneously escaped turtle ;-)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001505
1506
1507.. function:: setworldcoordinates(llx, lly, urx, ury)
1508
1509 :param llx: a number, x-coordinate of lower left corner of canvas
1510 :param lly: a number, y-coordinate of lower left corner of canvas
1511 :param urx: a number, x-coordinate of upper right corner of canvas
1512 :param ury: a number, y-coordinate of upper right corner of canvas
1513
1514 Set up user-defined coordinate system and switch to mode "world" if
1515 necessary. This performs a ``screen.reset()``. If mode "world" is already
1516 active, all drawings are redrawn according to the new coordinates.
1517
1518 **ATTENTION**: in user-defined coordinate systems angles may appear
1519 distorted.
1520
R. David Murrayf877feb2009-05-05 02:08:52 +00001521 .. doctest::
1522
1523 >>> screen.reset()
1524 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1525 >>> for _ in range(72):
1526 ... left(10)
1527 ...
1528 >>> for _ in range(8):
1529 ... left(45); fd(2) # a regular octagon
1530
1531 .. doctest::
1532 :hide:
1533
1534 >>> screen.reset()
1535 >>> for t in turtles():
1536 ... t.reset()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001537
1538
1539Animation control
1540-----------------
1541
1542.. function:: delay(delay=None)
1543
1544 :param delay: positive integer
1545
1546 Set or return the drawing *delay* in milliseconds. (This is approximately
Georg Brandl2ee470f2008-07-16 12:55:28 +00001547 the time interval between two consecutive canvas updates.) The longer the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001548 drawing delay, the slower the animation.
1549
1550 Optional argument:
1551
R. David Murrayf877feb2009-05-05 02:08:52 +00001552 .. doctest::
1553
1554 >>> screen.delay()
1555 10
1556 >>> screen.delay(5)
1557 >>> screen.delay()
1558 5
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001559
1560
1561.. function:: tracer(n=None, delay=None)
1562
1563 :param n: nonnegative integer
1564 :param delay: nonnegative integer
1565
1566 Turn turtle animation on/off and set delay for update drawings. If *n* is
1567 given, only each n-th regular screen update is really performed. (Can be
1568 used to accelerate the drawing of complex graphics.) Second argument sets
1569 delay value (see :func:`delay`).
1570
R. David Murrayf877feb2009-05-05 02:08:52 +00001571 .. doctest::
1572
1573 >>> screen.tracer(8, 25)
1574 >>> dist = 2
1575 >>> for i in range(200):
1576 ... fd(dist)
1577 ... rt(90)
1578 ... dist += 2
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001579
1580
1581.. function:: update()
1582
1583 Perform a TurtleScreen update. To be used when tracer is turned off.
1584
1585See also the RawTurtle/Turtle method :func:`speed`.
1586
1587
1588Using screen events
1589-------------------
1590
1591.. function:: listen(xdummy=None, ydummy=None)
1592
1593 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1594 are provided in order to be able to pass :func:`listen` to the onclick method.
1595
1596
1597.. function:: onkey(fun, key)
1598
1599 :param fun: a function with no arguments or ``None``
1600 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1601
1602 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1603 are removed. Remark: in order to be able to register key-events, TurtleScreen
1604 must have the focus. (See method :func:`listen`.)
1605
R. David Murrayf877feb2009-05-05 02:08:52 +00001606 .. doctest::
1607
1608 >>> def f():
1609 ... fd(50)
1610 ... lt(60)
1611 ...
1612 >>> screen.onkey(f, "Up")
1613 >>> screen.listen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001614
1615
1616.. function:: onclick(fun, btn=1, add=None)
1617 onscreenclick(fun, btn=1, add=None)
1618
1619 :param fun: a function with two arguments which will be called with the
1620 coordinates of the clicked point on the canvas
1621 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1622 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1623 added, otherwise it will replace a former binding
1624
1625 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1626 existing bindings are removed.
1627
1628 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1629 named turtle:
1630
R. David Murrayf877feb2009-05-05 02:08:52 +00001631 .. doctest::
1632
1633 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1634 >>> # make the turtle move to the clicked point.
1635 >>> screen.onclick(None) # remove event binding again
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001636
1637 .. note::
1638 This TurtleScreen method is available as a global function only under the
1639 name ``onscreenclick``. The global function ``onclick`` is another one
1640 derived from the Turtle method ``onclick``.
1641
1642
1643.. function:: ontimer(fun, t=0)
1644
1645 :param fun: a function with no arguments
1646 :param t: a number >= 0
1647
1648 Install a timer that calls *fun* after *t* milliseconds.
1649
R. David Murrayf877feb2009-05-05 02:08:52 +00001650 .. doctest::
1651
1652 >>> running = True
1653 >>> def f():
1654 ... if running:
1655 ... fd(50)
1656 ... lt(60)
1657 ... screen.ontimer(f, 250)
1658 >>> f() ### makes the turtle march around
1659 >>> running = False
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001660
1661
1662Settings and special methods
1663----------------------------
1664
1665.. function:: mode(mode=None)
1666
1667 :param mode: one of the strings "standard", "logo" or "world"
1668
1669 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1670 is not given, current mode is returned.
1671
1672 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1673 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1674 "world coordinates". **Attention**: in this mode angles appear distorted if
1675 ``x/y`` unit-ratio doesn't equal 1.
1676
1677 ============ ========================= ===================
1678 Mode Initial turtle heading positive angles
1679 ============ ========================= ===================
1680 "standard" to the right (east) counterclockwise
1681 "logo" upward (north) clockwise
1682 ============ ========================= ===================
1683
R. David Murrayf877feb2009-05-05 02:08:52 +00001684 .. doctest::
1685
1686 >>> mode("logo") # resets turtle heading to north
1687 >>> mode()
1688 'logo'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001689
1690
1691.. function:: colormode(cmode=None)
1692
1693 :param cmode: one of the values 1.0 or 255
1694
1695 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1696 values of color triples have to be in the range 0..\ *cmode*.
1697
R. David Murrayf877feb2009-05-05 02:08:52 +00001698 .. doctest::
1699
1700 >>> screen.colormode(1)
1701 >>> turtle.pencolor(240, 160, 80)
1702 Traceback (most recent call last):
1703 ...
1704 TurtleGraphicsError: bad color sequence: (240, 160, 80)
1705 >>> screen.colormode()
1706 1.0
1707 >>> screen.colormode(255)
1708 >>> screen.colormode()
1709 255
1710 >>> turtle.pencolor(240,160,80)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001711
1712
1713.. function:: getcanvas()
1714
1715 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1716 do with a Tkinter Canvas.
1717
R. David Murrayf877feb2009-05-05 02:08:52 +00001718 .. doctest::
1719
1720 >>> cv = screen.getcanvas()
1721 >>> cv
1722 <turtle.ScrolledCanvas instance at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001723
1724
1725.. function:: getshapes()
1726
1727 Return a list of names of all currently available turtle shapes.
1728
R. David Murrayf877feb2009-05-05 02:08:52 +00001729 .. doctest::
1730
1731 >>> screen.getshapes()
1732 ['arrow', 'blank', 'circle', ..., 'turtle']
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001733
1734
1735.. function:: register_shape(name, shape=None)
1736 addshape(name, shape=None)
1737
1738 There are three different ways to call this function:
1739
1740 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
R. David Murrayf877feb2009-05-05 02:08:52 +00001741 corresponding image shape. ::
1742
1743 >>> screen.register_shape("turtle.gif")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001744
1745 .. note::
1746 Image shapes *do not* rotate when turning the turtle, so they do not
1747 display the heading of the turtle!
1748
1749 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1750 coordinates: Install the corresponding polygon shape.
1751
R. David Murrayf877feb2009-05-05 02:08:52 +00001752 .. doctest::
1753
1754 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
1755
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001756 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
1757 object: Install the corresponding compound shape.
1758
1759 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1760 shapes can be used by issuing the command ``shape(shapename)``.
1761
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001762
1763.. function:: turtles()
1764
1765 Return the list of turtles on the screen.
1766
R. David Murrayf877feb2009-05-05 02:08:52 +00001767 .. doctest::
1768
1769 >>> for turtle in screen.turtles():
1770 ... turtle.color("red")
Georg Brandl116aa622007-08-15 14:28:22 +00001771
Georg Brandl116aa622007-08-15 14:28:22 +00001772
1773.. function:: window_height()
1774
R. David Murrayf877feb2009-05-05 02:08:52 +00001775 Return the height of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001776
R. David Murrayf877feb2009-05-05 02:08:52 +00001777 >>> screen.window_height()
1778 480
Georg Brandl116aa622007-08-15 14:28:22 +00001779
Georg Brandl116aa622007-08-15 14:28:22 +00001780
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001781.. function:: window_width()
1782
R. David Murrayf877feb2009-05-05 02:08:52 +00001783 Return the width of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001784
R. David Murrayf877feb2009-05-05 02:08:52 +00001785 >>> screen.window_width()
1786 640
Georg Brandl116aa622007-08-15 14:28:22 +00001787
1788
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001789.. _screenspecific:
Georg Brandl116aa622007-08-15 14:28:22 +00001790
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001791Methods specific to Screen, not inherited from TurtleScreen
1792-----------------------------------------------------------
1793
1794.. function:: bye()
1795
1796 Shut the turtlegraphics window.
Georg Brandl116aa622007-08-15 14:28:22 +00001797
1798
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001799.. function:: exitonclick()
Georg Brandl116aa622007-08-15 14:28:22 +00001800
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001801 Bind bye() method to mouse clicks on the Screen.
Georg Brandl116aa622007-08-15 14:28:22 +00001802
1803
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001804 If the value "using_IDLE" in the configuration dictionary is ``False``
1805 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
1806 (no subprocess) is used, this value should be set to ``True`` in
1807 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1808 client script.
Georg Brandl116aa622007-08-15 14:28:22 +00001809
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001810
1811.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
1812
1813 Set the size and position of the main window. Default values of arguments
1814 are stored in the configuration dicionary and can be changed via a
1815 :file:`turtle.cfg` file.
1816
1817 :param width: if an integer, a size in pixels, if a float, a fraction of the
1818 screen; default is 50% of screen
1819 :param height: if an integer, the height in pixels, if a float, a fraction of
1820 the screen; default is 75% of screen
1821 :param startx: if positive, starting position in pixels from the left
1822 edge of the screen, if negative from the right edge, if None,
1823 center window horizontally
1824 :param startx: if positive, starting position in pixels from the top
1825 edge of the screen, if negative from the bottom edge, if None,
1826 center window vertically
1827
R. David Murrayf877feb2009-05-05 02:08:52 +00001828 .. doctest::
1829
1830 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1831 >>> # sets window to 200x200 pixels, in upper left of screen
1832 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1833 >>> # sets window to 75% of screen by 50% of screen and centers
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001834
1835
1836.. function:: title(titlestring)
1837
1838 :param titlestring: a string that is shown in the titlebar of the turtle
1839 graphics window
1840
1841 Set title of turtle window to *titlestring*.
1842
R. David Murrayf877feb2009-05-05 02:08:52 +00001843 .. doctest::
1844
1845 >>> screen.title("Welcome to the turtle zoo!")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001846
1847
1848The public classes of the module :mod:`turtle`
1849==============================================
1850
1851
1852.. class:: RawTurtle(canvas)
1853 RawPen(canvas)
1854
1855 :param canvas: a :class:`Tkinter.Canvas`, a :class:`ScrolledCanvas` or a
1856 :class:`TurtleScreen`
1857
R. David Murrayf877feb2009-05-05 02:08:52 +00001858 Create a turtle. The turtle has all methods described above as "methods of
1859 Turtle/RawTurtle".
Georg Brandl116aa622007-08-15 14:28:22 +00001860
1861
1862.. class:: Turtle()
1863
R. David Murrayf877feb2009-05-05 02:08:52 +00001864 Subclass of RawTurtle, has the same interface but draws on a default
1865 :class:`Screen` object created automatically when needed for the first time.
Georg Brandl116aa622007-08-15 14:28:22 +00001866
1867
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001868.. class:: TurtleScreen(cv)
Georg Brandl116aa622007-08-15 14:28:22 +00001869
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001870 :param cv: a :class:`Tkinter.Canvas`
1871
1872 Provides screen oriented methods like :func:`setbg` etc. that are described
1873 above.
1874
1875.. class:: Screen()
1876
1877 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
1878
Georg Brandl48310cd2009-01-03 21:18:54 +00001879
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001880.. class:: ScrolledCavas(master)
1881
1882 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
1883 a Tkinter-canvas with scrollbars added
1884
1885 Used by class Screen, which thus automatically provides a ScrolledCanvas as
1886 playground for the turtles.
1887
1888.. class:: Shape(type_, data)
1889
1890 :param type\_: one of the strings "polygon", "image", "compound"
1891
1892 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
1893 specification:
Georg Brandl116aa622007-08-15 14:28:22 +00001894
1895
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001896 =========== ===========
1897 *type_* *data*
1898 =========== ===========
1899 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
1900 "image" an image (in this form only used internally!)
Georg Brandlae2dbe22009-03-13 19:04:40 +00001901 "compound" ``None`` (a compound shape has to be constructed using the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001902 :meth:`addcomponent` method)
1903 =========== ===========
Georg Brandl48310cd2009-01-03 21:18:54 +00001904
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001905 .. method:: addcomponent(poly, fill, outline=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001906
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001907 :param poly: a polygon, i.e. a tuple of pairs of numbers
1908 :param fill: a color the *poly* will be filled with
1909 :param outline: a color for the poly's outline (if given)
Georg Brandl48310cd2009-01-03 21:18:54 +00001910
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001911 Example:
Georg Brandl116aa622007-08-15 14:28:22 +00001912
R. David Murrayf877feb2009-05-05 02:08:52 +00001913 .. doctest::
1914
1915 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
1916 >>> s = Shape("compound")
1917 >>> s.addcomponent(poly, "red", "blue")
1918 >>> # ... add more components and then use register_shape()
Georg Brandl116aa622007-08-15 14:28:22 +00001919
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001920 See :ref:`compoundshapes`.
Georg Brandl116aa622007-08-15 14:28:22 +00001921
1922
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001923.. class:: Vec2D(x, y)
Georg Brandl116aa622007-08-15 14:28:22 +00001924
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001925 A two-dimensional vector class, used as a helper class for implementing
1926 turtle graphics. May be useful for turtle graphics programs too. Derived
1927 from tuple, so a vector is a tuple!
1928
1929 Provides (for *a*, *b* vectors, *k* number):
1930
1931 * ``a + b`` vector addition
1932 * ``a - b`` vector subtraction
1933 * ``a * b`` inner product
1934 * ``k * a`` and ``a * k`` multiplication with scalar
1935 * ``abs(a)`` absolute value of a
1936 * ``a.rotate(angle)`` rotation
1937
1938
1939Help and configuration
1940======================
1941
1942How to use help
1943---------------
1944
1945The public methods of the Screen and Turtle classes are documented extensively
1946via docstrings. So these can be used as online-help via the Python help
1947facilities:
1948
1949- When using IDLE, tooltips show the signatures and first lines of the
1950 docstrings of typed in function-/method calls.
1951
1952- Calling :func:`help` on methods or functions displays the docstrings::
1953
1954 >>> help(Screen.bgcolor)
1955 Help on method bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00001956
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001957 bgcolor(self, *args) unbound turtle.Screen method
1958 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00001959
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001960 Arguments (if given): a color string or three numbers
1961 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00001962
1963
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001964 >>> screen.bgcolor("orange")
1965 >>> screen.bgcolor()
1966 "orange"
1967 >>> screen.bgcolor(0.5,0,0.5)
1968 >>> screen.bgcolor()
1969 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00001970
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001971 >>> help(Turtle.penup)
1972 Help on method penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00001973
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001974 penup(self) unbound turtle.Turtle method
1975 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00001976
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001977 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00001978
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001979 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00001980
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001981 >>> turtle.penup()
1982
1983- The docstrings of the functions which are derived from methods have a modified
1984 form::
1985
1986 >>> help(bgcolor)
1987 Help on function bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00001988
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001989 bgcolor(*args)
1990 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00001991
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001992 Arguments (if given): a color string or three numbers
1993 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00001994
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001995 Example::
Georg Brandl48310cd2009-01-03 21:18:54 +00001996
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001997 >>> bgcolor("orange")
1998 >>> bgcolor()
1999 "orange"
2000 >>> bgcolor(0.5,0,0.5)
2001 >>> bgcolor()
2002 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002003
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002004 >>> help(penup)
2005 Help on function penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002006
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002007 penup()
2008 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002009
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002010 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002011
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002012 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002013
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002014 Example:
2015 >>> penup()
2016
2017These modified docstrings are created automatically together with the function
2018definitions that are derived from the methods at import time.
2019
2020
2021Translation of docstrings into different languages
2022--------------------------------------------------
2023
2024There is a utility to create a dictionary the keys of which are the method names
2025and the values of which are the docstrings of the public methods of the classes
2026Screen and Turtle.
2027
2028.. function:: write_docstringdict(filename="turtle_docstringdict")
2029
2030 :param filename: a string, used as filename
2031
2032 Create and write docstring-dictionary to a Python script with the given
2033 filename. This function has to be called explicitly (it is not used by the
2034 turtle graphics classes). The docstring dictionary will be written to the
2035 Python script :file:`{filename}.py`. It is intended to serve as a template
2036 for translation of the docstrings into different languages.
2037
2038If you (or your students) want to use :mod:`turtle` with online help in your
2039native language, you have to translate the docstrings and save the resulting
2040file as e.g. :file:`turtle_docstringdict_german.py`.
2041
2042If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2043will be read in at import time and will replace the original English docstrings.
2044
2045At the time of this writing there are docstring dictionaries in German and in
2046Italian. (Requests please to glingl@aon.at.)
2047
2048
2049
2050How to configure Screen and Turtles
2051-----------------------------------
2052
2053The built-in default configuration mimics the appearance and behaviour of the
2054old turtle module in order to retain best possible compatibility with it.
2055
2056If you want to use a different configuration which better reflects the features
2057of this module or which better fits to your needs, e.g. for use in a classroom,
2058you can prepare a configuration file ``turtle.cfg`` which will be read at import
2059time and modify the configuration according to its settings.
2060
2061The built in configuration would correspond to the following turtle.cfg::
2062
2063 width = 0.5
2064 height = 0.75
2065 leftright = None
2066 topbottom = None
2067 canvwidth = 400
2068 canvheight = 300
2069 mode = standard
2070 colormode = 1.0
2071 delay = 10
2072 undobuffersize = 1000
2073 shape = classic
2074 pencolor = black
2075 fillcolor = black
2076 resizemode = noresize
2077 visible = True
2078 language = english
2079 exampleturtle = turtle
2080 examplescreen = screen
2081 title = Python Turtle Graphics
2082 using_IDLE = False
2083
2084Short explanation of selected entries:
2085
2086- The first four lines correspond to the arguments of the :meth:`Screen.setup`
2087 method.
2088- Line 5 and 6 correspond to the arguments of the method
2089 :meth:`Screen.screensize`.
2090- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2091 info try ``help(shape)``.
2092- If you want to use no fillcolor (i.e. make the turtle transparent), you have
2093 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
2094 the cfg-file).
2095- If you want to reflect the turtle its state, you have to use ``resizemode =
2096 auto``.
2097- If you set e.g. ``language = italian`` the docstringdict
2098 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
2099 present on the import path, e.g. in the same directory as :mod:`turtle`.
2100- The entries *exampleturtle* and *examplescreen* define the names of these
2101 objects as they occur in the docstrings. The transformation of
2102 method-docstrings to function-docstrings will delete these names from the
2103 docstrings.
2104- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
2105 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
2106 mainloop.
2107
2108There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2109stored and an additional one in the current working directory. The latter will
2110override the settings of the first one.
2111
2112The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can
2113study it as an example and see its effects when running the demos (preferably
2114not from within the demo-viewer).
2115
2116
2117Demo scripts
2118============
2119
2120There is a set of demo scripts in the turtledemo directory located in the
2121:file:`Demo/turtle` directory in the source distribution.
2122
2123It contains:
2124
Georg Brandlae2dbe22009-03-13 19:04:40 +00002125- a set of 15 demo scripts demonstrating different features of the new module
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002126 :mod:`turtle`
2127- a demo viewer :file:`turtleDemo.py` which can be used to view the sourcecode
2128 of the scripts and run them at the same time. 14 of the examples can be
2129 accessed via the Examples menu; all of them can also be run standalone.
2130- The example :file:`turtledemo_two_canvases.py` demonstrates the simultaneous
2131 use of two canvases with the turtle module. Therefore it only can be run
2132 standalone.
2133- There is a :file:`turtle.cfg` file in this directory, which also serves as an
2134 example for how to write and use such files.
2135
2136The demoscripts are:
2137
2138+----------------+------------------------------+-----------------------+
2139| Name | Description | Features |
2140+----------------+------------------------------+-----------------------+
2141| bytedesign | complex classical | :func:`tracer`, delay,|
2142| | turtlegraphics pattern | :func:`update` |
2143+----------------+------------------------------+-----------------------+
2144| chaos | graphs verhust dynamics, | world coordinates |
2145| | proves that you must not | |
2146| | trust computers' computations| |
2147+----------------+------------------------------+-----------------------+
2148| clock | analog clock showing time | turtles as clock's |
2149| | of your computer | hands, ontimer |
2150+----------------+------------------------------+-----------------------+
2151| colormixer | experiment with r, g, b | :func:`ondrag` |
2152+----------------+------------------------------+-----------------------+
2153| fractalcurves | Hilbert & Koch curves | recursion |
2154+----------------+------------------------------+-----------------------+
2155| lindenmayer | ethnomathematics | L-System |
2156| | (indian kolams) | |
2157+----------------+------------------------------+-----------------------+
2158| minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
2159| | | as Hanoi discs |
2160| | | (shape, shapesize) |
2161+----------------+------------------------------+-----------------------+
2162| paint | super minimalistic | :func:`onclick` |
2163| | drawing program | |
2164+----------------+------------------------------+-----------------------+
2165| peace | elementary | turtle: appearance |
2166| | | and animation |
2167+----------------+------------------------------+-----------------------+
2168| penrose | aperiodic tiling with | :func:`stamp` |
2169| | kites and darts | |
2170+----------------+------------------------------+-----------------------+
2171| planet_and_moon| simulation of | compound shapes, |
2172| | gravitational system | :class:`Vec2D` |
2173+----------------+------------------------------+-----------------------+
2174| tree | a (graphical) breadth | :func:`clone` |
2175| | first tree (using generators)| |
2176+----------------+------------------------------+-----------------------+
2177| wikipedia | a pattern from the wikipedia | :func:`clone`, |
2178| | article on turtle graphics | :func:`undo` |
2179+----------------+------------------------------+-----------------------+
2180| yingyang | another elementary example | :func:`circle` |
2181+----------------+------------------------------+-----------------------+
2182
2183Have fun!
2184
2185
2186Changes since Python 2.6
2187========================
2188
Georg Brandl48310cd2009-01-03 21:18:54 +00002189- The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and
2190 :meth:`Turtle.window_height` have been eliminated.
2191 Methods with these names and functionality are now available only
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002192 as methods of :class:`Screen`. The functions derived from these remain
Georg Brandl48310cd2009-01-03 21:18:54 +00002193 available. (In fact already in Python 2.6 these methods were merely
2194 duplications of the corresponding
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002195 :class:`TurtleScreen`/:class:`Screen`-methods.)
2196
Georg Brandl48310cd2009-01-03 21:18:54 +00002197- The method :meth:`Turtle.fill` has been eliminated.
2198 The behaviour of :meth:`begin_fill` and :meth:`end_fill`
2199 have changed slightly: now every filling-process must be completed with an
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002200 ``end_fill()`` call.
Georg Brandl48310cd2009-01-03 21:18:54 +00002201
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002202- A method :meth:`Turtle.filling` has been added. It returns a boolean
2203 value: ``True`` if a filling process is under way, ``False`` otherwise.
2204 This behaviour corresponds to a ``fill()`` call without arguments in
Georg Brandl23d11d32008-09-21 07:50:52 +00002205 Python 2.6.
Georg Brandl116aa622007-08-15 14:28:22 +00002206
R. David Murrayf877feb2009-05-05 02:08:52 +00002207
2208.. doctest::
2209 :hide:
2210
2211 >>> for turtle in turtles():
2212 ... turtle.reset()
2213 >>> turtle.penup()
2214 >>> turtle.goto(-200,25)
2215 >>> turtle.pendown()
2216 >>> turtle.write("No one expects the Spanish Inquisition!",
2217 ... font=("Arial", 20, "normal"))
2218 >>> turtle.penup()
2219 >>> turtle.goto(-100,-50)
2220 >>> turtle.pendown()
2221 >>> turtle.write("Our two chief Turtles are...",
2222 ... font=("Arial", 16, "normal"))
2223 >>> turtle.penup()
2224 >>> turtle.goto(-450,-75)
2225 >>> turtle.write(str(turtles()))