| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 |  | 
 | 2 | :mod:`turtle` --- Turtle graphics for Tk | 
 | 3 | ======================================== | 
 | 4 |  | 
 | 5 | .. module:: turtle | 
 | 6 |    :platform: Tk | 
 | 7 |    :synopsis: An environment for turtle graphics. | 
 | 8 | .. moduleauthor:: Guido van Rossum <guido@python.org> | 
 | 9 |  | 
 | 10 |  | 
 | 11 | .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il> | 
 | 12 |  | 
 | 13 |  | 
 | 14 | The :mod:`turtle` module provides turtle graphics primitives, in both an | 
 | 15 | object-oriented and procedure-oriented ways. Because it uses :mod:`Tkinter` for | 
 | 16 | the underlying graphics, it needs a version of python installed with Tk support. | 
 | 17 |  | 
 | 18 | The procedural interface uses a pen and a canvas which are automagically created | 
 | 19 | when any of the functions are called. | 
 | 20 |  | 
 | 21 | The :mod:`turtle` module defines the following functions: | 
 | 22 |  | 
 | 23 |  | 
 | 24 | .. function:: degrees() | 
 | 25 |  | 
 | 26 |    Set angle measurement units to degrees. | 
 | 27 |  | 
 | 28 |  | 
 | 29 | .. function:: radians() | 
 | 30 |  | 
 | 31 |    Set angle measurement units to radians. | 
 | 32 |  | 
 | 33 |  | 
 | 34 | .. function:: setup(**kwargs) | 
 | 35 |  | 
 | 36 |    Sets the size and position of the main window.  Keywords are: | 
 | 37 |  | 
 | 38 |    * ``width``: either a size in pixels or a fraction of the screen. The default is | 
 | 39 |      50% of the screen. | 
 | 40 |  | 
 | 41 |    * ``height``: either a size in pixels or a fraction of the screen. The default | 
 | 42 |      is 50% of the screen. | 
 | 43 |  | 
 | 44 |    * ``startx``: starting position in pixels from the left edge of the screen. | 
 | 45 |      ``None`` is the default value and  centers the window horizontally on screen. | 
 | 46 |  | 
 | 47 |    * ``starty``: starting position in pixels from the top edge of the screen. | 
 | 48 |      ``None`` is the default value and  centers the window vertically on screen. | 
 | 49 |  | 
 | 50 |    Examples:: | 
 | 51 |  | 
 | 52 |       # Uses default geometry: 50% x 50% of screen, centered. | 
 | 53 |       setup()   | 
 | 54 |  | 
 | 55 |       # Sets window to 200x200 pixels, in upper left of screen | 
 | 56 |       setup (width=200, height=200, startx=0, starty=0) | 
 | 57 |  | 
 | 58 |       # Sets window to 75% of screen by 50% of screen, and centers it. | 
 | 59 |       setup(width=.75, height=0.5, startx=None, starty=None) | 
 | 60 |  | 
 | 61 |  | 
 | 62 | .. function:: title(title_str) | 
 | 63 |  | 
 | 64 |    Set the window's title to *title*. | 
 | 65 |  | 
 | 66 |  | 
 | 67 | .. function:: done() | 
 | 68 |  | 
 | 69 |    Enters the Tk main loop.  The window will continue to  be displayed until the | 
 | 70 |    user closes it or the process is killed. | 
 | 71 |  | 
 | 72 |  | 
 | 73 | .. function:: reset() | 
 | 74 |  | 
 | 75 |    Clear the screen, re-center the pen, and set variables to the default values. | 
 | 76 |  | 
 | 77 |  | 
 | 78 | .. function:: clear() | 
 | 79 |  | 
 | 80 |    Clear the screen. | 
 | 81 |  | 
 | 82 |  | 
 | 83 | .. function:: tracer(flag) | 
 | 84 |  | 
 | 85 |    Set tracing on/off (according to whether flag is true or not). Tracing means | 
 | 86 |    line are drawn more slowly, with an animation of an arrow along the  line. | 
 | 87 |  | 
 | 88 |  | 
 | 89 | .. function:: speed(speed) | 
 | 90 |  | 
 | 91 |    Set the speed of the turtle. Valid values for the parameter *speed* are | 
 | 92 |    ``'fastest'`` (no delay), ``'fast'``, (delay 5ms), ``'normal'`` (delay 10ms), | 
 | 93 |    ``'slow'`` (delay 15ms), and ``'slowest'`` (delay 20ms). | 
 | 94 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 |  | 
 | 96 | .. function:: delay(delay) | 
 | 97 |  | 
 | 98 |    Set the speed of the turtle to *delay*, which is given in ms. | 
 | 99 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 |  | 
 | 101 | .. function:: forward(distance) | 
 | 102 |  | 
 | 103 |    Go forward *distance* steps. | 
 | 104 |  | 
 | 105 |  | 
 | 106 | .. function:: backward(distance) | 
 | 107 |  | 
 | 108 |    Go backward *distance* steps. | 
 | 109 |  | 
 | 110 |  | 
 | 111 | .. function:: left(angle) | 
 | 112 |  | 
 | 113 |    Turn left *angle* units. Units are by default degrees, but can be set via the | 
 | 114 |    :func:`degrees` and :func:`radians` functions. | 
 | 115 |  | 
 | 116 |  | 
 | 117 | .. function:: right(angle) | 
 | 118 |  | 
 | 119 |    Turn right *angle* units. Units are by default degrees, but can be set via the | 
 | 120 |    :func:`degrees` and :func:`radians` functions. | 
 | 121 |  | 
 | 122 |  | 
 | 123 | .. function:: up() | 
 | 124 |  | 
 | 125 |    Move the pen up --- stop drawing. | 
 | 126 |  | 
 | 127 |  | 
 | 128 | .. function:: down() | 
 | 129 |  | 
 | 130 |    Move the pen down --- draw when moving. | 
 | 131 |  | 
 | 132 |  | 
 | 133 | .. function:: width(width) | 
 | 134 |  | 
 | 135 |    Set the line width to *width*. | 
 | 136 |  | 
 | 137 |  | 
 | 138 | .. function:: color(s) | 
 | 139 |               color((r, g, b)) | 
 | 140 |               color(r, g, b) | 
 | 141 |  | 
 | 142 |    Set the pen color.  In the first form, the color is specified as a Tk color | 
 | 143 |    specification as a string.  The second form specifies the color as a tuple of | 
 | 144 |    the RGB values, each in the range [0..1].  For the third form, the color is | 
 | 145 |    specified giving the RGB values as three separate parameters (each in the range | 
 | 146 |    [0..1]). | 
 | 147 |  | 
 | 148 |  | 
 | 149 | .. function:: write(text[, move]) | 
 | 150 |  | 
 | 151 |    Write *text* at the current pen position. If *move* is true, the pen is moved to | 
 | 152 |    the bottom-right corner of the text. By default, *move* is false. | 
 | 153 |  | 
 | 154 |  | 
 | 155 | .. function:: fill(flag) | 
 | 156 |  | 
 | 157 |    The complete specifications are rather complex, but the recommended  usage is: | 
 | 158 |    call ``fill(1)`` before drawing a path you want to fill, and call ``fill(0)`` | 
 | 159 |    when you finish to draw the path. | 
 | 160 |  | 
 | 161 |  | 
 | 162 | .. function:: begin_fill() | 
 | 163 |  | 
 | 164 |    Switch turtle into filling mode;  Must eventually be followed by a corresponding | 
 | 165 |    end_fill() call. Otherwise it will be ignored. | 
 | 166 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 |  | 
 | 168 | .. function:: end_fill() | 
 | 169 |  | 
 | 170 |    End filling mode, and fill the shape; equivalent to ``fill(0)``. | 
 | 171 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 |  | 
 | 173 | .. function:: circle(radius[, extent]) | 
 | 174 |  | 
 | 175 |    Draw a circle with radius *radius* whose center-point is *radius* units left of | 
 | 176 |    the turtle. *extent* determines which part of a circle is drawn: if not given it | 
 | 177 |    defaults to a full circle. | 
 | 178 |  | 
 | 179 |    If *extent* is not a full circle, one endpoint of the arc is the current pen | 
 | 180 |    position. The arc is drawn in a counter clockwise direction if *radius* is | 
 | 181 |    positive, otherwise in a clockwise direction.  In the process, the direction of | 
 | 182 |    the turtle is changed by the amount of the *extent*. | 
 | 183 |  | 
 | 184 |  | 
 | 185 | .. function:: goto(x, y) | 
 | 186 |               goto((x, y)) | 
 | 187 |  | 
 | 188 |    Go to co-ordinates *x*, *y*.  The co-ordinates may be specified either as two | 
 | 189 |    separate arguments or as a 2-tuple. | 
 | 190 |  | 
 | 191 |  | 
 | 192 | .. function:: towards(x, y) | 
 | 193 |  | 
 | 194 |    Return the angle of the line from the turtle's position to the point *x*, *y*. | 
 | 195 |    The co-ordinates may be specified either as two separate arguments, as a | 
 | 196 |    2-tuple, or as another pen object. | 
 | 197 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 198 |  | 
 | 199 | .. function:: heading() | 
 | 200 |  | 
 | 201 |    Return the current orientation of the turtle. | 
 | 202 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 |  | 
 | 204 | .. function:: setheading(angle) | 
 | 205 |  | 
 | 206 |    Set the orientation of the turtle to *angle*. | 
 | 207 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 |  | 
 | 209 | .. function:: position() | 
 | 210 |  | 
 | 211 |    Return the current location of the turtle as an ``(x,y)`` pair. | 
 | 212 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 213 |  | 
 | 214 | .. function:: setx(x) | 
 | 215 |  | 
 | 216 |    Set the x coordinate of the turtle to *x*. | 
 | 217 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 |  | 
 | 219 | .. function:: sety(y) | 
 | 220 |  | 
 | 221 |    Set the y coordinate of the turtle to *y*. | 
 | 222 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 |  | 
 | 224 | .. function:: window_width() | 
 | 225 |  | 
 | 226 |    Return the width of the canvas window. | 
 | 227 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 228 |  | 
 | 229 | .. function:: window_height() | 
 | 230 |  | 
 | 231 |    Return the height of the canvas window. | 
 | 232 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 |  | 
 | 234 | This module also does ``from math import *``, so see the documentation for the | 
 | 235 | :mod:`math` module for additional constants and functions useful for turtle | 
 | 236 | graphics. | 
 | 237 |  | 
 | 238 |  | 
 | 239 | .. function:: demo() | 
 | 240 |  | 
 | 241 |    Exercise the module a bit. | 
 | 242 |  | 
 | 243 |  | 
 | 244 | .. exception:: Error | 
 | 245 |  | 
 | 246 |    Exception raised on any error caught by this module. | 
 | 247 |  | 
 | 248 | For examples, see the code of the :func:`demo` function. | 
 | 249 |  | 
 | 250 | This module defines the following classes: | 
 | 251 |  | 
 | 252 |  | 
 | 253 | .. class:: Pen() | 
 | 254 |  | 
 | 255 |    Define a pen. All above functions can be called as a methods on the given pen. | 
 | 256 |    The constructor automatically creates a canvas do be drawn on. | 
 | 257 |  | 
 | 258 |  | 
 | 259 | .. class:: Turtle() | 
 | 260 |  | 
 | 261 |    Define a pen. This is essentially a synonym for ``Pen()``; :class:`Turtle` is an | 
 | 262 |    empty subclass of :class:`Pen`. | 
 | 263 |  | 
 | 264 |  | 
 | 265 | .. class:: RawPen(canvas) | 
 | 266 |  | 
 | 267 |    Define a pen which draws on a canvas *canvas*. This is useful if  you want to | 
 | 268 |    use the module to create graphics in a "real" program. | 
 | 269 |  | 
 | 270 |  | 
 | 271 | .. _pen-rawpen-objects: | 
 | 272 |  | 
 | 273 | Turtle, Pen and RawPen Objects | 
 | 274 | ------------------------------ | 
 | 275 |  | 
 | 276 | Most of the global functions available in the module are also available as | 
 | 277 | methods of the :class:`Turtle`, :class:`Pen` and :class:`RawPen` classes, | 
 | 278 | affecting only the state of the given pen. | 
 | 279 |  | 
 | 280 | The only method which is more powerful as a method is :func:`degrees`, which | 
 | 281 | takes an optional argument letting  you specify the number of units | 
 | 282 | corresponding to a full circle: | 
 | 283 |  | 
 | 284 |  | 
 | 285 | .. method:: Turtle.degrees([fullcircle]) | 
 | 286 |  | 
 | 287 |    *fullcircle* is by default 360. This can cause the pen to have any angular units | 
| Christian Heimes | 2c18161 | 2007-12-17 20:04:13 +0000 | [diff] [blame] | 288 |    whatever: give *fullcircle* ``2*pi`` for radians, or 400 for gradians. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 |  |