blob: 5185a8ae1aab20b37fa66512cfad8d108ba88d0b [file] [log] [blame]
Martin v. Löwis87184592008-06-04 06:29:55 +00001========================================
Georg Brandl6634bf22008-05-20 07:13:37 +00002:mod:`turtle` --- Turtle graphics for Tk
3========================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00004
Martin v. Löwis87184592008-06-04 06:29:55 +00005------------
6Introduction
7------------
8
9Turtle graphics is a popular way for introducing programming to
10kids. It was part of the original Logo programming language developed
11by Wally Feurzig and Seymour Papert in 1966.
12
13Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it
14the command turtle.forward(15), and it moves (on-screen!) 15 pixels in
15the direction it is facing, drawing a line as it moves. Give it the
16command turtle.left(25), and it rotates in-place 25 degrees clockwise.
17
18By combining together these and similar commands, intricate shapes and
19pictures can easily be drawn.
20
21The module turtle.py is an extended reimplementation of turtle.py from
22the Python standard distribution up to version Python 2.5.
23
24It tries to keep the merits of turtle.py and to be (nearly) 100%
25compatible with it. This means in the first place to enable the
26learning programmer to use all the commands, classes and methods
27interactively when using the module from within IDLE run with
28the -n switch.
29
30The turtle module provides turtle graphics primitives, in both
31object-oriented and procedure-oriented ways. Because it uses Tkinter
32for the underlying graphics, it needs a version of python installed
33with Tk support.
34
35The objectoriented interface uses essentially two+two classes:
36
371. The TurtleScreen class defines graphics windows as a playground for the
38 drawing turtles. It's constructor needs a Tk-Canvas or a ScrolledCanvas
39 as argument. It should be used when turtle.py is used as part of some
40 application.
41
42 Derived from TurtleScreen is the subclass Screen. Screen is implemented
43 as sort of singleton, so there can exist only one instance of Screen at a
44 time. It should be used when turtle.py is used as a standalone tool for
45 doing graphics.
46
47 All methods of TurtleScreen/Screen also exist as functions, i. e.
48 as part of the procedure-oriented interface.
49
502. RawTurtle (alias: RawPen) defines Turtle-objects which draw on a
51 TurtleScreen. It's constructor needs a Canvas/ScrolledCanvas/Turtlescreen
52 as argument, so the RawTurtle objects know where to draw.
53
54 Derived from RawTurtle is the subclass Turtle (alias: Pen), which
55 draws on "the" Screen - instance which is automatically created,
56 if not already present.
57
58 All methods of RawTurtle/Turtle also exist as functions, i. e.
59 part of the procedure-oriented interface.
60
61The procedural interface uses functions which are derived from the methods
62of the classes Screen and Turtle. They have the same names as the
63corresponding methods. A screen-object is automativally created
64whenever a function derived form a Screen-method is called. An (unnamed)
65turtle object is automatically created whenever any of the functions
66derived from a Turtle method is called.
67
68To use multiple turtles an a screen one has to use the objectoriented
69interface.
70
71
72IMPORTANT NOTE!
73
74In the following documentation the argumentlist for functions is given.
75--->> Methods, of course, have the additional first argument self <<---
76--->> which is omitted here. <<---
77
78
79--------------------------------------------------
80OVERVIEW over available Turtle and Screen methods:
81--------------------------------------------------
82
83(A) TURTLE METHODS:
84===================
85
86I. TURTLE MOTION
87-----------------
88
89MOVE AND DRAW
90 forward | fd
91 back | bk | back
92 right | rt
93 left | lt
94 goto | setpos | setposition
95 setx
96 sety
97 setheading | seth
98 home
99 circle
100 dot
101 stamp
102 clearstamp
103 clearstamps
104 undo
105 speed
106
107TELL TURTLE'S STATE
108 position | pos
109 towards
110 xcor
111 ycor
112 heading
113 distance
114
115SETTING AND MEASUREMENT
116 degrees
117 radians
118
119II. PEN CONTROL
120---------------
121
122DRAWING STATE
123 pendown | pd | down
124 penup | pu | up
125 pensize | width
126 pen
127 isdown
128
129COLOR CONTROL
130 color
131 pencolor
132 fillcolor
133
134FILLING
135 fill
136 begin_fill
137 end_fill
138
139MORE DRAWING CONTROL
140 reset
141 clear
142 write
143
144III. TURTLE STATE
145-----------------
146
147VISIBILITY
148 showturtle | st
149 hideturtle | ht
150 isvisible
151
152APPEARANCE
153 shape
154 resizemode
155 shapesize | turtlesize
156 settiltangle
157 tiltangle
158 tilt
159
160IV. USING EVENTS
161----------------
162 onclick
163 onrelease
164 ondrag
165
166V. SPECIAL TURTLE METHODS
167-------------------------
168 begin_poly
169 end_poly
170 get_poly
171 clone
172 getturtle | getpen
173 getscreen
174 setundobuffer
175 undobufferentries
176 tracer
177 window_width
178 window_height
179
180..EXCURSUS ABOUT THE USE OF COMPOUND SHAPES
181..-----------------------------------------
182
183(B) METHODS OF TurtleScreen/Screen
184==================================
185
186I. WINDOW CONTROL
187-----------------
188 bgcolor
189 bgpic
190 clear | clearscreen
191 reset | resetscreen
192 screensize
193 setworldcoordinates
194
195II. ANIMATION CONTROL
196---------------------
197 delay
198 tracer
199 update
200
201III. USING SCREEN EVENTS
202------------------------
203 listen
204 onkey
205 onclick | onscreenclick
206 ontimer
207
208IV. SETTINGS AND SPECIAL METHODS
209--------------------------------
210 mode
211 colormode
212 getcanvas
213 getshapes
214 register_shape | addshape
215 turtles
216 window_height
217 window_width
218
219V. METHODS SPECIFIC TO Screen
220=============================
221 bye()
222 exitonclick()
223 setup()
224 title()
225
226---------------end of OVERVIEW ---------------------------
227
228
229
2302. METHODS OF RawTurtle/Turtle AND CORRESPONDING FUNCTIONS
231==========================================================
232
233(I) TURTLE MOTION:
234------------------
235
236(a) --- MOVE (AND DRAW)
237
238
239 .. method:: forward(distance)
240 .. method:: fd(distance)
241 distance -- a number (integer or float)
242
243 Move the turtle forward by the specified distance, in the direction
244 the turtle is headed.
245
246 Example (for a Turtle instance named turtle)::
247 >>> turtle.position()
248 (0.00, 0.00)
249 >>> turtle.forward(25)
250 >>> turtle.position()
251 (25.00,0.00)
252 >>> turtle.forward(-75)
253 >>> turtle.position()
254 (-50.00,0.00)
255
256
257 .. method:: back(distance)
258 .. method:: bk(distance)
259 .. method:: backward(distance)
260 distance -- a number
261
262 call: back(distance)
263 --or: bk(distance)
264 --or: backward(distance)
265
266 Move the turtle backward by distance ,opposite to the direction the
267 turtle is headed. Do not change the turtle's heading.
268
269 Example (for a Turtle instance named turtle)::
270
271 >>> turtle.position()
272 (0.00, 0.00)
273 >>> turtle.backward(30)
274 >>> turtle.position()
275 (-30.00, 0.00)
276
277
278 .. method:: right(angle)
279 .. method:: rt(angle)
280 angle -- a number (integer or float)
281
282 Turn turtle right by angle units. (Units are by default degrees,
283 but can be set via the degrees() and radians() functions.)
284 Angle orientation depends on mode. (See this.)
285
286 Example (for a Turtle instance named turtle)::
287 >>> turtle.heading()
288 22.0
289 >>> turtle.right(45)
290 >>> turtle.heading()
291 337.0
292
293
294 .. method:: left(angle)
295 .. method:: lt(angle)
296 angle -- a number (integer or float)
297
298 Turn turtle left by angle units. (Units are by default degrees,
299 but can be set via the degrees() and radians() functions.)
300 Angle orientation depends on mode. (See this.)
301
302 Example (for a Turtle instance named turtle)::
303 >>> turtle.heading()
304 22.0
305 >>> turtle.left(45)
306 >>> turtle.heading()
307 67.0
308
309 .. method:: goto(x, y=None)
310 .. method:: setpos(x, y=None)
311 .. method:: setposition(x, y=None)
312 x -- a number or a pair/vector of numbers
313 y -- a number None
314
315 call: goto(x, y) # two coordinates
316 --or: goto((x, y)) # a pair (tuple) of coordinates
317 --or: goto(vec) # e.g. as returned by pos()
318
319 Move turtle to an absolute position. If the pen is down,
320 draw line. Do not change the turtle's orientation.
321
322 Example (for a Turtle instance named turtle)::
323 >>> tp = turtle.pos()
324 >>> tp
325 (0.00, 0.00)
326 >>> turtle.setpos(60,30)
327 >>> turtle.pos()
328 (60.00,30.00)
329 >>> turtle.setpos((20,80))
330 >>> turtle.pos()
331 (20.00,80.00)
332 >>> turtle.setpos(tp)
333 >>> turtle.pos()
334 (0.00,0.00)
335
336
337 .. method:: setx(x)
338 x -- a number (integer or float)
339
340 Set the turtle's first coordinate to x, leave second coordinate
341 unchanged.
342
343 Example (for a Turtle instance named turtle)::
344 >>> turtle.position()
345 (0.00, 240.00)
346 >>> turtle.setx(10)
347 >>> turtle.position()
348 (10.00, 240.00)
349
350
351 .. method:: sety(y)
352 y -- a number (integer or float)
353
354 Set the turtle's first coordinate to x, leave second coordinate
355 unchanged.
356
357 Example (for a Turtle instance named turtle)::
358 >>> turtle.position()
359 (0.00, 40.00)
360 >>> turtle.sety(-10)
361 >>> turtle.position()
362 (0.00, -10.00)
363
364
365 .. method:: setheading(to_angle)
366 .. method:: seth(to_angle)
367 to_angle -- a number (integer or float)
368
369 Set the orientation of the turtle to to_angle.
370 Here are some common directions in degrees:
371
372 =================== ====================
373 standard - mode logo-mode
374 =================== ====================
375 0 - east 0 - north
376 90 - north 90 - east
377 180 - west 180 - south
378 270 - south 270 - west
379 =================== ====================
380
381 Example (for a Turtle instance named turtle)::
382 >>> turtle.setheading(90)
383 >>> turtle.heading()
384 90
385
386
387 .. method:: home():
388 Move turtle to the origin - coordinates (0,0) and set it's
389 heading to it's start-orientation (which depends on mode).
390
391 Example (for a Turtle instance named turtle)::
392 >>> turtle.home()
393
394
395 .. method:: circle(radius, extent=None, steps=None)
396 radius -- a number
397 extent (optional) -- a number
398 steps (optional) -- an integer
399
400 Draw a circle with given radius. The center is radius units left
401 of the turtle; extent - an angle - determines which part of the
402 circle is drawn. If extent is not given, draw the entire circle.
403 If extent is not a full circle, one endpoint of the arc is the
404 current pen position. Draw the arc in counterclockwise direction
405 if radius is positive, otherwise in clockwise direction. Finally
406 the direction of the turtle is changed by the amount of extent.
407
408 As the circle is approximated by an inscribed regular polygon,
409 steps determines the number of steps to use. If not given,
410 it will be calculated automatically. Maybe used to draw regular
411 polygons.
412
413 call: circle(radius) # full circle
414 --or: circle(radius, extent) # arc
415 --or: circle(radius, extent, steps)
416 --or: circle(radius, steps=6) # 6-sided polygon
417
418 Example (for a Turtle instance named turtle)::
419 >>> turtle.circle(50)
420 >>> turtle.circle(120, 180) # semicircle
421
422
423 .. method:: dot(size=None, *color)
424 size -- an integer >= 1 (if given)
425 color -- a colorstring or a numeric color tuple
426
427 Draw a circular dot with diameter size, using color. If size
428 is not given, the maximum of pensize+4 and 2*pensize is used.
429
430 Example (for a Turtle instance named turtle)::
431 >>> turtle.dot()
432 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
433
434
435 .. method:: stamp():
436 Stamp a copy of the turtle shape onto the canvas at the current
437 turtle position. Return a stamp_id for that stamp, which can be
438 used to delete it by calling clearstamp(stamp_id).
439
440 Example (for a Turtle instance named turtle)::
441 >>> turtle.color("blue")
442 >>> turtle.stamp()
443 13
444 >>> turtle.fd(50)
445
446
447 .. method:: clearstamp(stampid):
448 stampid - an integer, must be return value of previous stamp() call.
449
450 Delete stamp with given stampid
451
452 Example (for a Turtle instance named turtle)::
453 >>> turtle.color("blue")
454 >>> astamp = turtle.stamp()
455 >>> turtle.fd(50)
456 >>> turtle.clearstamp(astamp)
457
458
459 .. method:: clearstamps(n=None):
460 n -- an integer
461
462 Delete all or first/last n of turtle's stamps.
463 If n is None, delete all of pen's stamps,
464 else if n > 0 delete first n stamps
465 else if n < 0 delete last n stamps.
466
467 Example (for a Turtle instance named turtle)::
468 >>> for i in range(8):
469 ... turtle.stamp(); turtle.fd(30)
470 >>> turtle.clearstamps(2)
471 >>> turtle.clearstamps(-2)
472 >>> turtle.clearstamps()
473
474
475 .. method:: undo():
476 undo (repeatedly) the last turtle action(s). Number of available
477 undo actions is determined by the size of the undobuffer.
478
479 Example (for a Turtle instance named turtle)::
480 >>> for i in range(4):
481 turtle.fd(50); turtle.lt(80)
482
483 >>> for i in range(8):
484 turtle.undo()
485
486
487 .. method:: speed(speed=None):
488 speed -- an integer in the range 0..10 or a speedstring (see below)
489
490 Set the turtle's speed to an integer value in the range 0 .. 10.
491 If no argument is given: return current speed.
492
493 If input is a number greater than 10 or smaller than 0.5,
494 speed is set to 0.
495 Speedstrings are mapped to speedvalues as follows:
496
497 * 'fastest' : 0
498 * 'fast' : 10
499 * 'normal' : 6
500 * 'slow' : 3
501 * 'slowest' : 1
502
503 speeds from 1 to 10 enforce increasingly faster animation of
504 line drawing and turtle turning.
505
506 Attention:
507 speed = 0 : *no* animation takes place. forward/back makes turtle jump
508 and likewise left/right make the turtle turn instantly.
509
510 Example (for a Turtle instance named turtle)::
511 >>> turtle.speed(3)
512
513
514TELL TURTLE'S STATE
515-------------------
516
517
518 .. method:: position()
519 .. method:: pos()
520 Return the turtle's current location (x,y) (as a Vec2D-vector)
521
522 Example (for a Turtle instance named turtle)::
523 >>> turtle.pos()
524 (0.00, 240.00)
525
526
527 .. method:: towards(x, y=None)
528 x -- a number or a pair/vector of numbers or a turtle instance
529 y -- a number None None
530
531 call: distance(x, y) # two coordinates
532 --or: distance((x, y)) # a pair (tuple) of coordinates
533 --or: distance(vec) # e.g. as returned by pos()
534 --or: distance(mypen) # where mypen is another turtle
535
536 Return the angle, between the line from turtle-position to position
537 specified by x, y and the turtle's start orientation. (Depends on
538 modes - "standard"/"world" or "logo")
539
540 Example (for a Turtle instance named turtle)::
541 >>> turtle.pos()
542 (10.00, 10.00)
543 >>> turtle.towards(0,0)
544 225.0
545
546
547 .. method:: xcor()
548 Return the turtle's x coordinate
549
550 Example (for a Turtle instance named turtle)::
551 >>> reset()
552 >>> turtle.left(60)
553 >>> turtle.forward(100)
554 >>> print turtle.xcor()
555 50.0
556
557
558 .. method:: ycor()
559 Return the turtle's y coordinate
560
561 Example (for a Turtle instance named turtle)::
562 >>> reset()
563 >>> turtle.left(60)
564 >>> turtle.forward(100)
565 >>> print turtle.ycor()
566 86.6025403784
567
568
569 .. method:: heading()
570 Return the turtle's current heading (value depends on mode).
571
572 Example (for a Turtle instance named turtle)::
573 >>> turtle.left(67)
574 >>> turtle.heading()
575 67.0
576
577
578 .. method:: distance(x, y=None)
579 x -- a number or a pair/vector of numbers or a turtle instance
580 y -- a number None None
581
582 call: distance(x, y) # two coordinates
583 --or: distance((x, y)) # a pair (tuple) of coordinates
584 --or: distance(vec) # e.g. as returned by pos()
585 --or: distance(mypen) # where mypen is another turtle
586
587 Return the distance from the turtle to (x,y) in turtle step units.
588
589 Example (for a Turtle instance named turtle)::
590 >>> turtle.pos()
591 (0.00, 0.00)
592 >>> turtle.distance(30,40)
593 50.0
594 >>> joe = Turtle()
595 >>> joe.forward(77)
596 >>> turtle.distance(joe)
597 77.0
598
599
600SETTINGS FOR MEASUREMENT
601
602
603 .. method:: degrees(fullcircle=360.0)
604 fullcircle - a number
605
606 Set angle measurement units, i. e. set number
607 of 'degrees' for a full circle. Dafault value is
608 360 degrees.
609
610 Example (for a Turtle instance named turtle)::
611 >>> turtle.left(90)
612 >>> turtle.heading()
613 90
614 >>> turtle.degrees(400.0) # angle measurement in gon
615 >>> turtle.heading()
616 100
617
618
619 .. method:: radians()
620 Set the angle measurement units to radians.
621
622 Example (for a Turtle instance named turtle)::
623 >>> turtle.heading()
624 90
625 >>> turtle.radians()
626 >>> turtle.heading()
627 1.5707963267948966
628
629
630(II) PEN CONTROL:
631-----------------
632
633DRAWING STATE
634
635
636 .. method:: pendown()
637 .. method:: pd()
638 .. method:: down()
639 Pull the pen down -- drawing when moving.
640
641 Example (for a Turtle instance named turtle)::
642 >>> turtle.pendown()
643
644
645 .. method:: penup()
646 .. method:: pu()
647 .. method:: up()
648 Pull the pen up -- no drawing when moving.
649
650 Example (for a Turtle instance named turtle)::
651 >>> turtle.penup()
652
653
654 .. method:: pensize(width=None)
655 .. method:: width(width=None)
656 width -- positive number
657
658 Set the line thickness to width or return it. If resizemode is set
659 to "auto" and turtleshape is a polygon, that polygon is drawn with
660 the same line thickness. If no argument is given, the current pensize
661 is returned.
662
663 Example (for a Turtle instance named turtle)::
664 >>> turtle.pensize()
665 1
666 turtle.pensize(10) # from here on lines of width 10 are drawn
667
668
669 .. method:: pen(pen=None, **pendict)
670 pen -- a dictionary with some or all of the below listed keys.
671 **pendict -- one or more keyword-arguments with the below
672 listed keys as keywords.
673
674 Return or set the pen's attributes in a 'pen-dictionary'
675 with the following key/value pairs:
676
677 * "shown" : True/False
678 * "pendown" : True/False
679 * "pencolor" : color-string or color-tuple
680 * "fillcolor" : color-string or color-tuple
681 * "pensize" : positive number
682 * "speed" : number in range 0..10
683 * "resizemode" : "auto" or "user" or "noresize"
684 * "stretchfactor": (positive number, positive number)
685 * "outline" : positive number
686 * "tilt" : number
687
688 This dicionary can be used as argument for a subsequent
689 pen()-call to restore the former pen-state. Moreover one
690 or more of these attributes can be provided as keyword-arguments.
691 This can be used to set several pen attributes in one statement.
692
693 Examples (for a Turtle instance named turtle)::
694 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
695 >>> turtle.pen()
696 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
697 'pencolor': 'red', 'pendown': True, 'fillcolor': 'black',
698 'stretchfactor': (1,1), 'speed': 3}
699 >>> penstate=turtle.pen()
700 >>> turtle.color("yellow","")
701 >>> turtle.penup()
702 >>> turtle.pen()
703 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
704 'pencolor': 'yellow', 'pendown': False, 'fillcolor': '',
705 'stretchfactor': (1,1), 'speed': 3}
706 >>> p.pen(penstate, fillcolor="green")
707 >>> p.pen()
708 {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
709 'pencolor': 'red', 'pendown': True, 'fillcolor': 'green',
710 'stretchfactor': (1,1), 'speed': 3}
711
712
713 .. method:: isdown(self):
714 Return True if pen is down, False if it's up.
715
716 Example (for a Turtle instance named turtle)::
717 >>> turtle.penup()
718 >>> turtle.isdown()
719 False
720 >>> turtle.pendown()
721 >>> turtle.isdown()
722 True
723
724
725COLOR CONTROL
726
727
728 .. method:: color(*args)
729 Return or set pencolor and fillcolor.
730
731 Several input formats are allowed. They use 0, 1, 2, or 3 arguments
732 as follows:
733
734 - color()
735 Return the current pencolor and the current fillcolor
736 as a pair of color specification strings as are returned
737 by pencolor and fillcolor.
738 - color(colorstring), color((r,g,b)), color(r,g,b)
739 inputs as in pencolor, set both, fillcolor and pencolor,
740 to the given value.
741 - color(colorstring1, colorstring2),
742 - color((r1,g1,b1), (r2,g2,b2))
743 equivalent to pencolor(colorstring1) and fillcolor(colorstring2)
744 and analogously, if the other input format is used.
745
746 If turtleshape is a polygon, outline and interior of that polygon
747 is drawn with the newly set colors.
748 For more info see: pencolor, fillcolor
749
750 Example (for a Turtle instance named turtle)::
751 >>> turtle.color('red', 'green')
752 >>> turtle.color()
753 ('red', 'green')
754 >>> colormode(255)
755 >>> color((40, 80, 120), (160, 200, 240))
756 >>> color()
757 ('#285078', '#a0c8f0')
758
759
760 .. method:: pencolor(*args)
761 Return or set the pencolor.
762
763 Four input formats are allowed:
764
765 - pencolor()
766 Return the current pencolor as color specification string,
767 possibly in hex-number format (see example).
768 May be used as input to another color/pencolor/fillcolor call.
769 - pencolor(colorstring)
770 s is a Tk color specification string, such as "red" or "yellow"
771 - pencolor((r, g, b))
772 *a tuple* of r, g, and b, which represent, an RGB color,
773 and each of r, g, and b are in the range 0..colormode,
774 where colormode is either 1.0 or 255
775 - pencolor(r, g, b)
776 r, g, and b represent an RGB color, and each of r, g, and b
777 are in the range 0..colormode
778
779 If turtleshape is a polygon, the outline of that polygon is drawn
780 with the newly set pencolor.
781
782 Example (for a Turtle instance named turtle)::
783 >>> turtle.pencolor('brown')
784 >>> tup = (0.2, 0.8, 0.55)
785 >>> turtle.pencolor(tup)
786 >>> turtle.pencolor()
787 '#33cc8c'
788
789
790 .. method:: fillcolor(*args)
791 """ Return or set the fillcolor.
792
793 Four input formats are allowed:
794
795 - fillcolor()
796 Return the current fillcolor as color specification string,
797 possibly in hex-number format (see example).
798 May be used as input to another color/pencolor/fillcolor call.
799 - fillcolor(colorstring)
800 s is a Tk color specification string, such as "red" or "yellow"
801 - fillcolor((r, g, b))
802 *a tuple* of r, g, and b, which represent, an RGB color,
803 and each of r, g, and b are in the range 0..colormode,
804 where colormode is either 1.0 or 255
805 - fillcolor(r, g, b)
806 r, g, and b represent an RGB color, and each of r, g, and b
807 are in the range 0..colormode
808
809 If turtleshape is a polygon, the interior of that polygon is drawn
810 with the newly set fillcolor.
811
812 Example (for a Turtle instance named turtle)::
813 >>> turtle.fillcolor('violet')
814 >>> col = turtle.pencolor()
815 >>> turtle.fillcolor(col)
816 >>> turtle.fillcolor(0, .5, 0)
817
818
819 See also: Screen method colormode()
820
821
822FILLING
823
824
825 .. method:: fill(flag)
826 flag -- True/False (or 1/0 respectively)
827
828 Call fill(True) before drawing the shape you want to fill,
829 and fill(False) when done. When used without argument: return
830 fillstate (True if filling, False else).
831
832 Example (for a Turtle instance named turtle)::
833 >>> turtle.fill(True)
834 >>> for _ in range(3):
835 ... turtle.forward(100)
836 ... turtle.left(120)
837 ...
838 >>> turtle.fill(False)
839
840
841 .. method:: begin_fill()
842 Called just before drawing a shape to be filled.
843
844 Example (for a Turtle instance named turtle)::
845 >>> turtle.color("black", "red")
846 >>> turtle.begin_fill()
847 >>> turtle.circle(60)
848 >>> turtle.end_fill()
849
850
851 .. method:: end_fill()
852 Fill the shape drawn after the call begin_fill().
853
854 Example: See begin_fill()
855
856
857MORE DRAWING CONTROL
858
859
860 .. method:: reset()
861 Delete the turtle's drawings from the screen, re-center the turtle
862 and set variables to the default values.
863
864 Example (for a Turtle instance named turtle)::
865 >>> turtle.position()
866 (0.00,-22.00)
867 >>> turtle.heading()
868 100.0
869 >>> turtle.reset()
870 >>> turtle.position()
871 (0.00,0.00)
872 >>> turtle.heading()
873 0.0
874
875
876 .. method:: clear()
877 Delete the turtle's drawings from the screen. Do not move turtle.
878 State and position of the turtle as well as drawings of other
879 turtles are not affected.
880
881 Examples (for a Turtle instance named turtle):
882 >>> turtle.clear()
883
884
885 .. method:: write(arg, move=False, align='left', font=('Arial', 8, 'normal'))
886 arg -- info, which is to be written to the TurtleScreen
887 move (optional) -- True/False
888 align (optional) -- one of the strings "left", "center" or right"
889 font (optional) -- a triple (fontname, fontsize, fonttype)
890
891 Write text - the string representation of arg - at the current
892 turtle position according to align ("left", "center" or right")
893 and with the given font.
894 If move is True, the pen is moved to the bottom-right corner
895 of the text. By default, move is False.
896
897 Example (for a Turtle instance named turtle)::
898 >>> turtle.write('Home = ', True, align="center")
899 >>> turtle.write((0,0), True)
900
901
902TURTLE STATE:
903-------------
904
905VISIBILITY
906
907
908 .. method:: showturtle()
909 .. method:: st()
910 Makes the turtle visible.
911
912 Example (for a Turtle instance named turtle)::
913 >>> turtle.hideturtle()
914 >>> turtle.showturtle()
915
916
917 .. method:: hideturtle()
918 .. method:: ht()
919 Makes the turtle invisible.
920 It's a good idea to do this while you're in the middle of
921 doing some complex drawing, because hiding the turtle speeds
922 up the drawing observably.
923
924 Example (for a Turtle instance named turtle)::
925 >>> turtle.hideturtle()
926
927
928 .. method:: isvisible(self):
929 Return True if the Turtle is shown, False if it's hidden.
930
931 Example (for a Turtle instance named turtle)::
932 >>> turtle.hideturtle()
933 >>> print turtle.isvisible():
934 False
935
936
937APPEARANCE
938
939
940 .. method:: shape(name=None)
941 name -- a string, which is a valid shapename
942
943 Set turtle shape to shape with given name or, if name is not given,
944 return name of current shape.
945 Shape with name must exist in the TurtleScreen's shape dictionary.
946 Initially there are the following polygon shapes:
947 'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'.
948 To learn about how to deal with shapes see Screen-method register_shape.
949
950 Example (for a Turtle instance named turtle)::
951 >>> turtle.shape()
952 'arrow'
953 >>> turtle.shape("turtle")
954 >>> turtle.shape()
955 'turtle'
956
957
958 .. method:: resizemode(rmode=None)
959 rmode -- one of the strings "auto", "user", "noresize"
960
961 Set resizemode to one of the values: "auto", "user", "noresize".
962 If rmode is not given, return current resizemode.
963 Different resizemodes have the following effects:
964
965 - "auto" adapts the appearance of the turtle
966 corresponding to the value of pensize.
967 - "user" adapts the appearance of the turtle according to the
968 values of stretchfactor and outlinewidth (outline),
969 which are set by shapesize()
970 - "noresize" no adaption of the turtle's appearance takes place.
971
972 resizemode("user") is called by a shapesize when used with arguments.
973
974 Examples (for a Turtle instance named turtle)::
975 >>> turtle.resizemode("noresize")
976 >>> turtle.resizemode()
977 'noresize'
978
979
980 .. method:: shapesize(stretch_wid=None, stretch_len=None, outline=None):
981 stretch_wid -- positive number
982 stretch_len -- positive number
983 outline -- positive number
984
985 Return or set the pen's attributes x/y-stretchfactors and/or outline.
986 Set resizemode to "user".
987 If and only if resizemode is set to "user", the turtle will be
988 displayed stretched according to its stretchfactors:
989 stretch_wid is stretchfactor perpendicular to it's orientation,
990 stretch_len is stretchfactor in direction of it's orientation,
991 outline determines the width of the shapes's outline.
992
993 Examples (for a Turtle instance named turtle)::
994 >>> turtle.resizemode("user")
995 >>> turtle.shapesize(5, 5, 12)
996 >>> turtle.shapesize(outline=8)
997
998
999 .. method:: tilt(angle)
1000 angle - a number
1001
1002 Rotate the turtleshape by angle from its current tilt-angle,
1003 but do NOT change the turtle's heading (direction of movement).
1004
1005 Examples (for a Turtle instance named turtle)::
1006 >>> turtle.shape("circle")
1007 >>> turtle.shapesize(5,2)
1008 >>> turtle.tilt(30)
1009 >>> turtle.fd(50)
1010 >>> turtle.tilt(30)
1011 >>> turtle.fd(50)
1012
1013
1014 .. method:: settiltangle(angle)
1015 angle -- number
1016
1017 Rotate the turtleshape to point in the direction specified by angle,
1018 regardless of its current tilt-angle. DO NOT change the turtle's
1019 heading (direction of movement).
1020
1021 Examples (for a Turtle instance named turtle)::
1022 >>> turtle.shape("circle")
1023 >>> turtle.shapesize(5,2)
1024 >>> turtle.settiltangle(45)
1025 >>> stamp()
1026 >>> turtle.fd(50)
1027 >>> turtle.settiltangle(-45)
1028 >>> stamp()
1029 >>> turtle.fd(50)
1030
1031
1032 .. method:: tiltangle()
1033 Return the current tilt-angle, i. e. the angle between the
1034 orientation of the turtleshape and the heading of the turtle
1035 (it's direction of movement).
1036
1037 Examples (for a Turtle instance named turtle)::
1038 >>> turtle.shape("circle")
1039 >>> turtle.shapesize(5,2)
1040 >>> turtle.tilt(45)
1041 >>> turtle.tiltangle()
1042 45
1043
1044
1045IV. USING EVENTS
1046----------------
1047
1048
1049 .. method:: onclick(fun, btn=1, add=None)
1050 fun -- a function with two arguments, to which will be assigned
1051 the coordinates of the clicked point on the canvas.
1052 num -- number of the mouse-button defaults to 1 (left mouse button).
1053 add -- True or False. If True, new binding will be added, otherwise
1054 it will replace a former binding.
1055
1056 Bind fun to mouse-click event on this turtle on canvas.
1057 If fun is None, existing bindings are removed.
1058 Example for the anonymous turtle, i. e. the procedural way::
1059
1060 >>> def turn(x, y):
1061 left(360)
1062
1063 >>> onclick(turn) # Now clicking into the turtle will turn it.
1064 >>> onclick(None) # event-binding will be removed
1065
1066
1067 .. method:: onrelease(fun, btn=1, add=None):
1068 """
1069 Arguments:
1070 fun -- a function with two arguments, to which will be assigned
1071 the coordinates of the clicked point on the canvas.
1072 num -- number of the mouse-button defaults to 1 (left mouse button).
1073 add -- True or False. If True, new binding will be added, otherwise
1074 it will replace a former binding.
1075
1076 Bind fun to mouse-button-release event on this turtle on canvas.
1077 If fun is None, existing bindings are removed.
1078
1079 Example (for a MyTurtle instance named turtle):
1080 >>> class MyTurtle(Turtle):
1081 ... def glow(self,x,y):
1082 ... self.fillcolor("red")
1083 ... def unglow(self,x,y):
1084 ... self.fillcolor("")
1085 ...
1086 >>> turtle = MyTurtle()
1087 >>> turtle.onclick(turtle.glow)
1088 >>> turtle.onrelease(turtle.unglow)
1089 ### clicking on turtle turns fillcolor red,
1090 ### unclicking turns it to transparent.
1091
1092
1093 .. method:: ondrag(fun, btn=1, add=None):
1094 fun -- a function with two arguments, to which will be assigned
1095 the coordinates of the clicked point on the canvas.
1096 num -- number of the mouse-button defaults to 1 (left mouse button).
1097 add -- True or False. If True, new binding will be added, otherwise
1098 it will replace a former binding.
1099
1100 Bind fun to mouse-move event on this turtle on canvas.
1101 If fun is None, existing bindings are removed.
1102
1103 Remark: Every sequence of mouse-move-events on a turtle is preceded
1104 by a mouse-click event on that turtle.
1105 If fun is None, existing bindings are removed.
1106
1107 Example (for a Turtle instance named turtle):
1108 >>> turtle.ondrag(turtle.goto)
1109 ### Subsequently clicking and dragging a Turtle will move it across
1110 ### the screen thereby producing handdrawings (if pen is down).
1111
1112
1113V. SPECIAL TURTLE METHODS
1114--------------------------
1115
1116
1117 .. method:: begin_poly():
1118 Start recording the vertices of a polygon. Current turtle position
1119 is first vertex of polygon.
1120
1121 Example (for a Turtle instance named turtle):
1122 >>> turtle.begin_poly()
1123
1124
1125 .. method:: end_poly():
1126 Stop recording the vertices of a polygon. Current turtle position is
1127 last vertex of polygon. This will be connected with the first vertex.
1128
1129 Example (for a Turtle instance named turtle):
1130 >>> turtle.end_poly()
1131
1132
1133 .. method:: get_poly():
1134 Return the lastly recorded polygon.
1135
1136 Example (for a Turtle instance named turtle):
1137 >>> p = turtle.get_poly()
1138 >>> turtle.register_shape("myFavouriteShape", p)
1139
1140
1141 .. method:: clone():
1142 Create and return a clone of the turtle with same position, heading
1143 and turtle properties.
1144
1145 Example (for a Turtle instance named mick):
1146 mick = Turtle()
1147 joe = mick.clone()
1148
1149
1150 .. method:: getturtle():
1151 Return the Turtleobject itself.
1152 Only reasonable use: as a function to return the 'anonymous turtle':
1153
1154 Example:
1155 >>> pet = getturtle()
1156 >>> pet.fd(50)
1157 >>> pet
1158 <turtle.Turtle object at 0x01417350>
1159 >>> turtles()
1160 [<turtle.Turtle object at 0x01417350>]
1161
1162
1163 .. method:: getscreen():
1164 Return the TurtleScreen object, the turtle is drawing on.
1165 So TurtleScreen-methods can be called for that object.
1166
1167 Example (for a Turtle instance named turtle):
1168 >>> ts = turtle.getscreen()
1169 >>> ts
1170 <turtle.Screen object at 0x01417710>
1171 >>> ts.bgcolor("pink")
1172
1173
1174 .. method:: def setundobuffer(size):
1175 size -- an integer or None
1176
1177 Set or disable undobuffer.
1178 If size is an integer an empty undobuffer of given size is installed.
1179 Size gives the maximum number of turtle-actions that can be undone
1180 by the undo() method/function.
1181 If size is None, no undobuffer is present.
1182
1183 Example (for a Turtle instance named turtle):
1184 >>> turtle.setundobuffer(42)
1185
1186
1187 .. method:: undobufferentries():
1188 """Return count of entries in the undobuffer.
1189
1190 Example (for a Turtle instance named turtle):
1191 >>> while undobufferentries():
1192 ... undo()
1193
1194
1195 .. method:: tracer(flag=None, delay=None)
1196 A replica of the corresponding TurtleScreen-method
1197 *Deprecated since Python 2.6* (as RawTurtle method)
1198
Georg Brandl8ec7f652007-08-15 14:28:01 +00001199
Martin v. Löwis87184592008-06-04 06:29:55 +00001200 .. method:: window_width()
1201 .. method:: window_height()
1202 Both are replicas of the corresponding TurtleScreen-methods
1203 *Deprecated since Python 2.6* (as RawTurtle methods)
1204
Georg Brandl8ec7f652007-08-15 14:28:01 +00001205
Martin v. Löwis87184592008-06-04 06:29:55 +00001206EXCURSUS ABOUT THE USE OF COMPOUND SHAPES
1207-----------------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001208
Martin v. Löwis87184592008-06-04 06:29:55 +00001209To use compound turtle shapes, which consist of several polygons
1210of different color, you must use the helper class Shape
1211explicitely as described below:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001212
Martin v. Löwis87184592008-06-04 06:29:55 +00001213 1. Create an empty Shape object of type compound
1214 2. Add as many components to this object as desired,
1215 using the addcomponent() method:
1216
1217 .. method:: addcomponent(self, poly, fill, outline=None)
1218 poly -- a polygon
1219 fill -- a color, the poly will be filled with
1220 outline -- a color for the poly's outline (if given)
1221
1222So it goes like this::
Georg Brandl8ec7f652007-08-15 14:28:01 +00001223
Martin v. Löwis87184592008-06-04 06:29:55 +00001224 >>> s = Shape("compound")
1225 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1226 >>> s.addcomponent(poly1, "red", "blue")
1227 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1228 >>> s.addcomponent(poly2, "blue", "red")
Georg Brandl8ec7f652007-08-15 14:28:01 +00001229
Martin v. Löwis87184592008-06-04 06:29:55 +00001230Now add Shape s to the Screen's shapelist ...
1231.. and use it::
Georg Brandl8ec7f652007-08-15 14:28:01 +00001232
Martin v. Löwis87184592008-06-04 06:29:55 +00001233 >>> register_shape("myshape", s)
1234 >>> shape("myshape")
1235
Georg Brandl8ec7f652007-08-15 14:28:01 +00001236
Martin v. Löwis87184592008-06-04 06:29:55 +00001237NOTE 1: addcomponent() is a method of class Shape (not of
1238Turtle nor Screen) and thus there is NO FUNCTION of the same name.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001239
Martin v. Löwis87184592008-06-04 06:29:55 +00001240NOTE 2: class Shape is used internally by the register_shape method
1241in different ways.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001242
Martin v. Löwis87184592008-06-04 06:29:55 +00001243The application programmer has to deal with the Shape class
1244ONLY when using compound shapes like shown above!
Georg Brandl8ec7f652007-08-15 14:28:01 +00001245
Martin v. Löwis87184592008-06-04 06:29:55 +00001246NOTE 3: A short description of the class Shape is in section 4.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001247
Martin v. Löwis87184592008-06-04 06:29:55 +00001248
1249
12503. METHODS OF TurtleScreen/Screen AND CORRESPONDING FUNCTIONS
1251=============================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00001252
1253
Martin v. Löwis87184592008-06-04 06:29:55 +00001254WINDOW CONTROL
1255--------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001256
Georg Brandl8ec7f652007-08-15 14:28:01 +00001257
Martin v. Löwis87184592008-06-04 06:29:55 +00001258 .. method:: bgcolor(*args)
1259 args -- a color string or three numbers in the range 0..colormode
1260 or a 3-tuple of such numbers.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001261
Martin v. Löwis87184592008-06-04 06:29:55 +00001262 Set or return backgroundcolor of the TurtleScreen.
1263
1264 Example (for a TurtleScreen instance named screen):
1265 >>> screen.bgcolor("orange")
1266 >>> screen.bgcolor()
1267 'orange'
1268 >>> screen.bgcolor(0.5,0,0.5)
1269 >>> screen.bgcolor()
1270 '#800080'
Georg Brandl8ec7f652007-08-15 14:28:01 +00001271
Georg Brandl8ec7f652007-08-15 14:28:01 +00001272
Martin v. Löwis87184592008-06-04 06:29:55 +00001273 .. method:: bgpic(picname=None)
1274 picname -- a string, name of a gif-file or "nopic".
1275
1276 Set background image or return name of current backgroundimage.
1277 If picname is a filename, set the corresponing image as background.
1278 If picname is "nopic", delete backgroundimage, if present.
1279 If picname is None, return the filename of the current backgroundimage.
1280
1281 Example (for a TurtleScreen instance named screen):
1282 >>> screen.bgpic()
1283 'nopic'
1284 >>> screen.bgpic("landscape.gif")
1285 >>> screen.bgpic()
1286 'landscape.gif'
Georg Brandl8ec7f652007-08-15 14:28:01 +00001287
Georg Brandl8ec7f652007-08-15 14:28:01 +00001288
Martin v. Löwis87184592008-06-04 06:29:55 +00001289 .. method:: clear()
1290 .. method:: clearscreen()
1291 Delete all drawings and all turtles from the TurtleScreen.
1292 Reset empty TurtleScreen to it's initial state: white background,
1293 no backgroundimage, no eventbindings and tracing on.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001294
Martin v. Löwis87184592008-06-04 06:29:55 +00001295 Example (for a TurtleScreen instance named screen):
1296 screen.clear()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001297
Martin v. Löwis87184592008-06-04 06:29:55 +00001298 *Note*: this method is only available as the function named
1299 clearscreen(). (The function clear() is another one derived from
1300 the Turtle-method clear()!).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001301
1302
Martin v. Löwis87184592008-06-04 06:29:55 +00001303 .. method:: reset()
1304 .. method:: resetscreen()
1305 Reset all Turtles on the Screen to their initial state.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001306
Martin v. Löwis87184592008-06-04 06:29:55 +00001307 Example (for a TurtleScreen instance named screen):
1308 >>> screen.reset()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001309
Martin v. Löwis87184592008-06-04 06:29:55 +00001310 *Note*: this method is pnly available as the function named
1311 resetscreen(). (The function reset() is another one derived from
1312 the Turtle-method reset()!).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001313
Georg Brandl8ec7f652007-08-15 14:28:01 +00001314
Martin v. Löwis87184592008-06-04 06:29:55 +00001315 .. method:: screensize(canvwidth=None, canvheight=None, bg=None):
1316 canvwidth -- positive integer, new width of canvas in pixels
1317 canvheight -- positive integer, new height of canvas in pixels
1318 bg -- colorstring or color-tupel, new backgroundcolor
1319
1320 If no arguments are given, return current (canvaswidth, canvasheight)
1321 Resize the canvas, the turtles are drawing on.
1322 Do not alter the drawing window. To observe hidden parts of
1323 the canvas use the scrollbars. (So one can make visible those
1324 parts of a drawing, which were outside the canvas before!)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001325
Martin v. Löwis87184592008-06-04 06:29:55 +00001326 Example (for a Turtle instance named turtle):
1327 >>> turtle.screensize(2000,1500)
1328 ### e. g. to search for an erroneously escaped turtle ;-)
1329
1330
1331 .. method:: setworldcoordinates(llx, lly, urx, ury):
1332 llx -- a number, x-coordinate of lower left corner of canvas
1333 lly -- a number, y-coordinate of lower left corner of canvas
1334 urx -- a number, x-coordinate of upper right corner of canvas
1335 ury -- a number, y-coordinate of upper right corner of canvas
Georg Brandl8ec7f652007-08-15 14:28:01 +00001336
Martin v. Löwis87184592008-06-04 06:29:55 +00001337 Set up user coodinate-system and switch to mode 'world' if necessary.
1338 This performs a screen.reset. If mode 'world' is already active,
1339 all drawings are redrawn according to the new coordinates.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001340
Martin v. Löwis87184592008-06-04 06:29:55 +00001341 But *ATTENTION*: in user-defined coordinatesystems angles may appear
1342 distorted. (see Screen.mode())
Georg Brandl8ec7f652007-08-15 14:28:01 +00001343
Martin v. Löwis87184592008-06-04 06:29:55 +00001344 Example (for a TurtleScreen instance named screen):
1345 >>> screen.reset()
1346 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1347 >>> for _ in range(72):
1348 ... left(10)
1349 ...
1350 >>> for _ in range(8):
1351 ... left(45); fd(2) # a regular octogon
1352
1353
1354ANIMATION CONTROL
1355-----------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001356
Georg Brandl8ec7f652007-08-15 14:28:01 +00001357
Martin v. Löwis87184592008-06-04 06:29:55 +00001358 .. method:: delay(delay=None):
1359 delay -- positive integer
1360
1361 Set or return the drawing delay in milliseconds. (This is sort of
1362 time interval between two consecutived canvas updates.) The longer
1363 the drawing delay, the slower the animation.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001364
Martin v. Löwis87184592008-06-04 06:29:55 +00001365 Optional argument:
1366 Example (for a TurtleScreen instance named screen)::
Georg Brandl8ec7f652007-08-15 14:28:01 +00001367
Martin v. Löwis87184592008-06-04 06:29:55 +00001368 >>> screen.delay(15)
1369 >>> screen.delay()
1370 15
Georg Brandl8ec7f652007-08-15 14:28:01 +00001371
Georg Brandl8ec7f652007-08-15 14:28:01 +00001372
Martin v. Löwis87184592008-06-04 06:29:55 +00001373 .. method:: tracer(n=None, delay=None):
1374 n -- nonnegative integer
1375 delay -- nonnegative integer
1376
1377 Turn turtle animation on/off and set delay for update drawings.
1378 If n is given, only each n-th regular screen update is really performed.
1379 (Can be used to accelerate the drawing of complex graphics.)
1380 Second argument sets delay value (see delay())
Georg Brandl8ec7f652007-08-15 14:28:01 +00001381
Martin v. Löwis87184592008-06-04 06:29:55 +00001382 Example (for a TurtleScreen instance named screen):
1383 >>> screen.tracer(8, 25)
1384 >>> dist = 2
1385 >>> for i in range(200):
1386 ... fd(dist)
1387 ... rt(90)
1388 ... dist += 2
1389
1390
1391 .. method:: update():
1392 Perform a TurtleScreen update. To be used, when tracer is turned
1393 off.
1394
1395 See also RawTurtle/Turtle - method speed()
1396
Georg Brandl8ec7f652007-08-15 14:28:01 +00001397
Martin v. Löwis87184592008-06-04 06:29:55 +00001398USING SCREEN EVENTS
1399-------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001400
Georg Brandl8ec7f652007-08-15 14:28:01 +00001401
Martin v. Löwis87184592008-06-04 06:29:55 +00001402 .. method:: listen(xdummy=None, ydummy=None):
1403 """Set focus on TurtleScreen (in order to collect key-events)
1404 Dummy arguments are provided in order to be able to pass listen
1405 to the onclick method.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001406
Martin v. Löwis87184592008-06-04 06:29:55 +00001407 Example (for a TurtleScreen instance named screen):
1408 >>> screen.listen()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001409
Georg Brandl8ec7f652007-08-15 14:28:01 +00001410
Martin v. Löwis87184592008-06-04 06:29:55 +00001411 .. method:: onkey(fun, key):
1412 fun -- a function with no arguments or None
1413 key -- a string: key (e.g. "a") or key-symbol (e.g. "space")
Georg Brandl8ec7f652007-08-15 14:28:01 +00001414
Martin v. Löwis87184592008-06-04 06:29:55 +00001415 Bind fun to key-release event of key. If fun is None, event-bindings
1416 are removed.
1417 Remark: in order to be able to register key-events, TurtleScreen
1418 must have focus. (See method listen.)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001419
Martin v. Löwis87184592008-06-04 06:29:55 +00001420 Example (for a TurtleScreen instance named screen
1421 and a Turtle instance named turtle)::
Georg Brandl8ec7f652007-08-15 14:28:01 +00001422
Martin v. Löwis87184592008-06-04 06:29:55 +00001423 >>> def f():
1424 ... fd(50)
1425 ... lt(60)
1426 ...
1427 >>> screen.onkey(f, "Up")
1428 >>> screen.listen()
1429
1430
1431 .. method:: onclick(fun, btn=1, add=None):
1432 .. method:: onscreenclick(fun, btn=1, add=None):
1433 fun -- a function with two arguments, to which will be assigned
1434 the coordinates of the clicked point on the canvas - or None.
1435 num -- number of the mouse-button defaults to 1 (left mouse button).
1436 add -- True or False. If True, new binding will be added, otherwise
1437 it will replace a former binding.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001438
Martin v. Löwis87184592008-06-04 06:29:55 +00001439 Example (for a TurtleScreen instance named screen and a Turtle instance
1440 named turtle)::
Georg Brandl8ec7f652007-08-15 14:28:01 +00001441
Martin v. Löwis87184592008-06-04 06:29:55 +00001442 >>> screen.onclick(turtle.goto)
1443 ### Subsequently clicking into the TurtleScreen will
1444 ### make the turtle move to the clicked point.
1445 >>> screen.onclick(None)
1446
1447 ### event-binding will be removed
Georg Brandl8ec7f652007-08-15 14:28:01 +00001448
Martin v. Löwis87184592008-06-04 06:29:55 +00001449 *Note*: this method is only available as the function named
1450 onscreenclick(). (The function onclick() is a different one derived
1451 from the Turtle-method onclick()!).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001452
1453
Martin v. Löwis87184592008-06-04 06:29:55 +00001454 .. method:: ontimer(fun, t=0):
1455 fun -- a function with no arguments.
1456 t -- a number >= 0
Georg Brandl8ec7f652007-08-15 14:28:01 +00001457
Martin v. Löwis87184592008-06-04 06:29:55 +00001458 Install a timer, which calls fun after t milliseconds.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001459
Martin v. Löwis87184592008-06-04 06:29:55 +00001460 Example (for a TurtleScreen instance named screen):
Georg Brandl8ec7f652007-08-15 14:28:01 +00001461
Martin v. Löwis87184592008-06-04 06:29:55 +00001462 >>> running = True
1463 >>> def f():
1464 if running:
1465 fd(50)
1466 lt(60)
1467 screen.ontimer(f, 250)
1468 >>> f() ### makes the turtle marching around
1469 >>> running = False
Georg Brandl8ec7f652007-08-15 14:28:01 +00001470
Georg Brandl8ec7f652007-08-15 14:28:01 +00001471
Martin v. Löwis87184592008-06-04 06:29:55 +00001472SETTINGS AND SPECIAL METHODS
Georg Brandl8ec7f652007-08-15 14:28:01 +00001473
Georg Brandl8ec7f652007-08-15 14:28:01 +00001474
Martin v. Löwis87184592008-06-04 06:29:55 +00001475 .. method:: mode(mode=None):
1476 mode -- on of the strings 'standard', 'logo' or 'world'
1477
1478 Set turtle-mode ('standard', 'logo' or 'world') and perform reset.
1479 If mode is not given, current mode is returned.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001480
Martin v. Löwis87184592008-06-04 06:29:55 +00001481 Mode 'standard' is compatible with old turtle.py.
1482 Mode 'logo' is compatible with most Logo-Turtle-Graphics.
1483 Mode 'world' uses userdefined 'worldcoordinates'. *Attention*: in
1484 this mode angles appear distorted if x/y unit-ratio doesn't equal 1.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001485
Martin v. Löwis87184592008-06-04 06:29:55 +00001486 ============ ========================= ===================
1487 Mode Initial turtle heading positive angles
1488 ============ ========================= ===================
1489 'standard' to the right (east) counterclockwise
1490 'logo' upward (north) clockwise
1491 ============ ========================= ===================
Georg Brandl8ec7f652007-08-15 14:28:01 +00001492
Martin v. Löwis87184592008-06-04 06:29:55 +00001493 Examples::
1494 >>> mode('logo') # resets turtle heading to north
1495 >>> mode()
1496 'logo'
Georg Brandl8ec7f652007-08-15 14:28:01 +00001497
1498
Martin v. Löwis87184592008-06-04 06:29:55 +00001499 .. method:: colormode(cmode=None):
1500 cmode -- one of the values 1.0 or 255
Georg Brandl8ec7f652007-08-15 14:28:01 +00001501
Martin v. Löwis87184592008-06-04 06:29:55 +00001502 """Return the colormode or set it to 1.0 or 255.
1503 Subsequently r, g, b values of colortriples have to be in
1504 range 0..cmode.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001505
Martin v. Löwis87184592008-06-04 06:29:55 +00001506 Example (for a TurtleScreen instance named screen):
1507 >>> screen.colormode()
1508 1.0
1509 >>> screen.colormode(255)
1510 >>> turtle.pencolor(240,160,80)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001511
Georg Brandl8ec7f652007-08-15 14:28:01 +00001512
Martin v. Löwis87184592008-06-04 06:29:55 +00001513 .. method:: getcanvas():
1514 Return the Canvas of this TurtleScreen. Useful for insiders, who
1515 know what to do with a Tkinter-Canvas ;-)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001516
Martin v. Löwis87184592008-06-04 06:29:55 +00001517 Example (for a Screen instance named screen):
1518 >>> cv = screen.getcanvas()
1519 >>> cv
1520 <turtle.ScrolledCanvas instance at 0x010742D8>
Georg Brandl8ec7f652007-08-15 14:28:01 +00001521
Georg Brandl8ec7f652007-08-15 14:28:01 +00001522
Martin v. Löwis87184592008-06-04 06:29:55 +00001523 .. method:: getshapes():
1524 """Return a list of names of all currently available turtle shapes.
1525
1526 Example (for a TurtleScreen instance named screen):
1527 >>> screen.getshapes()
1528 ['arrow', 'blank', 'circle', ... , 'turtle']
Georg Brandl8ec7f652007-08-15 14:28:01 +00001529
1530
Martin v. Löwis87184592008-06-04 06:29:55 +00001531 .. method:: register_shape(name, shape=None)
1532 .. method:: addshape(name, shape=None)
1533 Arguments:
1534 (1) name is the name of a gif-file and shape is None.
1535 Installs the corresponding image shape.
1536 !! Image-shapes DO NOT rotate when turning the turtle,
1537 !! so they do not display the heading of the turtle!
1538 (2) name is an arbitrary string and shape is a tuple
1539 of pairs of coordinates. Installs the corresponding
1540 polygon shape
1541 (3) name is an arbitrary string and shape is a
1542 (compound) Shape object. Installs the corresponding
1543 compound shape. (See class Shape.)
1544
1545 Adds a turtle shape to TurtleScreen's shapelist. Only thusly
1546 registered shapes can be used by issueing the command shape(shapename).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001547
Martin v. Löwis87184592008-06-04 06:29:55 +00001548 call: register_shape("turtle.gif")
1549 --or: register_shape("tri", ((0,0), (10,10), (-10,10)))
1550
1551 Example (for a TurtleScreen instance named screen):
1552 >>> screen.register_shape("triangle", ((5,-3),(0,5),(-5,-3)))
Georg Brandl8ec7f652007-08-15 14:28:01 +00001553
1554
Martin v. Löwis87184592008-06-04 06:29:55 +00001555 .. method:: turtles():
1556 Return the list of turtles on the screen.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001557
Martin v. Löwis87184592008-06-04 06:29:55 +00001558 Example (for a TurtleScreen instance named screen):
1559 >>> for turtle in screen.turtles()
1560 ... turtle.color("red")
Georg Brandl8ec7f652007-08-15 14:28:01 +00001561
Georg Brandl8ec7f652007-08-15 14:28:01 +00001562
Martin v. Löwis87184592008-06-04 06:29:55 +00001563 .. method:: window_height():
1564 Return the height of the turtle window.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001565
Martin v. Löwis87184592008-06-04 06:29:55 +00001566 Example (for a TurtleScreen instance named screen):
1567 >>> screen.window_height()
1568 480
Georg Brandl8ec7f652007-08-15 14:28:01 +00001569
Georg Brandl8ec7f652007-08-15 14:28:01 +00001570
Martin v. Löwis87184592008-06-04 06:29:55 +00001571 .. method:: window_width():
1572 Return the width of the turtle window.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001573
Martin v. Löwis87184592008-06-04 06:29:55 +00001574 Example (for a TurtleScreen instance named screen):
1575 >>> screen.window_width()
1576 640
1577
Georg Brandl8ec7f652007-08-15 14:28:01 +00001578
Martin v. Löwis87184592008-06-04 06:29:55 +00001579METHODS SPECIFIC TO Screen, not inherited from TurtleScreen
1580-----------------------------------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001581
Georg Brandl8ec7f652007-08-15 14:28:01 +00001582
Martin v. Löwis87184592008-06-04 06:29:55 +00001583 .. method:: bye():
1584 """Shut the turtlegraphics window.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001585
Martin v. Löwis87184592008-06-04 06:29:55 +00001586 This is a method of the Screen-class and not available for
1587 TurtleScreen instances.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001588
Martin v. Löwis87184592008-06-04 06:29:55 +00001589 Example (for a TurtleScreen instance named screen):
1590 >>> screen.bye()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001591
Georg Brandl8ec7f652007-08-15 14:28:01 +00001592
Martin v. Löwis87184592008-06-04 06:29:55 +00001593 .. method:: exitonclick():
1594 Bind bye() method to mouseclick on TurtleScreen.
1595 If "using_IDLE" - value in configuration dictionary is False
1596 (default value), enter mainloop.
1597 Remark: If IDLE with -n switch (no subprocess) is used, this value
1598 should be set to True in turtle.cfg. In this case IDLE's own mainloop
1599 is active also for the client script.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001600
Martin v. Löwis87184592008-06-04 06:29:55 +00001601 This is a method of the Screen-class and not available for
1602 TurtleScreen instances.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001603
Martin v. Löwis87184592008-06-04 06:29:55 +00001604 Example (for a Screen instance named screen):
1605 >>> screen.exitonclick()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001606
Georg Brandl8ec7f652007-08-15 14:28:01 +00001607
Martin v. Löwis87184592008-06-04 06:29:55 +00001608 .. method:: setup(width=_CFG["width"], height=_CFG["height"],
1609 startx=_CFG["leftright"], starty=_CFG["topbottom"]):
1610 Set the size and position of the main window.
1611 Default values of arguments are stored in the configuration dicionary
1612 and can be changed via a turtle.cfg file.
1613
1614 width -- as integer a size in pixels, as float a fraction of the screen.
1615 Default is 50% of screen.
1616 height -- as integer the height in pixels, as float a fraction of the
1617 screen. Default is 75% of screen.
1618 startx -- if positive, starting position in pixels from the left
1619 edge of the screen, if negative from the right edge
1620 Default, startx=None is to center window horizontally.
1621 starty -- if positive, starting position in pixels from the top
1622 edge of the screen, if negative from the bottom edge
1623 Default, starty=None is to center window vertically.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001624
Martin v. Löwis87184592008-06-04 06:29:55 +00001625 Examples (for a Screen instance named screen)::
1626 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1627 # sets window to 200x200 pixels, in upper left of screen
Georg Brandl8ec7f652007-08-15 14:28:01 +00001628
Martin v. Löwis87184592008-06-04 06:29:55 +00001629 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1630 # sets window to 75% of screen by 50% of screen and centers
Georg Brandl8ec7f652007-08-15 14:28:01 +00001631
Martin v. Löwis87184592008-06-04 06:29:55 +00001632
1633 .. method:: title(titlestring):
1634 titlestring -- a string, to appear in the titlebar of the
1635 turtle graphics window.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001636
Martin v. Löwis87184592008-06-04 06:29:55 +00001637 Set title of turtle-window to titlestring
Georg Brandl8ec7f652007-08-15 14:28:01 +00001638
Martin v. Löwis87184592008-06-04 06:29:55 +00001639 This is a method of the Screen-class and not available for
1640 TurtleScreen instances.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001641
Martin v. Löwis87184592008-06-04 06:29:55 +00001642 Example (for a Screen instance named screen):
1643 >>> screen.title("Welcome to the turtle-zoo!")
Georg Brandl8ec7f652007-08-15 14:28:01 +00001644
Georg Brandl8ec7f652007-08-15 14:28:01 +00001645
1646
Martin v. Löwis87184592008-06-04 06:29:55 +000016474. THE PUBLIC CLASSES of the module turtle.py
1648=============================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00001649
Georg Brandl8ec7f652007-08-15 14:28:01 +00001650
Martin v. Löwis87184592008-06-04 06:29:55 +00001651class RawTurtle(canvas):
1652 canvas -- a Tkinter-Canvas, a ScrolledCanvas or a TurtleScreen
1653
1654 Alias: RawPen
1655
1656 Define a turtle.
1657 A description of the methods follows below. All methods are also
1658 available as functions (to control some anonymous turtle) thus
1659 providing a procedural interface to turtlegraphics
1660
1661class Turtle()
1662 Subclass of RawTurtle, has the same interface with the additional
1663 property, that Turtle instances draw on a default Screen object,
1664 which is created automatically, when needed for the first time.
1665
1666class TurtleScreen(cv)
1667 cv -- a Tkinter-Canvas
1668 Provides screen oriented methods like setbg etc.
1669 A description of the methods follows below.
1670
1671class Screen()
1672 Subclass of TurtleScreen, with four methods added.
1673 All methods are also available as functions to conrtol a unique
1674 Screen instance thus belonging to the procedural interface
1675 to turtlegraphics. This Screen instance is automatically created
1676 when needed for the first time.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001677
Martin v. Löwis87184592008-06-04 06:29:55 +00001678class ScrolledCavas(master)
1679 master -- some Tkinter widget to contain the ScrolledCanvas, i.e.
1680 a Tkinter-canvas with scrollbars added.
1681 Used by class Screen, which thus provides automatically a
1682 ScrolledCanvas as playground for the turtles.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001683
Martin v. Löwis87184592008-06-04 06:29:55 +00001684class Shape(type\_, data)
1685 type --- one of the strings "polygon", "image", "compound"
Georg Brandl8ec7f652007-08-15 14:28:01 +00001686
Martin v. Löwis87184592008-06-04 06:29:55 +00001687 Data structure modeling shapes.
1688 The pair type\_, data must be as follows:
1689
1690 type\_ data
Georg Brandl8ec7f652007-08-15 14:28:01 +00001691
Martin v. Löwis87184592008-06-04 06:29:55 +00001692 "polygon" a polygon-tuple, i. e.
1693 a tuple of pairs of coordinates
1694
1695 "image" an image (in this form only used internally!)
1696
1697 "compound" None
1698 A compund shape has to be constructed using
1699 the addcomponent method
1700
1701 addcomponent(self, poly, fill, outline=None)
1702 poly -- polygon, i. e. a tuple of pairs of numbers.
1703 fill -- the fillcolor of the component,
1704 outline -- the outline color of the component.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001705
Martin v. Löwis87184592008-06-04 06:29:55 +00001706 Example:
1707 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
1708 >>> s = Shape("compound")
1709 >>> s.addcomponent(poly, "red", "blue")
1710 ### .. add more components and then use register_shape()
1711
1712 See EXCURSUS ABOUT THE USE OF COMPOUND SHAPES
1713
Georg Brandl8ec7f652007-08-15 14:28:01 +00001714
Martin v. Löwis87184592008-06-04 06:29:55 +00001715class Vec2D(x, y):
1716 A two-dimensional vector class, used as a helper class
1717 for implementing turtle graphics.
1718 May be useful for turtle graphics programs also.
1719 Derived from tuple, so a vector is a tuple!
Georg Brandl8ec7f652007-08-15 14:28:01 +00001720
Martin v. Löwis87184592008-06-04 06:29:55 +00001721 Provides (for a, b vectors, k number):
Georg Brandl8ec7f652007-08-15 14:28:01 +00001722
Martin v. Löwis87184592008-06-04 06:29:55 +00001723 * a+b vector addition
1724 * a-b vector subtraction
1725 * a*b inner product
1726 * k*a and a*k multiplication with scalar
1727 * \|a\| absolute value of a
1728 * a.rotate(angle) rotation
Georg Brandl8ec7f652007-08-15 14:28:01 +00001729
1730
Martin v. Löwis87184592008-06-04 06:29:55 +00001731
1732V. HELP AND CONFIGURATION
1733=========================
Georg Brandl8ec7f652007-08-15 14:28:01 +00001734
Martin v. Löwis87184592008-06-04 06:29:55 +00001735This section contains subsections on:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001736
Martin v. Löwis87184592008-06-04 06:29:55 +00001737- how to use help
1738- how to prepare and use translations of the online-help
1739 into other languages
1740- how to configure the appearance of the graphics window and
1741 the turtles at startup
Georg Brandl8ec7f652007-08-15 14:28:01 +00001742
1743
Martin v. Löwis87184592008-06-04 06:29:55 +00001744HOW TO USE HELP:
1745----------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001746
Martin v. Löwis87184592008-06-04 06:29:55 +00001747The public methods of the Screen and Turtle classes are documented
1748extensively via docstrings. So these can be used as online-help
1749via the Python help facilities:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001750
Martin v. Löwis87184592008-06-04 06:29:55 +00001751- When using IDLE, tooltips show the signatures and first lines of
1752 the docstrings of typed in function-/method calls.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001753
Martin v. Löwis87184592008-06-04 06:29:55 +00001754- calling help on methods or functions display the docstrings.
1755 Examples::
1756
1757 >>> help(Screen.bgcolor)
1758 Help on method bgcolor in module turtle:
1759
1760 bgcolor(self, *args) unbound turtle.Screen method
1761 Set or return backgroundcolor of the TurtleScreen.
1762
1763 Arguments (if given): a color string or three numbers
1764 in the range 0..colormode or a 3-tuple of such numbers.
1765
1766 Example (for a TurtleScreen instance named screen)::
1767
1768 >>> screen.bgcolor("orange")
1769 >>> screen.bgcolor()
1770 'orange'
1771 >>> screen.bgcolor(0.5,0,0.5)
1772 >>> screen.bgcolor()
1773 '#800080'
1774
1775 >>> help(Turtle.penup)
1776 Help on method penup in module turtle:
1777
1778 penup(self) unbound turtle.Turtle method
1779 Pull the pen up -- no drawing when moving.
1780
1781 Aliases: penup | pu | up
1782
1783 No argument
1784
1785 Example (for a Turtle instance named turtle):
1786 >>> turtle.penup()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001787
Martin v. Löwis87184592008-06-04 06:29:55 +00001788The docstrings of the functions which are derived from methods have
1789a modified form::
Georg Brandl8ec7f652007-08-15 14:28:01 +00001790
Martin v. Löwis87184592008-06-04 06:29:55 +00001791 >>> help(bgcolor)
1792 Help on function bgcolor in module turtle:
1793
1794 bgcolor(*args)
1795 Set or return backgroundcolor of the TurtleScreen.
1796
1797 Arguments (if given): a color string or three numbers
1798 in the range 0..colormode or a 3-tuple of such numbers.
1799
1800 Example::
1801
1802 >>> bgcolor("orange")
1803 >>> bgcolor()
1804 'orange'
1805 >>> bgcolor(0.5,0,0.5)
1806 >>> bgcolor()
1807 '#800080'
1808
1809 >>> help(penup)
1810 Help on function penup in module turtle:
1811
1812 penup()
1813 Pull the pen up -- no drawing when moving.
1814
1815 Aliases: penup | pu | up
1816
1817 No argument
1818
1819 Example:
1820 >>> penup()
Georg Brandl8ec7f652007-08-15 14:28:01 +00001821
Martin v. Löwis87184592008-06-04 06:29:55 +00001822These modified docstrings are created automatically together with the
1823function definitions that are derived from the methods at import time.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001824
1825
Martin v. Löwis87184592008-06-04 06:29:55 +00001826TRANSLATION OF DOCSTRINGS INTO DIFFERENT LANGUAGES
1827--------------------------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001828
Martin v. Löwis87184592008-06-04 06:29:55 +00001829There is a utility to create a dictionary the keys of which are the
1830method names and the values of which are the docstrings of the public
1831methods of the classes Screen and Turtle.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001832
Martin v. Löwis87184592008-06-04 06:29:55 +00001833write_docstringdict(filename="turtle_docstringdict"):
1834 filename -- a string, used as filename
Georg Brandl8ec7f652007-08-15 14:28:01 +00001835
Martin v. Löwis87184592008-06-04 06:29:55 +00001836 Create and write docstring-dictionary to a Python script
1837 with the given filename.
1838 This function has to be called explicitely, (it is not used by the
1839 turtle-graphics classes). The docstring dictionary will be written
1840 to the Python script <filname>.py It is intended to serve as a
1841 template for translation of the docstrings into different languages.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001842
Martin v. Löwis87184592008-06-04 06:29:55 +00001843If you (or your students) want to use turtle.py with online help in
1844your native language. You have to translate the docstrings and save
1845the resulting file as e.g. turtle_docstringdict_german.py
Georg Brandl8ec7f652007-08-15 14:28:01 +00001846
Martin v. Löwis87184592008-06-04 06:29:55 +00001847If you have an appropriate entry in your turtle.cfg file this dictionary
1848will be read in at import time and will replace the original English
1849docstrings.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001850
Martin v. Löwis87184592008-06-04 06:29:55 +00001851At the time of this writing there exist docstring_dicts in German
1852and in Italian. (Requests please to glingl@aon.at)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001853
Martin v. Löwis87184592008-06-04 06:29:55 +00001854
1855
1856HOW TO CONFIGURE SCREEN AND TURTLES
1857-----------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +00001858
Martin v. Löwis87184592008-06-04 06:29:55 +00001859The built-in default configuration mimics the appearance and
1860behaviour of the old turtle module in order to retain best possible
1861compatibility with it.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001862
Martin v. Löwis87184592008-06-04 06:29:55 +00001863If you want to use a different configuration which reflects
1864better the features of this module or which fits better to
1865your needs, e. g. for use in a classroom, you can prepare
1866a configuration file turtle.cfg which will be read at import
1867time and modify the configuration according to it's settings.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001868
Martin v. Löwis87184592008-06-04 06:29:55 +00001869The built in configuration would correspond to the following
1870turtle.cfg:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001871
Martin v. Löwis87184592008-06-04 06:29:55 +00001872width = 0.5
1873height = 0.75
1874leftright = None
1875topbottom = None
1876canvwidth = 400
1877canvheight = 300
1878mode = standard
1879colormode = 1.0
1880delay = 10
1881undobuffersize = 1000
1882shape = classic
1883pencolor = black
1884fillcolor = black
1885resizemode = noresize
1886visible = True
1887language = english
1888exampleturtle = turtle
1889examplescreen = screen
1890title = Python Turtle Graphics
1891using_IDLE = False
Georg Brandl8ec7f652007-08-15 14:28:01 +00001892
Martin v. Löwis87184592008-06-04 06:29:55 +00001893Short explanation of selected entries:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001894
Martin v. Löwis87184592008-06-04 06:29:55 +00001895- The first four lines correspond to the arguments of the
1896 Screen.setup method
1897- Line 5 and 6 correspond to the arguments of the Method
1898 Screen.screensize
1899- shape can be any of the built-in shapes, e.g: arrow, turtle,
1900 etc. For more info try help(shape)
1901- if you want to use no fillcolor (i. e. turtle transparent),
1902 you have to write:
1903 fillcolor = ""
1904 (All not empty strings must not have quotes in the cfg-file!)
1905- if you want to reflect the turtle its state, you have to use
1906 resizemode = auto
1907- if you set, e. g.: language = italian
1908 the docstringdict turtle_docstringdict_italian.py will be
1909 loaded at import time (if present on the import path, e.g. in
1910 the same directory as turtle.py
1911- the entries exampleturtle and examplescreen define the names
1912 of these objects as they occur in the docstrings. The
1913 transformation of method-docstrings to function-docstrings
1914 will delete these names from the docstrings. (See examples in
1915 section on HELP)
1916- using_IDLE Set this to True if you regularly work with IDLE
1917 and it's -n - switch. ("No subprocess") This will prevent
1918 exitonclick to enter the mainloop.
1919
1920There can be a turtle.cfg file in the directory where turtle.py
1921is stored and an additional one in the currentworkingdirectory.
1922The latter will override the settings of the first one.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001923
Martin v. Löwis87184592008-06-04 06:29:55 +00001924The turtledemo directory contains a turtle.cfg file. If you
1925study it as an example and see its effects when running the
1926demos (preferably not from within the demo-viewer).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001927
Martin v. Löwis87184592008-06-04 06:29:55 +00001928
1929VI. Demo scripts
1930================
1931
1932There is a set of demo scripts in the turtledemo directory
1933located here ...
Georg Brandl8ec7f652007-08-15 14:28:01 +00001934
Martin v. Löwis87184592008-06-04 06:29:55 +00001935 ##### please complete info about path ########################
1936
1937It contains:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001938
Martin v. Löwis87184592008-06-04 06:29:55 +00001939- a set of 15 demo scripts demonstrating differet features
1940 of the new module turtle.py
1941- a Demo-Viewer turtleDemo.py which can be used to view
1942 the sourcecode of the scripts and run them at the same time
1943 14 of the examples can be accessed via the Examples Menu.
1944 All of them can also be run standalone.
1945- The example turtledemo_two_canvases.py demonstrates the
1946 simultaneous use of two canvases with the turtle module.
1947 Therefor it only can be run standalone.
1948- There is a turtle.cfg file in this directory, which also
1949 serves as an example for how to write and use such files.
1950
1951The demoscripts are:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001952
Martin v. Löwis87184592008-06-04 06:29:55 +00001953+----------------+------------------------------+-----------------------+
1954|Name | description | features |
1955+----------------+------------------------------+-----------------------+
1956|bytedesign | complex classical | tracer, delay |
1957| | turtlegraphics pattern | update |
1958+----------------+------------------------------+-----------------------+
1959|chaos | graphs verhust dynamics, | worldcoordinates |
1960| | proofs that you must not | |
1961| | trust computers computations| |
1962+----------------+------------------------------+-----------------------+
1963|clock | analog clock showing time | turtles as clock's |
1964| | of your computer | hands, ontimer |
1965+----------------+------------------------------+-----------------------+
1966|colormixer | experiment with r, g, b | ondrag |
1967+----------------+------------------------------+-----------------------+
1968|fractalcurves | Hilbert & Koch | recursion |
1969+----------------+------------------------------+-----------------------+
1970|lindenmayer | ethnomathematics | L-System |
1971| | (indian kolams) | |
1972+----------------+------------------------------+-----------------------+
1973|minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
1974| | | as Hanoi-Discs |
1975| | | (shape, shapesize) |
1976+----------------+------------------------------+-----------------------+
1977|paint | super minimalistic | onclick |
1978| | drawing program | |
1979+----------------+------------------------------+-----------------------+
1980|peace | elementary | turtle: appearance |
1981| | | and animation |
1982+----------------+------------------------------+-----------------------+
1983|penrose | aperiodic tiling with | stamp |
1984| | kites and darts | |
1985+----------------+------------------------------+-----------------------+
1986|planet_and_moon | simulation of | compound shape |
1987| | gravitational system | Vec2D |
1988+----------------+------------------------------+-----------------------+
1989|tree | a (graphical) breadth | clone |
1990| | first tree (using generators)| |
1991+----------------+------------------------------+-----------------------+
1992|wikipedia | a pattern from the wikipedia | clone, undo |
1993| | article on turtle-graphics | |
1994+----------------+------------------------------+-----------------------+
1995|yingyang | another elementary example | circle |
1996+----------------+------------------------------+-----------------------+
Georg Brandl8ec7f652007-08-15 14:28:01 +00001997
Martin v. Löwis87184592008-06-04 06:29:55 +00001998turtledemo_two-canvases: two distinct Tkinter-Canvases
1999are populated with turtles. Uses class RawTurtle.
Georg Brandl8ec7f652007-08-15 14:28:01 +00002000
Georg Brandl8ec7f652007-08-15 14:28:01 +00002001
Martin v. Löwis87184592008-06-04 06:29:55 +00002002Have fun!