1 ========================================
2 :mod:`turtle` --- Turtle graphics for Tk
3 ========================================
6 :synopsis: Turtle graphics for Tk
7 .. sectionauthor:: Gregor Lingl <gregor.lingl@aon.at>
17 Turtle graphics is a popular way for introducing programming to kids. It was
18 part of the original Logo programming language developed by Wally Feurzig and
19 Seymour Papert in 1966.
21 Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it the
22 command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
23 direction 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.
26 By combining together these and similar commands, intricate shapes and pictures
29 The :mod:`turtle` module is an extended reimplementation of the same-named
30 module from the Python standard distribution up to version Python 2.5.
32 It tries to keep the merits of the old turtle module and to be (nearly) 100%
33 compatible with it. This means in the first place to enable the learning
34 programmer to use all the commands, classes and methods interactively when using
35 the module from within IDLE run with the ``-n`` switch.
37 The turtle module provides turtle graphics primitives, in both object-oriented
38 and procedure-oriented ways. Because it uses :mod:`Tkinter` for the underlying
39 graphics, it needs a version of Python installed with Tk support.
41 The object-oriented interface uses essentially two+two classes:
43 1. 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.
48 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.
53 All methods of TurtleScreen/Screen also exist as functions, i.e. as part of
54 the procedure-oriented interface.
56 2. :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.
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.
64 All methods of RawTurtle/Turtle also exist as functions, i.e. part of the
65 procedure-oriented interface.
67 The procedural interface provides functions which are derived from the methods
68 of the classes :class:`Screen` and :class:`Turtle`. They have the same names as
69 the corresponding methods. A screen object is automatically created whenever a
70 function derived from a Screen method is called. An (unnamed) turtle object is
71 automatically created whenever any of the functions derived from a Turtle method
74 To use multiple turtles an a screen one has to use the object-oriented interface.
77 In the following documentation the argument list for functions is given.
78 Methods, of course, have the additional first argument *self* which is
82 Overview over available Turtle and Screen methods
83 =================================================
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`
97 | :func:`setheading` | :func:`seth`
103 | :func:`clearstamps`
108 | :func:`position` | :func:`pos`
115 Setting and measurement
121 | :func:`pendown` | :func:`pd` | :func:`down`
122 | :func:`penup` | :func:`pu` | :func:`up`
123 | :func:`pensize` | :func:`width`
144 | :func:`showturtle` | :func:`st`
145 | :func:`hideturtle` | :func:`ht`
151 | :func:`shapesize` | :func:`turtlesize`
152 | :func:`settiltangle`
161 Special Turtle methods
166 | :func:`getturtle` | :func:`getpen`
168 | :func:`setundobuffer`
169 | :func:`undobufferentries`
171 | :func:`window_width`
172 | :func:`window_height`
175 Methods of TurtleScreen/Screen
176 ------------------------------
181 | :func:`clear` | :func:`clearscreen`
182 | :func:`reset` | :func:`resetscreen`
184 | :func:`setworldcoordinates`
194 | :func:`onclick` | :func:`onscreenclick`
197 Settings and special methods
202 | :func:`register_shape` | :func:`addshape`
204 | :func:`window_height`
205 | :func:`window_width`
207 Methods specific to Screen
209 | :func:`exitonclick`
214 Methods of RawTurtle/Turtle and corresponding functions
215 =======================================================
217 Most of the examples in this section refer to a Turtle instance called
223 .. function:: forward(distance)
226 :param distance: a number (integer or float)
228 Move the turtle forward by the specified *distance*, in the direction the
233 >>> turtle.position()
235 >>> turtle.forward(25)
236 >>> turtle.position()
238 >>> turtle.forward(-75)
239 >>> turtle.position()
243 .. function:: back(distance)
247 :param distance: a number
249 Move the turtle backward by *distance*, opposite to the direction the
250 turtle is headed. Do not change the turtle's heading.
255 >>> turtle.goto(0, 0)
259 >>> turtle.position()
261 >>> turtle.backward(30)
262 >>> turtle.position()
266 .. function:: right(angle)
269 :param angle: a number (integer or float)
271 Turn turtle right by *angle* units. (Units are by default degrees, but
272 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
273 orientation depends on the turtle mode, see :func:`mode`.
278 >>> turtle.setheading(22)
289 .. function:: left(angle)
292 :param angle: a number (integer or float)
294 Turn turtle left by *angle* units. (Units are by default degrees, but
295 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
296 orientation depends on the turtle mode, see :func:`mode`.
301 >>> turtle.setheading(22)
312 .. function:: goto(x, y=None)
314 setposition(x, y=None)
316 :param x: a number or a pair/vector of numbers
317 :param y: a number or ``None``
319 If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D`
320 (e.g. as returned by :func:`pos`).
322 Move turtle to an absolute position. If the pen is down, draw line. Do
323 not change the turtle's orientation.
328 >>> turtle.goto(0, 0)
332 >>> tp = turtle.pos()
335 >>> turtle.setpos(60,30)
338 >>> turtle.setpos((20,80))
341 >>> turtle.setpos(tp)
346 .. function:: setx(x)
348 :param x: a number (integer or float)
350 Set the turtle's first coordinate to *x*, leave second coordinate
356 >>> turtle.goto(0, 240)
360 >>> turtle.position()
363 >>> turtle.position()
367 .. function:: sety(y)
369 :param y: a number (integer or float)
371 Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
376 >>> turtle.goto(0, 40)
380 >>> turtle.position()
383 >>> turtle.position()
387 .. function:: setheading(to_angle)
390 :param to_angle: a number (integer or float)
392 Set the orientation of the turtle to *to_angle*. Here are some common
393 directions in degrees:
395 =================== ====================
396 standard mode logo mode
397 =================== ====================
400 180 - west 180 - south
401 270 - south 270 - west
402 =================== ====================
406 >>> turtle.setheading(90)
413 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
414 its start-orientation (which depends on the mode, see :func:`mode`).
419 >>> turtle.setheading(90)
420 >>> turtle.goto(0, -10)
426 >>> turtle.position()
429 >>> turtle.position()
435 .. function:: circle(radius, extent=None, steps=None)
437 :param radius: a number
438 :param extent: a number (or ``None``)
439 :param steps: an integer (or ``None``)
441 Draw a circle with given *radius*. The center is *radius* units left of
442 the turtle; *extent* -- an angle -- determines which part of the circle
443 is drawn. If *extent* is not given, draw the entire circle. If *extent*
444 is not a full circle, one endpoint of the arc is the current pen
445 position. Draw the arc in counterclockwise direction if *radius* is
446 positive, otherwise in clockwise direction. Finally the direction of the
447 turtle is changed by the amount of *extent*.
449 As the circle is approximated by an inscribed regular polygon, *steps*
450 determines the number of steps to use. If not given, it will be
451 calculated automatically. May be used to draw regular polygons.
456 >>> turtle.position()
460 >>> turtle.circle(50)
461 >>> turtle.position()
465 >>> turtle.circle(120, 180) # draw a semicircle
466 >>> turtle.position()
472 .. function:: dot(size=None, *color)
474 :param size: an integer >= 1 (if given)
475 :param color: a colorstring or a numeric color tuple
477 Draw a circular dot with diameter *size*, using *color*. If *size* is
478 not given, the maximum of pensize+4 and 2*pensize is used.
485 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
486 >>> turtle.position()
492 .. function:: stamp()
494 Stamp a copy of the turtle shape onto the canvas at the current turtle
495 position. Return a stamp_id for that stamp, which can be used to delete
496 it by calling ``clearstamp(stamp_id)``.
500 >>> turtle.color("blue")
506 .. function:: clearstamp(stampid)
508 :param stampid: an integer, must be return value of previous
511 Delete stamp with given *stampid*.
515 >>> turtle.position()
517 >>> turtle.color("blue")
518 >>> astamp = turtle.stamp()
520 >>> turtle.position()
522 >>> turtle.clearstamp(astamp)
523 >>> turtle.position()
527 .. function:: clearstamps(n=None)
529 :param n: an integer (or ``None``)
531 Delete all or first/last *n* of turtle's stamps. If *n* is None, delete
532 all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete
537 >>> for i in range(8):
538 ... turtle.stamp(); turtle.fd(30)
547 >>> turtle.clearstamps(2)
548 >>> turtle.clearstamps(-2)
549 >>> turtle.clearstamps()
554 Undo (repeatedly) the last turtle action(s). Number of available
555 undo actions is determined by the size of the undobuffer.
559 >>> for i in range(4):
560 ... turtle.fd(50); turtle.lt(80)
562 >>> for i in range(8):
566 .. function:: speed(speed=None)
568 :param speed: an integer in the range 0..10 or a speedstring (see below)
570 Set the turtle's speed to an integer value in the range 0..10. If no
571 argument is given, return current speed.
573 If input is a number greater than 10 or smaller than 0.5, speed is set
574 to 0. Speedstrings are mapped to speedvalues as follows:
582 Speeds from 1 to 10 enforce increasingly faster animation of line drawing
585 Attention: *speed* = 0 means that *no* animation takes
586 place. forward/back makes turtle jump and likewise left/right make the
587 turtle turn instantly.
593 >>> turtle.speed('normal')
604 .. function:: position()
607 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
615 .. function:: towards(x, y=None)
617 :param x: a number or a pair/vector of numbers or a turtle instance
618 :param y: a number if *x* is a number, else ``None``
620 Return the angle between the line from turtle position to position specified
621 by (x,y), the vector or the other turtle. This depends on the turtle's start
622 orientation which depends on the mode - "standard"/"world" or "logo").
626 >>> turtle.goto(10, 10)
627 >>> turtle.towards(0,0)
633 Return the turtle's x coordinate.
639 >>> turtle.forward(100)
642 >>> print turtle.xcor()
648 Return the turtle's y coordinate.
654 >>> turtle.forward(100)
655 >>> print turtle.pos()
657 >>> print turtle.ycor()
661 .. function:: heading()
663 Return the turtle's current heading (value depends on the turtle mode, see
674 .. function:: distance(x, y=None)
676 :param x: a number or a pair/vector of numbers or a turtle instance
677 :param y: a number if *x* is a number, else ``None``
679 Return the distance from the turtle to (x,y), the given vector, or the given
680 other turtle, in turtle step units.
685 >>> turtle.distance(30,40)
687 >>> turtle.distance((30,40))
691 >>> turtle.distance(joe)
695 Settings for measurement
696 ------------------------
698 .. function:: degrees(fullcircle=360.0)
700 :param fullcircle: a number
702 Set angle measurement units, i.e. set number of "degrees" for a full circle.
703 Default value is 360 degrees.
712 Change angle measurement unit to grad (also known as gon,
713 grade, or gradian and equals 1/100-th of the right angle.)
714 >>> turtle.degrees(400.0)
717 >>> turtle.degrees(360)
722 .. function:: radians()
724 Set the angle measurement units to radians. Equivalent to
725 ``degrees(2*math.pi)``.
740 >>> turtle.degrees(360)
749 .. function:: pendown()
753 Pull the pen down -- drawing when moving.
756 .. function:: penup()
760 Pull the pen up -- no drawing when moving.
763 .. function:: pensize(width=None)
766 :param width: a positive number
768 Set the line thickness to *width* or return it. If resizemode is set to
769 "auto" and turtleshape is a polygon, that polygon is drawn with the same line
770 thickness. If no argument is given, the current pensize is returned.
776 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
779 .. function:: pen(pen=None, **pendict)
781 :param pen: a dictionary with some or all of the below listed keys
782 :param pendict: one or more keyword-arguments with the below listed keys as keywords
784 Return or set the pen's attributes in a "pen-dictionary" with the following
787 * "shown": True/False
788 * "pendown": True/False
789 * "pencolor": color-string or color-tuple
790 * "fillcolor": color-string or color-tuple
791 * "pensize": positive number
792 * "speed": number in range 0..10
793 * "resizemode": "auto" or "user" or "noresize"
794 * "stretchfactor": (positive number, positive number)
795 * "outline": positive number
798 This dictionary can be used as argument for a subsequent call to :func:`pen`
799 to restore the former pen-state. Moreover one or more of these attributes
800 can be provided as keyword-arguments. This can be used to set several pen
801 attributes in one statement.
804 :options: +NORMALIZE_WHITESPACE
806 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
807 >>> sorted(turtle.pen().items())
808 [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
809 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
810 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
811 >>> penstate=turtle.pen()
812 >>> turtle.color("yellow", "")
814 >>> sorted(turtle.pen().items())
815 [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow'),
816 ('pendown', False), ('pensize', 10), ('resizemode', 'noresize'),
817 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
818 >>> turtle.pen(penstate, fillcolor="green")
819 >>> sorted(turtle.pen().items())
820 [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red'),
821 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
822 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
825 .. function:: isdown()
827 Return ``True`` if pen is down, ``False`` if it's up.
842 .. function:: pencolor(*args)
844 Return or set the pencolor.
846 Four input formats are allowed:
849 Return the current pencolor as color specification string or
850 as a tuple (see example). May be used as input to another
851 color/pencolor/fillcolor call.
853 ``pencolor(colorstring)``
854 Set pencolor to *colorstring*, which is a Tk color specification string,
855 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
857 ``pencolor((r, g, b))``
858 Set pencolor to the RGB color represented by the tuple of *r*, *g*, and
859 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
860 colormode is either 1.0 or 255 (see :func:`colormode`).
862 ``pencolor(r, g, b)``
863 Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of
864 *r*, *g*, and *b* must be in the range 0..colormode.
866 If turtleshape is a polygon, the outline of that polygon is drawn with the
873 >>> turtle.pencolor()
875 >>> turtle.pencolor("brown")
876 >>> turtle.pencolor()
878 >>> tup = (0.2, 0.8, 0.55)
879 >>> turtle.pencolor(tup)
880 >>> turtle.pencolor()
881 (0.2, 0.8, 0.5490196078431373)
883 >>> turtle.pencolor()
885 >>> turtle.pencolor('#32c18f')
886 >>> turtle.pencolor()
890 .. function:: fillcolor(*args)
892 Return or set the fillcolor.
894 Four input formats are allowed:
897 Return the current fillcolor as color specification string, possibly
898 in tuple format (see example). May be used as input to another
899 color/pencolor/fillcolor call.
901 ``fillcolor(colorstring)``
902 Set fillcolor to *colorstring*, which is a Tk color specification string,
903 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
905 ``fillcolor((r, g, b))``
906 Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and
907 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
908 colormode is either 1.0 or 255 (see :func:`colormode`).
910 ``fillcolor(r, g, b)``
911 Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of
912 *r*, *g*, and *b* must be in the range 0..colormode.
914 If turtleshape is a polygon, the interior of that polygon is drawn
915 with the newly set fillcolor.
919 >>> turtle.fillcolor("violet")
920 >>> turtle.fillcolor()
922 >>> col = turtle.pencolor()
925 >>> turtle.fillcolor(col)
926 >>> turtle.fillcolor()
928 >>> turtle.fillcolor('#ffffff')
929 >>> turtle.fillcolor()
933 .. function:: color(*args)
935 Return or set pencolor and fillcolor.
937 Several input formats are allowed. They use 0 to 3 arguments as
941 Return the current pencolor and the current fillcolor as a pair of color
942 specification strings or tuples as returned by :func:`pencolor` and
945 ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)``
946 Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the
949 ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))``
950 Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)``
951 and analogously if the other input format is used.
953 If turtleshape is a polygon, outline and interior of that polygon is drawn
954 with the newly set colors.
958 >>> turtle.color("red", "green")
961 >>> color("#285078", "#a0c8f0")
963 ((40, 80, 120), (160, 200, 240))
966 See also: Screen method :func:`colormode`.
977 .. function:: fill(flag)
979 :param flag: True/False (or 1/0 respectively)
981 Call ``fill(True)`` before drawing the shape you want to fill, and
982 ``fill(False)`` when done. When used without argument: return fillstate
983 (``True`` if filling, ``False`` else).
987 >>> turtle.fill(True)
988 >>> for _ in range(3):
989 ... turtle.forward(100)
992 >>> turtle.fill(False)
995 .. function:: begin_fill()
997 Call just before drawing a shape to be filled. Equivalent to ``fill(True)``.
1000 .. function:: end_fill()
1002 Fill the shape drawn after the last call to :func:`begin_fill`. Equivalent
1007 >>> turtle.color("black", "red")
1008 >>> turtle.begin_fill()
1009 >>> turtle.circle(80)
1010 >>> turtle.end_fill()
1013 More drawing control
1014 ~~~~~~~~~~~~~~~~~~~~
1016 .. function:: reset()
1018 Delete the turtle's drawings from the screen, re-center the turtle and set
1019 variables to the default values.
1023 >>> turtle.goto(0,-22)
1024 >>> turtle.left(100)
1025 >>> turtle.position()
1027 >>> turtle.heading()
1030 >>> turtle.position()
1032 >>> turtle.heading()
1036 .. function:: clear()
1038 Delete the turtle's drawings from the screen. Do not move turtle. State and
1039 position of the turtle as well as drawings of other turtles are not affected.
1042 .. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal"))
1044 :param arg: object to be written to the TurtleScreen
1045 :param move: True/False
1046 :param align: one of the strings "left", "center" or right"
1047 :param font: a triple (fontname, fontsize, fonttype)
1049 Write text - the string representation of *arg* - at the current turtle
1050 position according to *align* ("left", "center" or right") and with the given
1051 font. If *move* is True, the pen is moved to the bottom-right corner of the
1052 text. By default, *move* is False.
1054 >>> turtle.write("Home = ", True, align="center")
1055 >>> turtle.write((0,0), True)
1064 .. function:: hideturtle()
1067 Make the turtle invisible. It's a good idea to do this while you're in the
1068 middle of doing some complex drawing, because hiding the turtle speeds up the
1073 >>> turtle.hideturtle()
1076 .. function:: showturtle()
1079 Make the turtle visible.
1083 >>> turtle.showturtle()
1086 .. function:: isvisible()
1088 Return True if the Turtle is shown, False if it's hidden.
1090 >>> turtle.hideturtle()
1091 >>> turtle.isvisible()
1093 >>> turtle.showturtle()
1094 >>> turtle.isvisible()
1101 .. function:: shape(name=None)
1103 :param name: a string which is a valid shapename
1105 Set turtle shape to shape with given *name* or, if name is not given, return
1106 name of current shape. Shape with *name* must exist in the TurtleScreen's
1107 shape dictionary. Initially there are the following polygon shapes: "arrow",
1108 "turtle", "circle", "square", "triangle", "classic". To learn about how to
1109 deal with shapes see Screen method :func:`register_shape`.
1115 >>> turtle.shape("turtle")
1120 .. function:: resizemode(rmode=None)
1122 :param rmode: one of the strings "auto", "user", "noresize"
1124 Set resizemode to one of the values: "auto", "user", "noresize". If *rmode*
1125 is not given, return current resizemode. Different resizemodes have the
1128 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
1129 - "user": adapts the appearance of the turtle according to the values of
1130 stretchfactor and outlinewidth (outline), which are set by
1132 - "noresize": no adaption of the turtle's appearance takes place.
1134 resizemode("user") is called by :func:`shapesize` when used with arguments.
1138 >>> turtle.resizemode()
1140 >>> turtle.resizemode("auto")
1141 >>> turtle.resizemode()
1145 .. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None)
1146 turtlesize(stretch_wid=None, stretch_len=None, outline=None)
1148 :param stretch_wid: positive number
1149 :param stretch_len: positive number
1150 :param outline: positive number
1152 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set
1153 resizemode to "user". If and only if resizemode is set to "user", the turtle
1154 will be displayed stretched according to its stretchfactors: *stretch_wid* is
1155 stretchfactor perpendicular to its orientation, *stretch_len* is
1156 stretchfactor in direction of its orientation, *outline* determines the width
1157 of the shapes's outline.
1161 >>> turtle.shapesize()
1163 >>> turtle.resizemode("user")
1164 >>> turtle.shapesize(5, 5, 12)
1165 >>> turtle.shapesize()
1167 >>> turtle.shapesize(outline=8)
1168 >>> turtle.shapesize()
1172 .. function:: tilt(angle)
1174 :param angle: a number
1176 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
1177 change the turtle's heading (direction of movement).
1182 >>> turtle.shape("circle")
1183 >>> turtle.shapesize(5,2)
1190 .. function:: settiltangle(angle)
1192 :param angle: a number
1194 Rotate the turtleshape to point in the direction specified by *angle*,
1195 regardless of its current tilt-angle. *Do not* change the turtle's heading
1196 (direction of movement).
1201 >>> turtle.shape("circle")
1202 >>> turtle.shapesize(5,2)
1203 >>> turtle.settiltangle(45)
1205 >>> turtle.settiltangle(-45)
1209 .. function:: tiltangle()
1211 Return the current tilt-angle, i.e. the angle between the orientation of the
1212 turtleshape and the heading of the turtle (its direction of movement).
1217 >>> turtle.shape("circle")
1218 >>> turtle.shapesize(5,2)
1220 >>> turtle.tiltangle()
1227 .. function:: onclick(fun, btn=1, add=None)
1229 :param fun: a function with two arguments which will be called with the
1230 coordinates of the clicked point on the canvas
1231 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1232 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1233 added, otherwise it will replace a former binding
1235 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1236 existing bindings are removed. Example for the anonymous turtle, i.e. the
1244 >>> onclick(turn) # Now clicking into the turtle will turn it.
1245 >>> onclick(None) # event-binding will be removed
1248 .. function:: onrelease(fun, btn=1, add=None)
1250 :param fun: a function with two arguments which will be called with the
1251 coordinates of the clicked point on the canvas
1252 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1253 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1254 added, otherwise it will replace a former binding
1256 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1257 ``None``, existing bindings are removed.
1261 >>> class MyTurtle(Turtle):
1262 ... def glow(self,x,y):
1263 ... self.fillcolor("red")
1264 ... def unglow(self,x,y):
1265 ... self.fillcolor("")
1267 >>> turtle = MyTurtle()
1268 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1269 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
1272 .. function:: ondrag(fun, btn=1, add=None)
1274 :param fun: a function with two arguments which will be called with the
1275 coordinates of the clicked point on the canvas
1276 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1277 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1278 added, otherwise it will replace a former binding
1280 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1281 existing bindings are removed.
1283 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1284 mouse-click event on that turtle.
1288 >>> turtle.ondrag(turtle.goto)
1290 Subsequently, clicking and dragging the Turtle will move it across
1291 the screen thereby producing handdrawings (if pen is down).
1294 Special Turtle methods
1295 ----------------------
1297 .. function:: begin_poly()
1299 Start recording the vertices of a polygon. Current turtle position is first
1303 .. function:: end_poly()
1305 Stop recording the vertices of a polygon. Current turtle position is last
1306 vertex of polygon. This will be connected with the first vertex.
1309 .. function:: get_poly()
1311 Return the last recorded polygon.
1316 >>> turtle.begin_poly()
1322 >>> turtle.end_poly()
1323 >>> p = turtle.get_poly()
1324 >>> register_shape("myFavouriteShape", p)
1327 .. function:: clone()
1329 Create and return a clone of the turtle with same position, heading and
1335 >>> joe = mick.clone()
1338 .. function:: getturtle()
1341 Return the Turtle object itself. Only reasonable use: as a function to
1342 return the "anonymous turtle":
1346 >>> pet = getturtle()
1349 <turtle.Turtle object at 0x...>
1352 .. function:: getscreen()
1354 Return the :class:`TurtleScreen` object the turtle is drawing on.
1355 TurtleScreen methods can then be called for that object.
1359 >>> ts = turtle.getscreen()
1361 <turtle._Screen object at 0x...>
1362 >>> ts.bgcolor("pink")
1365 .. function:: setundobuffer(size)
1367 :param size: an integer or ``None``
1369 Set or disable undobuffer. If *size* is an integer an empty undobuffer of
1370 given size is installed. *size* gives the maximum number of turtle actions
1371 that can be undone by the :func:`undo` method/function. If *size* is
1372 ``None``, the undobuffer is disabled.
1376 >>> turtle.setundobuffer(42)
1379 .. function:: undobufferentries()
1381 Return number of entries in the undobuffer.
1385 >>> while undobufferentries():
1389 .. function:: tracer(flag=None, delay=None)
1391 A replica of the corresponding TurtleScreen method.
1396 .. function:: window_width()
1399 Both are replicas of the corresponding TurtleScreen methods.
1406 Excursus about the use of compound shapes
1407 -----------------------------------------
1409 To use compound turtle shapes, which consist of several polygons of different
1410 color, you must use the helper class :class:`Shape` explicitly as described
1413 1. Create an empty Shape object of type "compound".
1414 2. Add as many components to this object as desired, using the
1415 :meth:`addcomponent` method.
1421 >>> s = Shape("compound")
1422 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1423 >>> s.addcomponent(poly1, "red", "blue")
1424 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1425 >>> s.addcomponent(poly2, "blue", "red")
1427 3. Now add the Shape to the Screen's shapelist and use it:
1431 >>> register_shape("myshape", s)
1432 >>> shape("myshape")
1437 The :class:`Shape` class is used internally by the :func:`register_shape`
1438 method in different ways. The application programmer has to deal with the
1439 Shape class *only* when using compound shapes like shown above!
1442 Methods of TurtleScreen/Screen and corresponding functions
1443 ==========================================================
1445 Most of the examples in this section refer to a TurtleScreen instance called
1451 >>> screen = Screen()
1456 .. function:: bgcolor(*args)
1458 :param args: a color string or three numbers in the range 0..colormode or a
1459 3-tuple of such numbers
1462 Set or return background color of the TurtleScreen.
1466 >>> screen.bgcolor("orange")
1467 >>> screen.bgcolor()
1469 >>> screen.bgcolor("#800080")
1470 >>> screen.bgcolor()
1474 .. function:: bgpic(picname=None)
1476 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
1478 Set background image or return name of current backgroundimage. If *picname*
1479 is a filename, set the corresponding image as background. If *picname* is
1480 ``"nopic"``, delete background image, if present. If *picname* is ``None``,
1481 return the filename of the current backgroundimage. ::
1485 >>> screen.bgpic("landscape.gif")
1490 .. function:: clear()
1493 Delete all drawings and all turtles from the TurtleScreen. Reset the now
1494 empty TurtleScreen to its initial state: white background, no background
1495 image, no event bindings and tracing on.
1498 This TurtleScreen method is available as a global function only under the
1499 name ``clearscreen``. The global function ``clear`` is another one
1500 derived from the Turtle method ``clear``.
1503 .. function:: reset()
1506 Reset all Turtles on the Screen to their initial state.
1509 This TurtleScreen method is available as a global function only under the
1510 name ``resetscreen``. The global function ``reset`` is another one
1511 derived from the Turtle method ``reset``.
1514 .. function:: screensize(canvwidth=None, canvheight=None, bg=None)
1516 :param canvwidth: positive integer, new width of canvas in pixels
1517 :param canvheight: positive integer, new height of canvas in pixels
1518 :param bg: colorstring or color-tuple, new background color
1520 If no arguments are given, return current (canvaswidth, canvasheight). Else
1521 resize the canvas the turtles are drawing on. Do not alter the drawing
1522 window. To observe hidden parts of the canvas, use the scrollbars. With this
1523 method, one can make visible those parts of a drawing which were outside the
1526 >>> screen.screensize()
1528 >>> screen.screensize(2000,1500)
1529 >>> screen.screensize()
1532 e.g. to search for an erroneously escaped turtle ;-)
1535 .. function:: setworldcoordinates(llx, lly, urx, ury)
1537 :param llx: a number, x-coordinate of lower left corner of canvas
1538 :param lly: a number, y-coordinate of lower left corner of canvas
1539 :param urx: a number, x-coordinate of upper right corner of canvas
1540 :param ury: a number, y-coordinate of upper right corner of canvas
1542 Set up user-defined coordinate system and switch to mode "world" if
1543 necessary. This performs a ``screen.reset()``. If mode "world" is already
1544 active, all drawings are redrawn according to the new coordinates.
1546 **ATTENTION**: in user-defined coordinate systems angles may appear
1552 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1553 >>> for _ in range(72):
1556 >>> for _ in range(8):
1557 ... left(45); fd(2) # a regular octagon
1563 >>> for t in turtles():
1570 .. function:: delay(delay=None)
1572 :param delay: positive integer
1574 Set or return the drawing *delay* in milliseconds. (This is approximately
1575 the time interval between two consecutive canvas updates.) The longer the
1576 drawing delay, the slower the animation.
1589 .. function:: tracer(n=None, delay=None)
1591 :param n: nonnegative integer
1592 :param delay: nonnegative integer
1594 Turn turtle animation on/off and set delay for update drawings. If *n* is
1595 given, only each n-th regular screen update is really performed. (Can be
1596 used to accelerate the drawing of complex graphics.) Second argument sets
1597 delay value (see :func:`delay`).
1601 >>> screen.tracer(8, 25)
1603 >>> for i in range(200):
1609 .. function:: update()
1611 Perform a TurtleScreen update. To be used when tracer is turned off.
1613 See also the RawTurtle/Turtle method :func:`speed`.
1619 .. function:: listen(xdummy=None, ydummy=None)
1621 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1622 are provided in order to be able to pass :func:`listen` to the onclick method.
1625 .. function:: onkey(fun, key)
1627 :param fun: a function with no arguments or ``None``
1628 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1630 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1631 are removed. Remark: in order to be able to register key-events, TurtleScreen
1632 must have the focus. (See method :func:`listen`.)
1640 >>> screen.onkey(f, "Up")
1644 .. function:: onclick(fun, btn=1, add=None)
1645 onscreenclick(fun, btn=1, add=None)
1647 :param fun: a function with two arguments which will be called with the
1648 coordinates of the clicked point on the canvas
1649 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1650 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1651 added, otherwise it will replace a former binding
1653 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1654 existing bindings are removed.
1656 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1661 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1662 >>> # make the turtle move to the clicked point.
1663 >>> screen.onclick(None) # remove event binding again
1666 This TurtleScreen method is available as a global function only under the
1667 name ``onscreenclick``. The global function ``onclick`` is another one
1668 derived from the Turtle method ``onclick``.
1671 .. function:: ontimer(fun, t=0)
1673 :param fun: a function with no arguments
1674 :param t: a number >= 0
1676 Install a timer that calls *fun* after *t* milliseconds.
1685 ... screen.ontimer(f, 250)
1686 >>> f() ### makes the turtle march around
1690 Settings and special methods
1691 ----------------------------
1693 .. function:: mode(mode=None)
1695 :param mode: one of the strings "standard", "logo" or "world"
1697 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1698 is not given, current mode is returned.
1700 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1701 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1702 "world coordinates". **Attention**: in this mode angles appear distorted if
1703 ``x/y`` unit-ratio doesn't equal 1.
1705 ============ ========================= ===================
1706 Mode Initial turtle heading positive angles
1707 ============ ========================= ===================
1708 "standard" to the right (east) counterclockwise
1709 "logo" upward (north) clockwise
1710 ============ ========================= ===================
1714 >>> mode("logo") # resets turtle heading to north
1719 .. function:: colormode(cmode=None)
1721 :param cmode: one of the values 1.0 or 255
1723 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1724 values of color triples have to be in the range 0..\ *cmode*.
1728 >>> screen.colormode(1)
1729 >>> turtle.pencolor(240, 160, 80)
1730 Traceback (most recent call last):
1732 TurtleGraphicsError: bad color sequence: (240, 160, 80)
1733 >>> screen.colormode()
1735 >>> screen.colormode(255)
1736 >>> screen.colormode()
1738 >>> turtle.pencolor(240,160,80)
1741 .. function:: getcanvas()
1743 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1744 do with a Tkinter Canvas.
1748 >>> cv = screen.getcanvas()
1750 <turtle.ScrolledCanvas instance at 0x...>
1753 .. function:: getshapes()
1755 Return a list of names of all currently available turtle shapes.
1759 >>> screen.getshapes()
1760 ['arrow', 'blank', 'circle', ..., 'turtle']
1763 .. function:: register_shape(name, shape=None)
1764 addshape(name, shape=None)
1766 There are three different ways to call this function:
1768 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
1769 corresponding image shape. ::
1771 >>> screen.register_shape("turtle.gif")
1774 Image shapes *do not* rotate when turning the turtle, so they do not
1775 display the heading of the turtle!
1777 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1778 coordinates: Install the corresponding polygon shape.
1782 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
1784 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
1785 object: Install the corresponding compound shape.
1787 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1788 shapes can be used by issuing the command ``shape(shapename)``.
1791 .. function:: turtles()
1793 Return the list of turtles on the screen.
1797 >>> for turtle in screen.turtles():
1798 ... turtle.color("red")
1801 .. function:: window_height()
1803 Return the height of the turtle window. ::
1805 >>> screen.window_height()
1809 .. function:: window_width()
1811 Return the width of the turtle window. ::
1813 >>> screen.window_width()
1819 Methods specific to Screen, not inherited from TurtleScreen
1820 -----------------------------------------------------------
1824 Shut the turtlegraphics window.
1827 .. function:: exitonclick()
1829 Bind bye() method to mouse clicks on the Screen.
1832 If the value "using_IDLE" in the configuration dictionary is ``False``
1833 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
1834 (no subprocess) is used, this value should be set to ``True`` in
1835 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1839 .. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
1841 Set the size and position of the main window. Default values of arguments
1842 are stored in the configuration dictionary and can be changed via a
1843 :file:`turtle.cfg` file.
1845 :param width: if an integer, a size in pixels, if a float, a fraction of the
1846 screen; default is 50% of screen
1847 :param height: if an integer, the height in pixels, if a float, a fraction of
1848 the screen; default is 75% of screen
1849 :param startx: if positive, starting position in pixels from the left
1850 edge of the screen, if negative from the right edge, if None,
1851 center window horizontally
1852 :param startx: if positive, starting position in pixels from the top
1853 edge of the screen, if negative from the bottom edge, if None,
1854 center window vertically
1858 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1859 >>> # sets window to 200x200 pixels, in upper left of screen
1860 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1861 >>> # sets window to 75% of screen by 50% of screen and centers
1864 .. function:: title(titlestring)
1866 :param titlestring: a string that is shown in the titlebar of the turtle
1869 Set title of turtle window to *titlestring*.
1873 >>> screen.title("Welcome to the turtle zoo!")
1876 The public classes of the module :mod:`turtle`
1877 ==============================================
1880 .. class:: RawTurtle(canvas)
1883 :param canvas: a :class:`Tkinter.Canvas`, a :class:`ScrolledCanvas` or a
1884 :class:`TurtleScreen`
1886 Create a turtle. The turtle has all methods described above as "methods of
1892 Subclass of RawTurtle, has the same interface but draws on a default
1893 :class:`Screen` object created automatically when needed for the first time.
1896 .. class:: TurtleScreen(cv)
1898 :param cv: a :class:`Tkinter.Canvas`
1900 Provides screen oriented methods like :func:`setbg` etc. that are described
1905 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
1908 .. class:: ScrolledCanvas(master)
1910 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
1911 a Tkinter-canvas with scrollbars added
1913 Used by class Screen, which thus automatically provides a ScrolledCanvas as
1914 playground for the turtles.
1916 .. class:: Shape(type_, data)
1918 :param type\_: one of the strings "polygon", "image", "compound"
1920 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
1924 =========== ===========
1926 =========== ===========
1927 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
1928 "image" an image (in this form only used internally!)
1929 "compound" ``None`` (a compound shape has to be constructed using the
1930 :meth:`addcomponent` method)
1931 =========== ===========
1933 .. method:: addcomponent(poly, fill, outline=None)
1935 :param poly: a polygon, i.e. a tuple of pairs of numbers
1936 :param fill: a color the *poly* will be filled with
1937 :param outline: a color for the poly's outline (if given)
1943 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
1944 >>> s = Shape("compound")
1945 >>> s.addcomponent(poly, "red", "blue")
1946 >>> # ... add more components and then use register_shape()
1948 See :ref:`compoundshapes`.
1951 .. class:: Vec2D(x, y)
1953 A two-dimensional vector class, used as a helper class for implementing
1954 turtle graphics. May be useful for turtle graphics programs too. Derived
1955 from tuple, so a vector is a tuple!
1957 Provides (for *a*, *b* vectors, *k* number):
1959 * ``a + b`` vector addition
1960 * ``a - b`` vector subtraction
1961 * ``a * b`` inner product
1962 * ``k * a`` and ``a * k`` multiplication with scalar
1963 * ``abs(a)`` absolute value of a
1964 * ``a.rotate(angle)`` rotation
1967 Help and configuration
1968 ======================
1973 The public methods of the Screen and Turtle classes are documented extensively
1974 via docstrings. So these can be used as online-help via the Python help
1977 - When using IDLE, tooltips show the signatures and first lines of the
1978 docstrings of typed in function-/method calls.
1980 - Calling :func:`help` on methods or functions displays the docstrings::
1982 >>> help(Screen.bgcolor)
1983 Help on method bgcolor in module turtle:
1985 bgcolor(self, *args) unbound turtle.Screen method
1986 Set or return backgroundcolor of the TurtleScreen.
1988 Arguments (if given): a color string or three numbers
1989 in the range 0..colormode or a 3-tuple of such numbers.
1992 >>> screen.bgcolor("orange")
1993 >>> screen.bgcolor()
1995 >>> screen.bgcolor(0.5,0,0.5)
1996 >>> screen.bgcolor()
1999 >>> help(Turtle.penup)
2000 Help on method penup in module turtle:
2002 penup(self) unbound turtle.Turtle method
2003 Pull the pen up -- no drawing when moving.
2005 Aliases: penup | pu | up
2011 - The docstrings of the functions which are derived from methods have a modified
2015 Help on function bgcolor in module turtle:
2018 Set or return backgroundcolor of the TurtleScreen.
2020 Arguments (if given): a color string or three numbers
2021 in the range 0..colormode or a 3-tuple of such numbers.
2025 >>> bgcolor("orange")
2028 >>> bgcolor(0.5,0,0.5)
2033 Help on function penup in module turtle:
2036 Pull the pen up -- no drawing when moving.
2038 Aliases: penup | pu | up
2045 These modified docstrings are created automatically together with the function
2046 definitions that are derived from the methods at import time.
2049 Translation of docstrings into different languages
2050 --------------------------------------------------
2052 There is a utility to create a dictionary the keys of which are the method names
2053 and the values of which are the docstrings of the public methods of the classes
2056 .. function:: write_docstringdict(filename="turtle_docstringdict")
2058 :param filename: a string, used as filename
2060 Create and write docstring-dictionary to a Python script with the given
2061 filename. This function has to be called explicitly (it is not used by the
2062 turtle graphics classes). The docstring dictionary will be written to the
2063 Python script :file:`{filename}.py`. It is intended to serve as a template
2064 for translation of the docstrings into different languages.
2066 If you (or your students) want to use :mod:`turtle` with online help in your
2067 native language, you have to translate the docstrings and save the resulting
2068 file as e.g. :file:`turtle_docstringdict_german.py`.
2070 If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2071 will be read in at import time and will replace the original English docstrings.
2073 At the time of this writing there are docstring dictionaries in German and in
2074 Italian. (Requests please to glingl@aon.at.)
2078 How to configure Screen and Turtles
2079 -----------------------------------
2081 The built-in default configuration mimics the appearance and behaviour of the
2082 old turtle module in order to retain best possible compatibility with it.
2084 If you want to use a different configuration which better reflects the features
2085 of this module or which better fits to your needs, e.g. for use in a classroom,
2086 you can prepare a configuration file ``turtle.cfg`` which will be read at import
2087 time and modify the configuration according to its settings.
2089 The built in configuration would correspond to the following turtle.cfg::
2100 undobuffersize = 1000
2104 resizemode = noresize
2107 exampleturtle = turtle
2108 examplescreen = screen
2109 title = Python Turtle Graphics
2112 Short explanation of selected entries:
2114 - The first four lines correspond to the arguments of the :meth:`Screen.setup`
2116 - Line 5 and 6 correspond to the arguments of the method
2117 :meth:`Screen.screensize`.
2118 - *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2119 info try ``help(shape)``.
2120 - If you want to use no fillcolor (i.e. make the turtle transparent), you have
2121 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
2123 - If you want to reflect the turtle its state, you have to use ``resizemode =
2125 - If you set e.g. ``language = italian`` the docstringdict
2126 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
2127 present on the import path, e.g. in the same directory as :mod:`turtle`.
2128 - The entries *exampleturtle* and *examplescreen* define the names of these
2129 objects as they occur in the docstrings. The transformation of
2130 method-docstrings to function-docstrings will delete these names from the
2132 - *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
2133 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
2136 There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2137 stored and an additional one in the current working directory. The latter will
2138 override the settings of the first one.
2140 The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can
2141 study it as an example and see its effects when running the demos (preferably
2142 not from within the demo-viewer).
2148 There is a set of demo scripts in the turtledemo directory located in the
2149 :file:`Demo/turtle` directory in the source distribution.
2153 - a set of 15 demo scripts demonstrating different features of the new module
2155 - a demo viewer :file:`turtleDemo.py` which can be used to view the sourcecode
2156 of the scripts and run them at the same time. 14 of the examples can be
2157 accessed via the Examples menu; all of them can also be run standalone.
2158 - The example :file:`turtledemo_two_canvases.py` demonstrates the simultaneous
2159 use of two canvases with the turtle module. Therefore it only can be run
2161 - There is a :file:`turtle.cfg` file in this directory, which also serves as an
2162 example for how to write and use such files.
2164 The demoscripts are:
2166 +----------------+------------------------------+-----------------------+
2167 | Name | Description | Features |
2168 +----------------+------------------------------+-----------------------+
2169 | bytedesign | complex classical | :func:`tracer`, delay,|
2170 | | turtlegraphics pattern | :func:`update` |
2171 +----------------+------------------------------+-----------------------+
2172 | chaos | graphs verhust dynamics, | world coordinates |
2173 | | proves that you must not | |
2174 | | trust computers' computations| |
2175 +----------------+------------------------------+-----------------------+
2176 | clock | analog clock showing time | turtles as clock's |
2177 | | of your computer | hands, ontimer |
2178 +----------------+------------------------------+-----------------------+
2179 | colormixer | experiment with r, g, b | :func:`ondrag` |
2180 +----------------+------------------------------+-----------------------+
2181 | fractalcurves | Hilbert & Koch curves | recursion |
2182 +----------------+------------------------------+-----------------------+
2183 | lindenmayer | ethnomathematics | L-System |
2184 | | (indian kolams) | |
2185 +----------------+------------------------------+-----------------------+
2186 | minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
2187 | | | as Hanoi discs |
2188 | | | (shape, shapesize) |
2189 +----------------+------------------------------+-----------------------+
2190 | paint | super minimalistic | :func:`onclick` |
2191 | | drawing program | |
2192 +----------------+------------------------------+-----------------------+
2193 | peace | elementary | turtle: appearance |
2194 | | | and animation |
2195 +----------------+------------------------------+-----------------------+
2196 | penrose | aperiodic tiling with | :func:`stamp` |
2197 | | kites and darts | |
2198 +----------------+------------------------------+-----------------------+
2199 | planet_and_moon| simulation of | compound shapes, |
2200 | | gravitational system | :class:`Vec2D` |
2201 +----------------+------------------------------+-----------------------+
2202 | tree | a (graphical) breadth | :func:`clone` |
2203 | | first tree (using generators)| |
2204 +----------------+------------------------------+-----------------------+
2205 | wikipedia | a pattern from the wikipedia | :func:`clone`, |
2206 | | article on turtle graphics | :func:`undo` |
2207 +----------------+------------------------------+-----------------------+
2208 | yingyang | another elementary example | :func:`circle` |
2209 +----------------+------------------------------+-----------------------+
2216 >>> for turtle in turtles():
2219 >>> turtle.goto(-200,25)
2220 >>> turtle.pendown()
2221 >>> turtle.write("No one expects the Spanish Inquisition!",
2222 ... font=("Arial", 20, "normal"))
2224 >>> turtle.goto(-100,-50)
2225 >>> turtle.pendown()
2226 >>> turtle.write("Our two chief Turtles are...",
2227 ... font=("Arial", 16, "normal"))
2229 >>> turtle.goto(-450,-75)
2230 >>> turtle.write(str(turtles()))