Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1 | ======================================== |
| 2 | :mod:`turtle` --- Turtle graphics for Tk |
| 3 | ======================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 4 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame^] | 5 | .. module:: tkinter.turtle |
| 6 | :synopsis: Turtle graphics for Tk |
| 7 | .. sectionauthor:: Gregor Lingl <gregor.lingl@aon.at> |
| 8 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 9 | Introduction |
| 10 | ============ |
| 11 | |
| 12 | Turtle graphics is a popular way for introducing programming to kids. It was |
| 13 | part of the original Logo programming language developed by Wally Feurzig and |
| 14 | Seymour Papert in 1966. |
| 15 | |
| 16 | Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it the |
| 17 | command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the |
| 18 | direction it is facing, drawing a line as it moves. Give it the command |
| 19 | ``turtle.left(25)``, and it rotates in-place 25 degrees clockwise. |
| 20 | |
| 21 | By combining together these and similar commands, intricate shapes and pictures |
| 22 | can easily be drawn. |
| 23 | |
| 24 | The :mod:`turtle` module is an extended reimplementation of the same-named |
| 25 | module from the Python standard distribution up to version Python 2.5. |
| 26 | |
| 27 | It tries to keep the merits of the old turtle module and to be (nearly) 100% |
| 28 | compatible with it. This means in the first place to enable the learning |
| 29 | programmer to use all the commands, classes and methods interactively when using |
| 30 | the module from within IDLE run with the ``-n`` switch. |
| 31 | |
| 32 | The turtle module provides turtle graphics primitives, in both object-oriented |
| 33 | and procedure-oriented ways. Because it uses :mod:`Tkinter` for the underlying |
| 34 | graphics, it needs a version of python installed with Tk support. |
| 35 | |
| 36 | The object-oriented interface uses essentially two+two classes: |
| 37 | |
| 38 | 1. The :class:`TurtleScreen` class defines graphics windows as a playground for |
| 39 | the drawing turtles. Its constructor needs a :class:`Tkinter.Canvas` or a |
| 40 | :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is |
| 41 | used as part of some application. |
| 42 | |
| 43 | Derived from :class:`TurtleScreen` is the subclass :class:`Screen`. Screen |
| 44 | is implemented as sort of singleton, so there can exist only one instance of |
| 45 | Screen at a time. It should be used when :mod:`turtle` is used as a |
| 46 | standalone tool for doing graphics. |
| 47 | |
| 48 | All methods of TurtleScreen/Screen also exist as functions, i.e. as part of |
| 49 | the procedure-oriented interface. |
| 50 | |
| 51 | 2. :class:`RawTurtle` (alias: :class:`RawPen`) defines Turtle objects which draw |
| 52 | on a :class:`TurtleScreen`. Its constructor needs a Canvas, ScrolledCanvas |
| 53 | or TurtleScreen as argument, so the RawTurtle objects know where to draw. |
| 54 | |
| 55 | Derived from RawTurtle is the subclass :class:`Turtle` (alias: :class:`Pen`), |
| 56 | which draws on "the" :class:`Screen` - instance which is automatically |
| 57 | created, if not already present. |
| 58 | |
| 59 | All methods of RawTurtle/Turtle also exist as functions, i.e. part of the |
| 60 | procedure-oriented interface. |
| 61 | |
| 62 | The procedural interface provides functions which are derived from the methods |
| 63 | of the classes :class:`Screen` and :class:`Turtle`. They have the same names as |
| 64 | the corresponding methods. A screen object is automativally created whenever a |
| 65 | function derived from a Screen method is called. An (unnamed) turtle object is |
| 66 | automatically created whenever any of the functions derived from a Turtle method |
| 67 | is called. |
| 68 | |
| 69 | To use multiple turtles an a screen one has to use the object-oriented interface. |
| 70 | |
| 71 | .. note:: |
| 72 | In the following documentation the argument list for functions is given. |
| 73 | Methods, of course, have the additional first argument *self* which is |
| 74 | omitted here. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
| 76 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 77 | Overview over available Turtle and Screen methods |
| 78 | ================================================= |
| 79 | |
| 80 | Turtle methods |
| 81 | -------------- |
| 82 | |
| 83 | Turtle motion |
| 84 | Move and draw |
| 85 | | :func:`forward` | :func:`fd` |
| 86 | | :func:`backward` | :func:`bk` | :func:`back` |
| 87 | | :func:`right` | :func:`rt` |
| 88 | | :func:`left` | :func:`lt` |
| 89 | | :func:`goto` | :func:`setpos` | :func:`setposition` |
| 90 | | :func:`setx` |
| 91 | | :func:`sety` |
| 92 | | :func:`setheading` | :func:`seth` |
| 93 | | :func:`home` |
| 94 | | :func:`circle` |
| 95 | | :func:`dot` |
| 96 | | :func:`stamp` |
| 97 | | :func:`clearstamp` |
| 98 | | :func:`clearstamps` |
| 99 | | :func:`undo` |
| 100 | | :func:`speed` |
| 101 | |
| 102 | Tell Turtle's state |
| 103 | | :func:`position` | :func:`pos` |
| 104 | | :func:`towards` |
| 105 | | :func:`xcor` |
| 106 | | :func:`ycor` |
| 107 | | :func:`heading` |
| 108 | | :func:`distance` |
| 109 | |
| 110 | Setting and measurement |
| 111 | | :func:`degrees` |
| 112 | | :func:`radians` |
| 113 | |
| 114 | Pen control |
| 115 | Drawing state |
| 116 | | :func:`pendown` | :func:`pd` | :func:`down` |
| 117 | | :func:`penup` | :func:`pu` | :func:`up` |
| 118 | | :func:`pensize` | :func:`width` |
| 119 | | :func:`pen` |
| 120 | | :func:`isdown` |
| 121 | |
| 122 | Color control |
| 123 | | :func:`color` |
| 124 | | :func:`pencolor` |
| 125 | | :func:`fillcolor` |
| 126 | |
| 127 | Filling |
| 128 | | :func:`filling` |
| 129 | | :func:`begin_fill` |
| 130 | | :func:`end_fill` |
| 131 | |
| 132 | More drawing control |
| 133 | | :func:`reset` |
| 134 | | :func:`clear` |
| 135 | | :func:`write` |
| 136 | |
| 137 | Turtle state |
| 138 | Visibility |
| 139 | | :func:`showturtle` | :func:`st` |
| 140 | | :func:`hideturtle` | :func:`ht` |
| 141 | | :func:`isvisible` |
| 142 | |
| 143 | Appearance |
| 144 | | :func:`shape` |
| 145 | | :func:`resizemode` |
| 146 | | :func:`shapesize` | :func:`turtlesize` |
| 147 | | :func:`settiltangle` |
| 148 | | :func:`tiltangle` |
| 149 | | :func:`tilt` |
| 150 | |
| 151 | Using events |
| 152 | | :func:`onclick` |
| 153 | | :func:`onrelease` |
| 154 | | :func:`ondrag` |
| 155 | |
| 156 | Special Turtle methods |
| 157 | | :func:`begin_poly` |
| 158 | | :func:`end_poly` |
| 159 | | :func:`get_poly` |
| 160 | | :func:`clone` |
| 161 | | :func:`getturtle` | :func:`getpen` |
| 162 | | :func:`getscreen` |
| 163 | | :func:`setundobuffer` |
| 164 | | :func:`undobufferentries` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
| 166 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 167 | Methods of TurtleScreen/Screen |
| 168 | ------------------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 169 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 170 | Window control |
| 171 | | :func:`bgcolor` |
| 172 | | :func:`bgpic` |
| 173 | | :func:`clear` | :func:`clearscreen` |
| 174 | | :func:`reset` | :func:`resetscreen` |
| 175 | | :func:`screensize` |
| 176 | | :func:`setworldcoordinates` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 178 | Animation control |
| 179 | | :func:`delay` |
| 180 | | :func:`tracer` |
| 181 | | :func:`update` |
| 182 | |
| 183 | Using screen events |
| 184 | | :func:`listen` |
| 185 | | :func:`onkey` |
| 186 | | :func:`onclick` | :func:`onscreenclick` |
| 187 | | :func:`ontimer` |
| 188 | |
| 189 | Settings and special methods |
| 190 | | :func:`mode` |
| 191 | | :func:`colormode` |
| 192 | | :func:`getcanvas` |
| 193 | | :func:`getshapes` |
| 194 | | :func:`register_shape` | :func:`addshape` |
| 195 | | :func:`turtles` |
| 196 | | :func:`window_height` |
| 197 | | :func:`window_width` |
| 198 | |
| 199 | Methods specific to Screen |
| 200 | | :func:`bye` |
| 201 | | :func:`exitonclick` |
| 202 | | :func:`setup` |
| 203 | | :func:`title` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 204 | |
| 205 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 206 | Methods of RawTurtle/Turtle and corresponding functions |
| 207 | ======================================================= |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 209 | Most of the examples in this section refer to a Turtle instance called |
| 210 | ``turtle``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 211 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 212 | Turtle motion |
| 213 | ------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 214 | |
| 215 | .. function:: forward(distance) |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 216 | fd(distance) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 218 | :param distance: a number (integer or float) |
| 219 | |
| 220 | Move the turtle forward by the specified *distance*, in the direction the |
| 221 | turtle is headed. |
| 222 | |
| 223 | >>> turtle.position() |
| 224 | (0.00, 0.00) |
| 225 | >>> turtle.forward(25) |
| 226 | >>> turtle.position() |
| 227 | (25.00,0.00) |
| 228 | >>> turtle.forward(-75) |
| 229 | >>> turtle.position() |
| 230 | (-50.00,0.00) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | |
| 232 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 233 | .. function:: back(distance) |
| 234 | bk(distance) |
| 235 | backward(distance) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 237 | :param distance: a number |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 238 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 239 | Move the turtle backward by *distance*, opposite to the direction the |
| 240 | turtle is headed. Do not change the turtle's heading. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 242 | >>> turtle.position() |
| 243 | (0.00, 0.00) |
| 244 | >>> turtle.backward(30) |
| 245 | >>> turtle.position() |
| 246 | (-30.00, 0.00) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 247 | |
| 248 | |
| 249 | .. function:: right(angle) |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 250 | rt(angle) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 252 | :param angle: a number (integer or float) |
| 253 | |
| 254 | Turn turtle right by *angle* units. (Units are by default degrees, but |
| 255 | can be set via the :func:`degrees` and :func:`radians` functions.) Angle |
| 256 | orientation depends on the turtle mode, see :func:`mode`. |
| 257 | |
| 258 | >>> turtle.heading() |
| 259 | 22.0 |
| 260 | >>> turtle.right(45) |
| 261 | >>> turtle.heading() |
| 262 | 337.0 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 263 | |
| 264 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 265 | .. function:: left(angle) |
| 266 | lt(angle) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 268 | :param angle: a number (integer or float) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 269 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 270 | Turn turtle left by *angle* units. (Units are by default degrees, but |
| 271 | can be set via the :func:`degrees` and :func:`radians` functions.) Angle |
| 272 | orientation depends on the turtle mode, see :func:`mode`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 273 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 274 | >>> turtle.heading() |
| 275 | 22.0 |
| 276 | >>> turtle.left(45) |
| 277 | >>> turtle.heading() |
| 278 | 67.0 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 279 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 280 | .. function:: goto(x, y=None) |
| 281 | setpos(x, y=None) |
| 282 | setposition(x, y=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 283 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 284 | :param x: a number or a pair/vector of numbers |
| 285 | :param y: a number or ``None`` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 286 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 287 | If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D` |
| 288 | (e.g. as returned by :func:`pos`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 289 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 290 | Move turtle to an absolute position. If the pen is down, draw line. Do |
| 291 | not change the turtle's orientation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 293 | >>> tp = turtle.pos() |
| 294 | >>> tp |
| 295 | (0.00, 0.00) |
| 296 | >>> turtle.setpos(60,30) |
| 297 | >>> turtle.pos() |
| 298 | (60.00,30.00) |
| 299 | >>> turtle.setpos((20,80)) |
| 300 | >>> turtle.pos() |
| 301 | (20.00,80.00) |
| 302 | >>> turtle.setpos(tp) |
| 303 | >>> turtle.pos() |
| 304 | (0.00,0.00) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 305 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 306 | |
| 307 | .. function:: setx(x) |
| 308 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 309 | :param x: a number (integer or float) |
| 310 | |
| 311 | Set the turtle's first coordinate to *x*, leave second coordinate |
| 312 | unchanged. |
| 313 | |
| 314 | >>> turtle.position() |
| 315 | (0.00, 240.00) |
| 316 | >>> turtle.setx(10) |
| 317 | >>> turtle.position() |
| 318 | (10.00, 240.00) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 319 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 320 | |
| 321 | .. function:: sety(y) |
| 322 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 323 | :param y: a number (integer or float) |
| 324 | |
| 325 | Set the turtle's first coordinate to *y*, leave second coordinate |
| 326 | unchanged. |
| 327 | |
| 328 | >>> turtle.position() |
| 329 | (0.00, 40.00) |
| 330 | >>> turtle.sety(-10) |
| 331 | >>> turtle.position() |
| 332 | (0.00, -10.00) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 333 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 334 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 335 | .. function:: setheading(to_angle) |
| 336 | seth(to_angle) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 338 | :param to_angle: a number (integer or float) |
| 339 | |
| 340 | Set the orientation of the turtle to *to_angle*. Here are some common |
| 341 | directions in degrees: |
| 342 | |
| 343 | =================== ==================== |
| 344 | standard mode logo mode |
| 345 | =================== ==================== |
| 346 | 0 - east 0 - north |
| 347 | 90 - north 90 - east |
| 348 | 180 - west 180 - south |
| 349 | 270 - south 270 - west |
| 350 | =================== ==================== |
| 351 | |
| 352 | >>> turtle.setheading(90) |
| 353 | >>> turtle.heading() |
| 354 | 90 |
| 355 | |
| 356 | |
| 357 | .. function:: home() |
| 358 | |
| 359 | Move turtle to the origin -- coordinates (0,0) -- and set its heading to |
| 360 | its start-orientation (which depends on the mode, see :func:`mode`). |
| 361 | |
| 362 | |
| 363 | .. function:: circle(radius, extent=None, steps=None) |
| 364 | |
| 365 | :param radius: a number |
| 366 | :param extent: a number (or ``None``) |
| 367 | :param steps: an integer (or ``None``) |
| 368 | |
| 369 | Draw a circle with given *radius*. The center is *radius* units left of |
| 370 | the turtle; *extent* -- an angle -- determines which part of the circle |
| 371 | is drawn. If *extent* is not given, draw the entire circle. If *extent* |
| 372 | is not a full circle, one endpoint of the arc is the current pen |
| 373 | position. Draw the arc in counterclockwise direction if *radius* is |
| 374 | positive, otherwise in clockwise direction. Finally the direction of the |
| 375 | turtle is changed by the amount of *extent*. |
| 376 | |
| 377 | As the circle is approximated by an inscribed regular polygon, *steps* |
| 378 | determines the number of steps to use. If not given, it will be |
| 379 | calculated automatically. May be used to draw regular polygons. |
| 380 | |
| 381 | >>> turtle.circle(50) |
| 382 | >>> turtle.circle(120, 180) # draw a semicircle |
| 383 | |
| 384 | |
| 385 | .. function:: dot(size=None, *color) |
| 386 | |
| 387 | :param size: an integer >= 1 (if given) |
| 388 | :param color: a colorstring or a numeric color tuple |
| 389 | |
| 390 | Draw a circular dot with diameter *size*, using *color*. If *size* is |
| 391 | not given, the maximum of pensize+4 and 2*pensize is used. |
| 392 | |
| 393 | >>> turtle.dot() |
| 394 | >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50) |
| 395 | |
| 396 | |
| 397 | .. function:: stamp() |
| 398 | |
| 399 | Stamp a copy of the turtle shape onto the canvas at the current turtle |
| 400 | position. Return a stamp_id for that stamp, which can be used to delete |
| 401 | it by calling ``clearstamp(stamp_id)``. |
| 402 | |
| 403 | >>> turtle.color("blue") |
| 404 | >>> turtle.stamp() |
| 405 | 13 |
| 406 | >>> turtle.fd(50) |
| 407 | |
| 408 | |
| 409 | .. function:: clearstamp(stampid) |
| 410 | |
| 411 | :param stampid: an integer, must be return value of previous |
| 412 | :func:`stamp` call |
| 413 | |
| 414 | Delete stamp with given *stampid*. |
| 415 | |
| 416 | >>> turtle.color("blue") |
| 417 | >>> astamp = turtle.stamp() |
| 418 | >>> turtle.fd(50) |
| 419 | >>> turtle.clearstamp(astamp) |
| 420 | |
| 421 | |
| 422 | .. function:: clearstamps(n=None) |
| 423 | |
| 424 | :param n: an integer (or ``None``) |
| 425 | |
| 426 | Delete all or first/last *n* of turtle's stamps. If *n* is None, delete |
| 427 | all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete |
| 428 | last *n* stamps. |
| 429 | |
| 430 | >>> for i in range(8): |
| 431 | ... turtle.stamp(); turtle.fd(30) |
| 432 | >>> turtle.clearstamps(2) |
| 433 | >>> turtle.clearstamps(-2) |
| 434 | >>> turtle.clearstamps() |
| 435 | |
| 436 | |
| 437 | .. function:: undo() |
| 438 | |
| 439 | Undo (repeatedly) the last turtle action(s). Number of available |
| 440 | undo actions is determined by the size of the undobuffer. |
| 441 | |
| 442 | >>> for i in range(4): |
| 443 | ... turtle.fd(50); turtle.lt(80) |
| 444 | ... |
| 445 | >>> for i in range(8): |
| 446 | ... turtle.undo() |
| 447 | |
| 448 | |
| 449 | .. function:: speed(speed=None) |
| 450 | |
| 451 | :param speed: an integer in the range 0..10 or a speedstring (see below) |
| 452 | |
| 453 | Set the turtle's speed to an integer value in the range 0..10. If no |
| 454 | argument is given, return current speed. |
| 455 | |
| 456 | If input is a number greater than 10 or smaller than 0.5, speed is set |
| 457 | to 0. Speedstrings are mapped to speedvalues as follows: |
| 458 | |
| 459 | * "fastest": 0 |
| 460 | * "fast": 10 |
| 461 | * "normal": 6 |
| 462 | * "slow": 3 |
| 463 | * "slowest": 1 |
| 464 | |
| 465 | Speeds from 1 to 10 enforce increasingly faster animation of line drawing |
| 466 | and turtle turning. |
| 467 | |
| 468 | Attention: *speed* = 0 means that *no* animation takes |
| 469 | place. forward/back makes turtle jump and likewise left/right make the |
| 470 | turtle turn instantly. |
| 471 | |
| 472 | >>> turtle.speed(3) |
| 473 | |
| 474 | |
| 475 | Tell Turtle's state |
| 476 | ------------------- |
| 477 | |
| 478 | .. function:: position() |
| 479 | pos() |
| 480 | |
| 481 | Return the turtle's current location (x,y) (as a :class:`Vec2D` vector). |
| 482 | |
| 483 | >>> turtle.pos() |
| 484 | (0.00, 240.00) |
| 485 | |
| 486 | |
| 487 | .. function:: towards(x, y=None) |
| 488 | |
| 489 | :param x: a number or a pair/vector of numbers or a turtle instance |
| 490 | :param y: a number if *x* is a number, else ``None`` |
| 491 | |
| 492 | Return the angle between the line from turtle position to position specified |
| 493 | by (x,y), the vector or the other turtle. This depends on the turtle's start |
| 494 | orientation which depends on the mode - "standard"/"world" or "logo"). |
| 495 | |
| 496 | >>> turtle.pos() |
| 497 | (10.00, 10.00) |
| 498 | >>> turtle.towards(0,0) |
| 499 | 225.0 |
| 500 | |
| 501 | |
| 502 | .. function:: xcor() |
| 503 | |
| 504 | Return the turtle's x coordinate. |
| 505 | |
| 506 | >>> reset() |
| 507 | >>> turtle.left(60) |
| 508 | >>> turtle.forward(100) |
| 509 | >>> print turtle.xcor() |
| 510 | 50.0 |
| 511 | |
| 512 | |
| 513 | .. function:: ycor() |
| 514 | |
| 515 | Return the turtle's y coordinate. |
| 516 | |
| 517 | >>> reset() |
| 518 | >>> turtle.left(60) |
| 519 | >>> turtle.forward(100) |
| 520 | >>> print turtle.ycor() |
| 521 | 86.6025403784 |
| 522 | |
| 523 | |
| 524 | .. function:: heading() |
| 525 | |
| 526 | Return the turtle's current heading (value depends on the turtle mode, see |
| 527 | :func:`mode`). |
| 528 | |
| 529 | >>> turtle.left(67) |
| 530 | >>> turtle.heading() |
| 531 | 67.0 |
| 532 | |
| 533 | |
| 534 | .. function:: distance(x, y=None) |
| 535 | |
| 536 | :param x: a number or a pair/vector of numbers or a turtle instance |
| 537 | :param y: a number if *x* is a number, else ``None`` |
| 538 | |
| 539 | Return the distance from the turtle to (x,y), the given vector, or the given |
| 540 | other turtle, in turtle step units. |
| 541 | |
| 542 | >>> turtle.pos() |
| 543 | (0.00, 0.00) |
| 544 | >>> turtle.distance(30,40) |
| 545 | 50.0 |
| 546 | >>> joe = Turtle() |
| 547 | >>> joe.forward(77) |
| 548 | >>> turtle.distance(joe) |
| 549 | 77.0 |
| 550 | |
| 551 | |
| 552 | Settings for measurement |
| 553 | ------------------------ |
| 554 | |
| 555 | .. function:: degrees(fullcircle=360.0) |
| 556 | |
| 557 | :param fullcircle: a number |
| 558 | |
| 559 | Set angle measurement units, i.e. set number of "degrees" for a full circle. |
| 560 | Default value is 360 degrees. |
| 561 | |
| 562 | >>> turtle.left(90) |
| 563 | >>> turtle.heading() |
| 564 | 90 |
| 565 | >>> turtle.degrees(400.0) # angle measurement in gon |
| 566 | >>> turtle.heading() |
| 567 | 100 |
| 568 | |
| 569 | |
| 570 | .. function:: radians() |
| 571 | |
| 572 | Set the angle measurement units to radians. Equivalent to |
| 573 | ``degrees(2*math.pi)``. |
| 574 | |
| 575 | >>> turtle.heading() |
| 576 | 90 |
| 577 | >>> turtle.radians() |
| 578 | >>> turtle.heading() |
| 579 | 1.5707963267948966 |
| 580 | |
| 581 | |
| 582 | Pen control |
| 583 | ----------- |
| 584 | |
| 585 | Drawing state |
| 586 | ~~~~~~~~~~~~~ |
| 587 | |
| 588 | .. function:: pendown() |
| 589 | pd() |
| 590 | down() |
| 591 | |
| 592 | Pull the pen down -- drawing when moving. |
| 593 | |
| 594 | |
| 595 | .. function:: penup() |
| 596 | pu() |
| 597 | up() |
| 598 | |
| 599 | Pull the pen up -- no drawing when moving. |
| 600 | |
| 601 | |
| 602 | .. function:: pensize(width=None) |
| 603 | width(width=None) |
| 604 | |
| 605 | :param width: a positive number |
| 606 | |
| 607 | Set the line thickness to *width* or return it. If resizemode is set to |
| 608 | "auto" and turtleshape is a polygon, that polygon is drawn with the same line |
| 609 | thickness. If no argument is given, the current pensize is returned. |
| 610 | |
| 611 | >>> turtle.pensize() |
| 612 | 1 |
| 613 | >>> turtle.pensize(10) # from here on lines of width 10 are drawn |
| 614 | |
| 615 | |
| 616 | .. function:: pen(pen=None, **pendict) |
| 617 | |
| 618 | :param pen: a dictionary with some or all of the below listed keys |
| 619 | :param pendict: one or more keyword-arguments with the below listed keys as keywords |
| 620 | |
| 621 | Return or set the pen's attributes in a "pen-dictionary" with the following |
| 622 | key/value pairs: |
| 623 | |
| 624 | * "shown": True/False |
| 625 | * "pendown": True/False |
| 626 | * "pencolor": color-string or color-tuple |
| 627 | * "fillcolor": color-string or color-tuple |
| 628 | * "pensize": positive number |
| 629 | * "speed": number in range 0..10 |
| 630 | * "resizemode": "auto" or "user" or "noresize" |
| 631 | * "stretchfactor": (positive number, positive number) |
| 632 | * "outline": positive number |
| 633 | * "tilt": number |
| 634 | |
| 635 | This dicionary can be used as argument for a subsequent call to :func:`pen` |
| 636 | to restore the former pen-state. Moreover one or more of these attributes |
| 637 | can be provided as keyword-arguments. This can be used to set several pen |
| 638 | attributes in one statement. |
| 639 | |
| 640 | >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10) |
| 641 | >>> turtle.pen() |
| 642 | {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, |
| 643 | 'pencolor': 'red', 'pendown': True, 'fillcolor': 'black', |
| 644 | 'stretchfactor': (1,1), 'speed': 3} |
| 645 | >>> penstate=turtle.pen() |
| 646 | >>> turtle.color("yellow","") |
| 647 | >>> turtle.penup() |
| 648 | >>> turtle.pen() |
| 649 | {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, |
| 650 | 'pencolor': 'yellow', 'pendown': False, 'fillcolor': '', |
| 651 | 'stretchfactor': (1,1), 'speed': 3} |
| 652 | >>> p.pen(penstate, fillcolor="green") |
| 653 | >>> p.pen() |
| 654 | {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1, |
| 655 | 'pencolor': 'red', 'pendown': True, 'fillcolor': 'green', |
| 656 | 'stretchfactor': (1,1), 'speed': 3} |
| 657 | |
| 658 | |
| 659 | .. function:: isdown() |
| 660 | |
| 661 | Return ``True`` if pen is down, ``False`` if it's up. |
| 662 | |
| 663 | >>> turtle.penup() |
| 664 | >>> turtle.isdown() |
| 665 | False |
| 666 | >>> turtle.pendown() |
| 667 | >>> turtle.isdown() |
| 668 | True |
| 669 | |
| 670 | |
| 671 | Color control |
| 672 | ~~~~~~~~~~~~~ |
| 673 | |
| 674 | .. function:: pencolor(*args) |
| 675 | |
| 676 | Return or set the pencolor. |
| 677 | |
| 678 | Four input formats are allowed: |
| 679 | |
| 680 | ``pencolor()`` |
| 681 | Return the current pencolor as color specification string, possibly in |
| 682 | hex-number format (see example). May be used as input to another |
| 683 | color/pencolor/fillcolor call. |
| 684 | |
| 685 | ``pencolor(colorstring)`` |
| 686 | Set pencolor to *colorstring*, which is a Tk color specification string, |
| 687 | such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``. |
| 688 | |
| 689 | ``pencolor((r, g, b))`` |
| 690 | Set pencolor to the RGB color represented by the tuple of *r*, *g*, and |
| 691 | *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where |
| 692 | colormode is either 1.0 or 255 (see :func:`colormode`). |
| 693 | |
| 694 | ``pencolor(r, g, b)`` |
| 695 | Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of |
| 696 | *r*, *g*, and *b* must be in the range 0..colormode. |
| 697 | |
| 698 | If turtleshape is a polygon, the outline of that polygon is drawn with the |
| 699 | newly set pencolor. |
| 700 | |
| 701 | >>> turtle.pencolor("brown") |
| 702 | >>> tup = (0.2, 0.8, 0.55) |
| 703 | >>> turtle.pencolor(tup) |
| 704 | >>> turtle.pencolor() |
| 705 | "#33cc8c" |
| 706 | |
| 707 | |
| 708 | .. function:: fillcolor(*args) |
| 709 | |
| 710 | Return or set the fillcolor. |
| 711 | |
| 712 | Four input formats are allowed: |
| 713 | |
| 714 | ``fillcolor()`` |
| 715 | Return the current fillcolor as color specification string, possibly in |
| 716 | hex-number format (see example). May be used as input to another |
| 717 | color/pencolor/fillcolor call. |
| 718 | |
| 719 | ``fillcolor(colorstring)`` |
| 720 | Set fillcolor to *colorstring*, which is a Tk color specification string, |
| 721 | such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``. |
| 722 | |
| 723 | ``fillcolor((r, g, b))`` |
| 724 | Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and |
| 725 | *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where |
| 726 | colormode is either 1.0 or 255 (see :func:`colormode`). |
| 727 | |
| 728 | ``fillcolor(r, g, b)`` |
| 729 | Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of |
| 730 | *r*, *g*, and *b* must be in the range 0..colormode. |
| 731 | |
| 732 | If turtleshape is a polygon, the interior of that polygon is drawn |
| 733 | with the newly set fillcolor. |
| 734 | |
| 735 | >>> turtle.fillcolor("violet") |
| 736 | >>> col = turtle.pencolor() |
| 737 | >>> turtle.fillcolor(col) |
| 738 | >>> turtle.fillcolor(0, .5, 0) |
| 739 | |
| 740 | |
| 741 | .. function:: color(*args) |
| 742 | |
| 743 | Return or set pencolor and fillcolor. |
| 744 | |
| 745 | Several input formats are allowed. They use 0 to 3 arguments as |
| 746 | follows: |
| 747 | |
| 748 | ``color()`` |
| 749 | Return the current pencolor and the current fillcolor as a pair of color |
| 750 | specification strings as returned by :func:`pencolor` and |
| 751 | :func:`fillcolor`. |
| 752 | |
| 753 | ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)`` |
| 754 | Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the |
| 755 | given value. |
| 756 | |
| 757 | ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))`` |
| 758 | Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)`` |
| 759 | and analogously if the other input format is used. |
| 760 | |
| 761 | If turtleshape is a polygon, outline and interior of that polygon is drawn |
| 762 | with the newly set colors. |
| 763 | |
| 764 | >>> turtle.color("red", "green") |
| 765 | >>> turtle.color() |
| 766 | ("red", "green") |
| 767 | >>> colormode(255) |
| 768 | >>> color((40, 80, 120), (160, 200, 240)) |
| 769 | >>> color() |
| 770 | ("#285078", "#a0c8f0") |
| 771 | |
| 772 | |
| 773 | See also: Screen method :func:`colormode`. |
| 774 | |
| 775 | |
| 776 | Filling |
| 777 | ~~~~~~~ |
| 778 | |
| 779 | .. function:: filling() |
| 780 | |
| 781 | Return fillstate (``True`` if filling, ``False`` else). |
| 782 | |
| 783 | >>> turtle.begin_fill() |
| 784 | >>> if turtle.filling(): |
| 785 | ... turtle.pensize(5) |
| 786 | else: |
| 787 | ... turtle.pensize(3) |
| 788 | |
| 789 | |
| 790 | .. function:: begin_fill() |
| 791 | |
| 792 | To be called just before drawing a shape to be filled. |
| 793 | |
| 794 | >>> turtle.color("black", "red") |
| 795 | >>> turtle.begin_fill() |
| 796 | >>> turtle.circle(60) |
| 797 | >>> turtle.end_fill() |
| 798 | |
| 799 | |
| 800 | .. function:: end_fill() |
| 801 | |
| 802 | Fill the shape drawn after the last call to :func:`begin_fill`. |
| 803 | |
| 804 | |
| 805 | More drawing control |
| 806 | ~~~~~~~~~~~~~~~~~~~~ |
| 807 | |
| 808 | .. function:: reset() |
| 809 | |
| 810 | Delete the turtle's drawings from the screen, re-center the turtle and set |
| 811 | variables to the default values. |
| 812 | |
| 813 | >>> turtle.position() |
| 814 | (0.00,-22.00) |
| 815 | >>> turtle.heading() |
| 816 | 100.0 |
| 817 | >>> turtle.reset() |
| 818 | >>> turtle.position() |
| 819 | (0.00,0.00) |
| 820 | >>> turtle.heading() |
| 821 | 0.0 |
| 822 | |
| 823 | |
| 824 | .. function:: clear() |
| 825 | |
| 826 | Delete the turtle's drawings from the screen. Do not move turtle. State and |
| 827 | position of the turtle as well as drawings of other turtles are not affected. |
| 828 | |
| 829 | |
| 830 | .. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal")) |
| 831 | |
| 832 | :param arg: object to be written to the TurtleScreen |
| 833 | :param move: True/False |
| 834 | :param align: one of the strings "left", "center" or right" |
| 835 | :param font: a triple (fontname, fontsize, fonttype) |
| 836 | |
| 837 | Write text - the string representation of *arg* - at the current turtle |
| 838 | position according to *align* ("left", "center" or right") and with the given |
| 839 | font. If *move* is True, the pen is moved to the bottom-right corner of the |
| 840 | text. By default, *move* is False. |
| 841 | |
| 842 | >>> turtle.write("Home = ", True, align="center") |
| 843 | >>> turtle.write((0,0), True) |
| 844 | |
| 845 | |
| 846 | Turtle state |
| 847 | ------------ |
| 848 | |
| 849 | Visibility |
| 850 | ~~~~~~~~~~ |
| 851 | |
| 852 | .. function:: showturtle() |
| 853 | st() |
| 854 | |
| 855 | Make the turtle visible. |
| 856 | |
| 857 | >>> turtle.hideturtle() |
| 858 | >>> turtle.showturtle() |
| 859 | |
| 860 | |
| 861 | .. function:: hideturtle() |
| 862 | ht() |
| 863 | |
| 864 | Make the turtle invisible. It's a good idea to do this while you're in the |
| 865 | middle of doing some complex drawing, because hiding the turtle speeds up the |
| 866 | drawing observably. |
| 867 | |
| 868 | >>> turtle.hideturtle() |
| 869 | |
| 870 | |
| 871 | .. function:: isvisible() |
| 872 | |
| 873 | Return True if the Turtle is shown, False if it's hidden. |
| 874 | |
| 875 | >>> turtle.hideturtle() |
| 876 | >>> print turtle.isvisible(): |
| 877 | False |
| 878 | |
| 879 | |
| 880 | Appearance |
| 881 | ~~~~~~~~~~ |
| 882 | |
| 883 | .. function:: shape(name=None) |
| 884 | |
| 885 | :param name: a string which is a valid shapename |
| 886 | |
| 887 | Set turtle shape to shape with given *name* or, if name is not given, return |
| 888 | name of current shape. Shape with *name* must exist in the TurtleScreen's |
| 889 | shape dictionary. Initially there are the following polygon shapes: "arrow", |
| 890 | "turtle", "circle", "square", "triangle", "classic". To learn about how to |
| 891 | deal with shapes see Screen method :func:`register_shape`. |
| 892 | |
| 893 | >>> turtle.shape() |
| 894 | "arrow" |
| 895 | >>> turtle.shape("turtle") |
| 896 | >>> turtle.shape() |
| 897 | "turtle" |
| 898 | |
| 899 | |
| 900 | .. function:: resizemode(rmode=None) |
| 901 | |
| 902 | :param rmode: one of the strings "auto", "user", "noresize" |
| 903 | |
| 904 | Set resizemode to one of the values: "auto", "user", "noresize". If *rmode* |
| 905 | is not given, return current resizemode. Different resizemodes have the |
| 906 | following effects: |
| 907 | |
| 908 | - "auto": adapts the appearance of the turtle corresponding to the value of pensize. |
| 909 | - "user": adapts the appearance of the turtle according to the values of |
| 910 | stretchfactor and outlinewidth (outline), which are set by |
| 911 | :func:`shapesize`. |
| 912 | - "noresize": no adaption of the turtle's appearance takes place. |
| 913 | |
| 914 | resizemode("user") is called by :func:`shapesize` when used with arguments. |
| 915 | |
| 916 | >>> turtle.resizemode("noresize") |
| 917 | >>> turtle.resizemode() |
| 918 | "noresize" |
| 919 | |
| 920 | |
| 921 | .. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None) |
| 922 | |
| 923 | :param stretch_wid: positive number |
| 924 | :param stretch_len: positive number |
| 925 | :param outline: positive number |
| 926 | |
| 927 | Return or set the pen's attributes x/y-stretchfactors and/or outline. Set |
| 928 | resizemode to "user". If and only if resizemode is set to "user", the turtle |
| 929 | will be displayed stretched according to its stretchfactors: *stretch_wid* is |
| 930 | stretchfactor perpendicular to its orientation, *stretch_len* is |
| 931 | stretchfactor in direction of its orientation, *outline* determines the width |
| 932 | of the shapes's outline. |
| 933 | |
| 934 | >>> turtle.resizemode("user") |
| 935 | >>> turtle.shapesize(5, 5, 12) |
| 936 | >>> turtle.shapesize(outline=8) |
| 937 | |
| 938 | |
| 939 | .. function:: tilt(angle) |
| 940 | |
| 941 | :param angle: a number |
| 942 | |
| 943 | Rotate the turtleshape by *angle* from its current tilt-angle, but do *not* |
| 944 | change the turtle's heading (direction of movement). |
| 945 | |
| 946 | >>> turtle.shape("circle") |
| 947 | >>> turtle.shapesize(5,2) |
| 948 | >>> turtle.tilt(30) |
| 949 | >>> turtle.fd(50) |
| 950 | >>> turtle.tilt(30) |
| 951 | >>> turtle.fd(50) |
| 952 | |
| 953 | |
| 954 | .. function:: settiltangle(angle) |
| 955 | |
| 956 | :param angle: a number |
| 957 | |
| 958 | Rotate the turtleshape to point in the direction specified by *angle*, |
| 959 | regardless of its current tilt-angle. *Do not* change the turtle's heading |
| 960 | (direction of movement). |
| 961 | |
| 962 | >>> turtle.shape("circle") |
| 963 | >>> turtle.shapesize(5,2) |
| 964 | >>> turtle.settiltangle(45) |
| 965 | >>> stamp() |
| 966 | >>> turtle.fd(50) |
| 967 | >>> turtle.settiltangle(-45) |
| 968 | >>> stamp() |
| 969 | >>> turtle.fd(50) |
| 970 | |
| 971 | |
| 972 | .. function:: tiltangle() |
| 973 | |
| 974 | Return the current tilt-angle, i.e. the angle between the orientation of the |
| 975 | turtleshape and the heading of the turtle (its direction of movement). |
| 976 | |
| 977 | >>> turtle.shape("circle") |
| 978 | >>> turtle.shapesize(5,2) |
| 979 | >>> turtle.tilt(45) |
| 980 | >>> turtle.tiltangle() |
| 981 | 45 |
| 982 | |
| 983 | |
| 984 | Using events |
| 985 | ------------ |
| 986 | |
| 987 | .. function:: onclick(fun, btn=1, add=None) |
| 988 | |
| 989 | :param fun: a function with two arguments which will be called with the |
| 990 | coordinates of the clicked point on the canvas |
| 991 | :param num: number of the mouse-button, defaults to 1 (left mouse button) |
| 992 | :param add: ``True`` or ``False`` -- if ``True``, a new binding will be |
| 993 | added, otherwise it will replace a former binding |
| 994 | |
| 995 | Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``, |
| 996 | existing bindings are removed. Example for the anonymous turtle, i.e. the |
| 997 | procedural way: |
| 998 | |
| 999 | >>> def turn(x, y): |
| 1000 | ... left(180) |
| 1001 | ... |
| 1002 | >>> onclick(turn) # Now clicking into the turtle will turn it. |
| 1003 | >>> onclick(None) # event-binding will be removed |
| 1004 | |
| 1005 | |
| 1006 | .. function:: onrelease(fun, btn=1, add=None) |
| 1007 | |
| 1008 | :param fun: a function with two arguments which will be called with the |
| 1009 | coordinates of the clicked point on the canvas |
| 1010 | :param num: number of the mouse-button, defaults to 1 (left mouse button) |
| 1011 | :param add: ``True`` or ``False`` -- if ``True``, a new binding will be |
| 1012 | added, otherwise it will replace a former binding |
| 1013 | |
| 1014 | Bind *fun* to mouse-button-release events on this turtle. If *fun* is |
| 1015 | ``None``, existing bindings are removed. |
| 1016 | |
| 1017 | >>> class MyTurtle(Turtle): |
| 1018 | ... def glow(self,x,y): |
| 1019 | ... self.fillcolor("red") |
| 1020 | ... def unglow(self,x,y): |
| 1021 | ... self.fillcolor("") |
| 1022 | ... |
| 1023 | >>> turtle = MyTurtle() |
| 1024 | >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red, |
| 1025 | >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent. |
| 1026 | |
| 1027 | |
| 1028 | .. function:: ondrag(fun, btn=1, add=None) |
| 1029 | |
| 1030 | :param fun: a function with two arguments which will be called with the |
| 1031 | coordinates of the clicked point on the canvas |
| 1032 | :param num: number of the mouse-button, defaults to 1 (left mouse button) |
| 1033 | :param add: ``True`` or ``False`` -- if ``True``, a new binding will be |
| 1034 | added, otherwise it will replace a former binding |
| 1035 | |
| 1036 | Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``, |
| 1037 | existing bindings are removed. |
| 1038 | |
| 1039 | Remark: Every sequence of mouse-move-events on a turtle is preceded by a |
| 1040 | mouse-click event on that turtle. |
| 1041 | |
| 1042 | >>> turtle.ondrag(turtle.goto) |
| 1043 | # Subsequently, clicking and dragging the Turtle will move it across |
| 1044 | # the screen thereby producing handdrawings (if pen is down). |
| 1045 | |
| 1046 | |
| 1047 | Special Turtle methods |
| 1048 | ---------------------- |
| 1049 | |
| 1050 | .. function:: begin_poly() |
| 1051 | |
| 1052 | Start recording the vertices of a polygon. Current turtle position is first |
| 1053 | vertex of polygon. |
| 1054 | |
| 1055 | |
| 1056 | .. function:: end_poly() |
| 1057 | |
| 1058 | Stop recording the vertices of a polygon. Current turtle position is last |
| 1059 | vertex of polygon. This will be connected with the first vertex. |
| 1060 | |
| 1061 | |
| 1062 | .. function:: get_poly() |
| 1063 | |
| 1064 | Return the last recorded polygon. |
| 1065 | |
| 1066 | >>> p = turtle.get_poly() |
| 1067 | >>> turtle.register_shape("myFavouriteShape", p) |
| 1068 | |
| 1069 | |
| 1070 | .. function:: clone() |
| 1071 | |
| 1072 | Create and return a clone of the turtle with same position, heading and |
| 1073 | turtle properties. |
| 1074 | |
| 1075 | >>> mick = Turtle() |
| 1076 | >>> joe = mick.clone() |
| 1077 | |
| 1078 | |
| 1079 | .. function:: getturtle() |
| 1080 | |
| 1081 | Return the Turtle object itself. Only reasonable use: as a function to |
| 1082 | return the "anonymous turtle": |
| 1083 | |
| 1084 | >>> pet = getturtle() |
| 1085 | >>> pet.fd(50) |
| 1086 | >>> pet |
| 1087 | <turtle.Turtle object at 0x01417350> |
| 1088 | >>> turtles() |
| 1089 | [<turtle.Turtle object at 0x01417350>] |
| 1090 | |
| 1091 | |
| 1092 | .. function:: getscreen() |
| 1093 | |
| 1094 | Return the :class:`TurtleScreen` object the turtle is drawing on. |
| 1095 | TurtleScreen methods can then be called for that object. |
| 1096 | |
| 1097 | >>> ts = turtle.getscreen() |
| 1098 | >>> ts |
| 1099 | <turtle.Screen object at 0x01417710> |
| 1100 | >>> ts.bgcolor("pink") |
| 1101 | |
| 1102 | |
| 1103 | .. function:: setundobuffer(size) |
| 1104 | |
| 1105 | :param size: an integer or ``None`` |
| 1106 | |
| 1107 | Set or disable undobuffer. If *size* is an integer an empty undobuffer of |
| 1108 | given size is installed. *size* gives the maximum number of turtle actions |
| 1109 | that can be undone by the :func:`undo` method/function. If *size* is |
| 1110 | ``None``, the undobuffer is disabled. |
| 1111 | |
| 1112 | >>> turtle.setundobuffer(42) |
| 1113 | |
| 1114 | |
| 1115 | .. function:: undobufferentries() |
| 1116 | |
| 1117 | Return number of entries in the undobuffer. |
| 1118 | |
| 1119 | >>> while undobufferentries(): |
| 1120 | ... undo() |
| 1121 | |
| 1122 | |
| 1123 | .. _compoundshapes: |
| 1124 | |
| 1125 | Excursus about the use of compound shapes |
| 1126 | ----------------------------------------- |
| 1127 | |
| 1128 | To use compound turtle shapes, which consist of several polygons of different |
| 1129 | color, you must use the helper class :class:`Shape` explicitly as described |
| 1130 | below: |
| 1131 | |
| 1132 | 1. Create an empty Shape object of type "compound". |
| 1133 | 2. Add as many components to this object as desired, using the |
| 1134 | :meth:`addcomponent` method. |
| 1135 | |
| 1136 | For example: |
| 1137 | |
| 1138 | >>> s = Shape("compound") |
| 1139 | >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5)) |
| 1140 | >>> s.addcomponent(poly1, "red", "blue") |
| 1141 | >>> poly2 = ((0,0),(10,-5),(-10,-5)) |
| 1142 | >>> s.addcomponent(poly2, "blue", "red") |
| 1143 | |
| 1144 | 3. Now add the Shape to the Screen's shapelist and use it: |
| 1145 | |
| 1146 | >>> register_shape("myshape", s) |
| 1147 | >>> shape("myshape") |
| 1148 | |
| 1149 | |
| 1150 | .. note:: |
| 1151 | |
| 1152 | The :class:`Shape` class is used internally by the :func:`register_shape` |
| 1153 | method in different ways. The application programmer has to deal with the |
| 1154 | Shape class *only* when using compound shapes like shown above! |
| 1155 | |
| 1156 | |
| 1157 | Methods of TurtleScreen/Screen and corresponding functions |
| 1158 | ========================================================== |
| 1159 | |
| 1160 | Most of the examples in this section refer to a TurtleScreen instance called |
| 1161 | ``screen``. |
| 1162 | |
| 1163 | |
| 1164 | Window control |
| 1165 | -------------- |
| 1166 | |
| 1167 | .. function:: bgcolor(*args) |
| 1168 | |
| 1169 | :param args: a color string or three numbers in the range 0..colormode or a |
| 1170 | 3-tuple of such numbers |
| 1171 | |
| 1172 | Set or return background color of the TurtleScreen. |
| 1173 | |
| 1174 | >>> screen.bgcolor("orange") |
| 1175 | >>> screen.bgcolor() |
| 1176 | "orange" |
| 1177 | >>> screen.bgcolor(0.5,0,0.5) |
| 1178 | >>> screen.bgcolor() |
| 1179 | "#800080" |
| 1180 | |
| 1181 | |
| 1182 | .. function:: bgpic(picname=None) |
| 1183 | |
| 1184 | :param picname: a string, name of a gif-file or ``"nopic"``, or ``None`` |
| 1185 | |
| 1186 | Set background image or return name of current backgroundimage. If *picname* |
| 1187 | is a filename, set the corresponding image as background. If *picname* is |
| 1188 | ``"nopic"``, delete background image, if present. If *picname* is ``None``, |
| 1189 | return the filename of the current backgroundimage. |
| 1190 | |
| 1191 | >>> screen.bgpic() |
| 1192 | "nopic" |
| 1193 | >>> screen.bgpic("landscape.gif") |
| 1194 | >>> screen.bgpic() |
| 1195 | "landscape.gif" |
| 1196 | |
| 1197 | |
| 1198 | .. function:: clear() |
| 1199 | clearscreen() |
| 1200 | |
| 1201 | Delete all drawings and all turtles from the TurtleScreen. Reset the now |
| 1202 | empty TurtleScreen to its initial state: white background, no background |
| 1203 | image, no event bindings and tracing on. |
| 1204 | |
| 1205 | .. note:: |
| 1206 | This TurtleScreen method is available as a global function only under the |
| 1207 | name ``clearscreen``. The global function ``clear`` is another one |
| 1208 | derived from the Turtle method ``clear``. |
| 1209 | |
| 1210 | |
| 1211 | .. function:: reset() |
| 1212 | resetscreen() |
| 1213 | |
| 1214 | Reset all Turtles on the Screen to their initial state. |
| 1215 | |
| 1216 | .. note:: |
| 1217 | This TurtleScreen method is available as a global function only under the |
| 1218 | name ``resetscreen``. The global function ``reset`` is another one |
| 1219 | derived from the Turtle method ``reset``. |
| 1220 | |
| 1221 | |
| 1222 | .. function:: screensize(canvwidth=None, canvheight=None, bg=None) |
| 1223 | |
| 1224 | :param canvwidth: positive integer, new width of canvas in pixels |
| 1225 | :param canvheight: positive integer, new height of canvas in pixels |
| 1226 | :param bg: colorstring or color-tupel, new background color |
| 1227 | |
| 1228 | If no arguments are given, return current (canvaswidth, canvasheight). Else |
| 1229 | resize the canvas the turtles are drawing on. Do not alter the drawing |
| 1230 | window. To observe hidden parts of the canvas, use the scrollbars. With this |
| 1231 | method, one can make visible those parts of a drawing which were outside the |
| 1232 | canvas before. |
| 1233 | |
| 1234 | >>> turtle.screensize(2000,1500) |
| 1235 | # e.g. to search for an erroneously escaped turtle ;-) |
| 1236 | |
| 1237 | |
| 1238 | .. function:: setworldcoordinates(llx, lly, urx, ury) |
| 1239 | |
| 1240 | :param llx: a number, x-coordinate of lower left corner of canvas |
| 1241 | :param lly: a number, y-coordinate of lower left corner of canvas |
| 1242 | :param urx: a number, x-coordinate of upper right corner of canvas |
| 1243 | :param ury: a number, y-coordinate of upper right corner of canvas |
| 1244 | |
| 1245 | Set up user-defined coordinate system and switch to mode "world" if |
| 1246 | necessary. This performs a ``screen.reset()``. If mode "world" is already |
| 1247 | active, all drawings are redrawn according to the new coordinates. |
| 1248 | |
| 1249 | **ATTENTION**: in user-defined coordinate systems angles may appear |
| 1250 | distorted. |
| 1251 | |
| 1252 | >>> screen.reset() |
| 1253 | >>> screen.setworldcoordinates(-50,-7.5,50,7.5) |
| 1254 | >>> for _ in range(72): |
| 1255 | ... left(10) |
| 1256 | ... |
| 1257 | >>> for _ in range(8): |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame^] | 1258 | ... left(45); fd(2) # a regular octagon |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1259 | |
| 1260 | |
| 1261 | Animation control |
| 1262 | ----------------- |
| 1263 | |
| 1264 | .. function:: delay(delay=None) |
| 1265 | |
| 1266 | :param delay: positive integer |
| 1267 | |
| 1268 | Set or return the drawing *delay* in milliseconds. (This is approximately |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame^] | 1269 | the time interval between two consecutive canvas updates.) The longer the |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1270 | drawing delay, the slower the animation. |
| 1271 | |
| 1272 | Optional argument: |
| 1273 | |
| 1274 | >>> screen.delay(15) |
| 1275 | >>> screen.delay() |
| 1276 | 15 |
| 1277 | |
| 1278 | |
| 1279 | .. function:: tracer(n=None, delay=None) |
| 1280 | |
| 1281 | :param n: nonnegative integer |
| 1282 | :param delay: nonnegative integer |
| 1283 | |
| 1284 | Turn turtle animation on/off and set delay for update drawings. If *n* is |
| 1285 | given, only each n-th regular screen update is really performed. (Can be |
| 1286 | used to accelerate the drawing of complex graphics.) Second argument sets |
| 1287 | delay value (see :func:`delay`). |
| 1288 | |
| 1289 | >>> screen.tracer(8, 25) |
| 1290 | >>> dist = 2 |
| 1291 | >>> for i in range(200): |
| 1292 | ... fd(dist) |
| 1293 | ... rt(90) |
| 1294 | ... dist += 2 |
| 1295 | |
| 1296 | |
| 1297 | .. function:: update() |
| 1298 | |
| 1299 | Perform a TurtleScreen update. To be used when tracer is turned off. |
| 1300 | |
| 1301 | See also the RawTurtle/Turtle method :func:`speed`. |
| 1302 | |
| 1303 | |
| 1304 | Using screen events |
| 1305 | ------------------- |
| 1306 | |
| 1307 | .. function:: listen(xdummy=None, ydummy=None) |
| 1308 | |
| 1309 | Set focus on TurtleScreen (in order to collect key-events). Dummy arguments |
| 1310 | are provided in order to be able to pass :func:`listen` to the onclick method. |
| 1311 | |
| 1312 | |
| 1313 | .. function:: onkey(fun, key) |
| 1314 | |
| 1315 | :param fun: a function with no arguments or ``None`` |
| 1316 | :param key: a string: key (e.g. "a") or key-symbol (e.g. "space") |
| 1317 | |
| 1318 | Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings |
| 1319 | are removed. Remark: in order to be able to register key-events, TurtleScreen |
| 1320 | must have the focus. (See method :func:`listen`.) |
| 1321 | |
| 1322 | >>> def f(): |
| 1323 | ... fd(50) |
| 1324 | ... lt(60) |
| 1325 | ... |
| 1326 | >>> screen.onkey(f, "Up") |
| 1327 | >>> screen.listen() |
| 1328 | |
| 1329 | |
| 1330 | .. function:: onclick(fun, btn=1, add=None) |
| 1331 | onscreenclick(fun, btn=1, add=None) |
| 1332 | |
| 1333 | :param fun: a function with two arguments which will be called with the |
| 1334 | coordinates of the clicked point on the canvas |
| 1335 | :param num: number of the mouse-button, defaults to 1 (left mouse button) |
| 1336 | :param add: ``True`` or ``False`` -- if ``True``, a new binding will be |
| 1337 | added, otherwise it will replace a former binding |
| 1338 | |
| 1339 | Bind *fun* to mouse-click events on this screen. If *fun* is ``None``, |
| 1340 | existing bindings are removed. |
| 1341 | |
| 1342 | Example for a TurtleScreen instance named ``screen`` and a Turtle instance |
| 1343 | named turtle: |
| 1344 | |
| 1345 | >>> screen.onclick(turtle.goto) |
| 1346 | # Subsequently clicking into the TurtleScreen will |
| 1347 | # make the turtle move to the clicked point. |
| 1348 | >>> screen.onclick(None) # remove event binding again |
| 1349 | |
| 1350 | .. note:: |
| 1351 | This TurtleScreen method is available as a global function only under the |
| 1352 | name ``onscreenclick``. The global function ``onclick`` is another one |
| 1353 | derived from the Turtle method ``onclick``. |
| 1354 | |
| 1355 | |
| 1356 | .. function:: ontimer(fun, t=0) |
| 1357 | |
| 1358 | :param fun: a function with no arguments |
| 1359 | :param t: a number >= 0 |
| 1360 | |
| 1361 | Install a timer that calls *fun* after *t* milliseconds. |
| 1362 | |
| 1363 | >>> running = True |
| 1364 | >>> def f(): |
| 1365 | if running: |
| 1366 | fd(50) |
| 1367 | lt(60) |
| 1368 | screen.ontimer(f, 250) |
| 1369 | >>> f() ### makes the turtle marching around |
| 1370 | >>> running = False |
| 1371 | |
| 1372 | |
| 1373 | Settings and special methods |
| 1374 | ---------------------------- |
| 1375 | |
| 1376 | .. function:: mode(mode=None) |
| 1377 | |
| 1378 | :param mode: one of the strings "standard", "logo" or "world" |
| 1379 | |
| 1380 | Set turtle mode ("standard", "logo" or "world") and perform reset. If mode |
| 1381 | is not given, current mode is returned. |
| 1382 | |
| 1383 | Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is |
| 1384 | compatible with most Logo turtle graphics. Mode "world" uses user-defined |
| 1385 | "world coordinates". **Attention**: in this mode angles appear distorted if |
| 1386 | ``x/y`` unit-ratio doesn't equal 1. |
| 1387 | |
| 1388 | ============ ========================= =================== |
| 1389 | Mode Initial turtle heading positive angles |
| 1390 | ============ ========================= =================== |
| 1391 | "standard" to the right (east) counterclockwise |
| 1392 | "logo" upward (north) clockwise |
| 1393 | ============ ========================= =================== |
| 1394 | |
| 1395 | >>> mode("logo") # resets turtle heading to north |
| 1396 | >>> mode() |
| 1397 | "logo" |
| 1398 | |
| 1399 | |
| 1400 | .. function:: colormode(cmode=None) |
| 1401 | |
| 1402 | :param cmode: one of the values 1.0 or 255 |
| 1403 | |
| 1404 | Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b* |
| 1405 | values of color triples have to be in the range 0..\ *cmode*. |
| 1406 | |
| 1407 | >>> screen.colormode() |
| 1408 | 1.0 |
| 1409 | >>> screen.colormode(255) |
| 1410 | >>> turtle.pencolor(240,160,80) |
| 1411 | |
| 1412 | |
| 1413 | .. function:: getcanvas() |
| 1414 | |
| 1415 | Return the Canvas of this TurtleScreen. Useful for insiders who know what to |
| 1416 | do with a Tkinter Canvas. |
| 1417 | |
| 1418 | >>> cv = screen.getcanvas() |
| 1419 | >>> cv |
| 1420 | <turtle.ScrolledCanvas instance at 0x010742D8> |
| 1421 | |
| 1422 | |
| 1423 | .. function:: getshapes() |
| 1424 | |
| 1425 | Return a list of names of all currently available turtle shapes. |
| 1426 | |
| 1427 | >>> screen.getshapes() |
| 1428 | ["arrow", "blank", "circle", ..., "turtle"] |
| 1429 | |
| 1430 | |
| 1431 | .. function:: register_shape(name, shape=None) |
| 1432 | addshape(name, shape=None) |
| 1433 | |
| 1434 | There are three different ways to call this function: |
| 1435 | |
| 1436 | (1) *name* is the name of a gif-file and *shape* is ``None``: Install the |
| 1437 | corresponding image shape. |
| 1438 | |
| 1439 | .. note:: |
| 1440 | Image shapes *do not* rotate when turning the turtle, so they do not |
| 1441 | display the heading of the turtle! |
| 1442 | |
| 1443 | (2) *name* is an arbitrary string and *shape* is a tuple of pairs of |
| 1444 | coordinates: Install the corresponding polygon shape. |
| 1445 | |
| 1446 | (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape` |
| 1447 | object: Install the corresponding compound shape. |
| 1448 | |
| 1449 | Add a turtle shape to TurtleScreen's shapelist. Only thusly registered |
| 1450 | shapes can be used by issuing the command ``shape(shapename)``. |
| 1451 | |
| 1452 | >>> screen.register_shape("turtle.gif") |
| 1453 | >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3))) |
| 1454 | |
| 1455 | |
| 1456 | .. function:: turtles() |
| 1457 | |
| 1458 | Return the list of turtles on the screen. |
| 1459 | |
| 1460 | >>> for turtle in screen.turtles() |
| 1461 | ... turtle.color("red") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1462 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1463 | |
| 1464 | .. function:: window_height() |
| 1465 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1466 | Return the height of the turtle window. |
| 1467 | |
| 1468 | >>> screen.window_height() |
| 1469 | 480 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1470 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1471 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1472 | .. function:: window_width() |
| 1473 | |
| 1474 | Return the width of the turtle window. |
| 1475 | |
| 1476 | >>> screen.window_width() |
| 1477 | 640 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1478 | |
| 1479 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1480 | .. _screenspecific: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1481 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1482 | Methods specific to Screen, not inherited from TurtleScreen |
| 1483 | ----------------------------------------------------------- |
| 1484 | |
| 1485 | .. function:: bye() |
| 1486 | |
| 1487 | Shut the turtlegraphics window. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1488 | |
| 1489 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1490 | .. function:: exitonclick() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1491 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1492 | Bind bye() method to mouse clicks on the Screen. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1493 | |
| 1494 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1495 | If the value "using_IDLE" in the configuration dictionary is ``False`` |
| 1496 | (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch |
| 1497 | (no subprocess) is used, this value should be set to ``True`` in |
| 1498 | :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the |
| 1499 | client script. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1500 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1501 | |
| 1502 | .. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"]) |
| 1503 | |
| 1504 | Set the size and position of the main window. Default values of arguments |
| 1505 | are stored in the configuration dicionary and can be changed via a |
| 1506 | :file:`turtle.cfg` file. |
| 1507 | |
| 1508 | :param width: if an integer, a size in pixels, if a float, a fraction of the |
| 1509 | screen; default is 50% of screen |
| 1510 | :param height: if an integer, the height in pixels, if a float, a fraction of |
| 1511 | the screen; default is 75% of screen |
| 1512 | :param startx: if positive, starting position in pixels from the left |
| 1513 | edge of the screen, if negative from the right edge, if None, |
| 1514 | center window horizontally |
| 1515 | :param startx: if positive, starting position in pixels from the top |
| 1516 | edge of the screen, if negative from the bottom edge, if None, |
| 1517 | center window vertically |
| 1518 | |
| 1519 | >>> screen.setup (width=200, height=200, startx=0, starty=0) |
| 1520 | # sets window to 200x200 pixels, in upper left of screen |
| 1521 | >>> screen.setup(width=.75, height=0.5, startx=None, starty=None) |
| 1522 | # sets window to 75% of screen by 50% of screen and centers |
| 1523 | |
| 1524 | |
| 1525 | .. function:: title(titlestring) |
| 1526 | |
| 1527 | :param titlestring: a string that is shown in the titlebar of the turtle |
| 1528 | graphics window |
| 1529 | |
| 1530 | Set title of turtle window to *titlestring*. |
| 1531 | |
| 1532 | >>> screen.title("Welcome to the turtle zoo!") |
| 1533 | |
| 1534 | |
| 1535 | The public classes of the module :mod:`turtle` |
| 1536 | ============================================== |
| 1537 | |
| 1538 | |
| 1539 | .. class:: RawTurtle(canvas) |
| 1540 | RawPen(canvas) |
| 1541 | |
| 1542 | :param canvas: a :class:`Tkinter.Canvas`, a :class:`ScrolledCanvas` or a |
| 1543 | :class:`TurtleScreen` |
| 1544 | |
| 1545 | Create a turtle. The turtle has all methods described above as "methods of |
| 1546 | Turtle/RawTurtle". |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1547 | |
| 1548 | |
| 1549 | .. class:: Turtle() |
| 1550 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1551 | Subclass of RawTurtle, has the same interface but draws on a default |
| 1552 | :class:`Screen` object created automatically when needed for the first time. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1553 | |
| 1554 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1555 | .. class:: TurtleScreen(cv) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1556 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1557 | :param cv: a :class:`Tkinter.Canvas` |
| 1558 | |
| 1559 | Provides screen oriented methods like :func:`setbg` etc. that are described |
| 1560 | above. |
| 1561 | |
| 1562 | .. class:: Screen() |
| 1563 | |
| 1564 | Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`. |
| 1565 | |
| 1566 | |
| 1567 | .. class:: ScrolledCavas(master) |
| 1568 | |
| 1569 | :param master: some Tkinter widget to contain the ScrolledCanvas, i.e. |
| 1570 | a Tkinter-canvas with scrollbars added |
| 1571 | |
| 1572 | Used by class Screen, which thus automatically provides a ScrolledCanvas as |
| 1573 | playground for the turtles. |
| 1574 | |
| 1575 | .. class:: Shape(type_, data) |
| 1576 | |
| 1577 | :param type\_: one of the strings "polygon", "image", "compound" |
| 1578 | |
| 1579 | Data structure modeling shapes. The pair ``(type_, data)`` must follow this |
| 1580 | specification: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1581 | |
| 1582 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1583 | =========== =========== |
| 1584 | *type_* *data* |
| 1585 | =========== =========== |
| 1586 | "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates |
| 1587 | "image" an image (in this form only used internally!) |
| 1588 | "compound" ``None`` (a compund shape has to be constructed using the |
| 1589 | :meth:`addcomponent` method) |
| 1590 | =========== =========== |
| 1591 | |
| 1592 | .. method:: addcomponent(poly, fill, outline=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1593 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1594 | :param poly: a polygon, i.e. a tuple of pairs of numbers |
| 1595 | :param fill: a color the *poly* will be filled with |
| 1596 | :param outline: a color for the poly's outline (if given) |
| 1597 | |
| 1598 | Example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1599 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1600 | >>> poly = ((0,0),(10,-5),(0,10),(-10,-5)) |
| 1601 | >>> s = Shape("compound") |
| 1602 | >>> s.addcomponent(poly, "red", "blue") |
| 1603 | # .. add more components and then use register_shape() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1604 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1605 | See :ref:`compoundshapes`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1606 | |
| 1607 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1608 | .. class:: Vec2D(x, y) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1609 | |
Martin v. Löwis | 97cf99f | 2008-06-10 04:44:07 +0000 | [diff] [blame] | 1610 | A two-dimensional vector class, used as a helper class for implementing |
| 1611 | turtle graphics. May be useful for turtle graphics programs too. Derived |
| 1612 | from tuple, so a vector is a tuple! |
| 1613 | |
| 1614 | Provides (for *a*, *b* vectors, *k* number): |
| 1615 | |
| 1616 | * ``a + b`` vector addition |
| 1617 | * ``a - b`` vector subtraction |
| 1618 | * ``a * b`` inner product |
| 1619 | * ``k * a`` and ``a * k`` multiplication with scalar |
| 1620 | * ``abs(a)`` absolute value of a |
| 1621 | * ``a.rotate(angle)`` rotation |
| 1622 | |
| 1623 | |
| 1624 | Help and configuration |
| 1625 | ====================== |
| 1626 | |
| 1627 | How to use help |
| 1628 | --------------- |
| 1629 | |
| 1630 | The public methods of the Screen and Turtle classes are documented extensively |
| 1631 | via docstrings. So these can be used as online-help via the Python help |
| 1632 | facilities: |
| 1633 | |
| 1634 | - When using IDLE, tooltips show the signatures and first lines of the |
| 1635 | docstrings of typed in function-/method calls. |
| 1636 | |
| 1637 | - Calling :func:`help` on methods or functions displays the docstrings:: |
| 1638 | |
| 1639 | >>> help(Screen.bgcolor) |
| 1640 | Help on method bgcolor in module turtle: |
| 1641 | |
| 1642 | bgcolor(self, *args) unbound turtle.Screen method |
| 1643 | Set or return backgroundcolor of the TurtleScreen. |
| 1644 | |
| 1645 | Arguments (if given): a color string or three numbers |
| 1646 | in the range 0..colormode or a 3-tuple of such numbers. |
| 1647 | |
| 1648 | |
| 1649 | >>> screen.bgcolor("orange") |
| 1650 | >>> screen.bgcolor() |
| 1651 | "orange" |
| 1652 | >>> screen.bgcolor(0.5,0,0.5) |
| 1653 | >>> screen.bgcolor() |
| 1654 | "#800080" |
| 1655 | |
| 1656 | >>> help(Turtle.penup) |
| 1657 | Help on method penup in module turtle: |
| 1658 | |
| 1659 | penup(self) unbound turtle.Turtle method |
| 1660 | Pull the pen up -- no drawing when moving. |
| 1661 | |
| 1662 | Aliases: penup | pu | up |
| 1663 | |
| 1664 | No argument |
| 1665 | |
| 1666 | >>> turtle.penup() |
| 1667 | |
| 1668 | - The docstrings of the functions which are derived from methods have a modified |
| 1669 | form:: |
| 1670 | |
| 1671 | >>> help(bgcolor) |
| 1672 | Help on function bgcolor in module turtle: |
| 1673 | |
| 1674 | bgcolor(*args) |
| 1675 | Set or return backgroundcolor of the TurtleScreen. |
| 1676 | |
| 1677 | Arguments (if given): a color string or three numbers |
| 1678 | in the range 0..colormode or a 3-tuple of such numbers. |
| 1679 | |
| 1680 | Example:: |
| 1681 | |
| 1682 | >>> bgcolor("orange") |
| 1683 | >>> bgcolor() |
| 1684 | "orange" |
| 1685 | >>> bgcolor(0.5,0,0.5) |
| 1686 | >>> bgcolor() |
| 1687 | "#800080" |
| 1688 | |
| 1689 | >>> help(penup) |
| 1690 | Help on function penup in module turtle: |
| 1691 | |
| 1692 | penup() |
| 1693 | Pull the pen up -- no drawing when moving. |
| 1694 | |
| 1695 | Aliases: penup | pu | up |
| 1696 | |
| 1697 | No argument |
| 1698 | |
| 1699 | Example: |
| 1700 | >>> penup() |
| 1701 | |
| 1702 | These modified docstrings are created automatically together with the function |
| 1703 | definitions that are derived from the methods at import time. |
| 1704 | |
| 1705 | |
| 1706 | Translation of docstrings into different languages |
| 1707 | -------------------------------------------------- |
| 1708 | |
| 1709 | There is a utility to create a dictionary the keys of which are the method names |
| 1710 | and the values of which are the docstrings of the public methods of the classes |
| 1711 | Screen and Turtle. |
| 1712 | |
| 1713 | .. function:: write_docstringdict(filename="turtle_docstringdict") |
| 1714 | |
| 1715 | :param filename: a string, used as filename |
| 1716 | |
| 1717 | Create and write docstring-dictionary to a Python script with the given |
| 1718 | filename. This function has to be called explicitly (it is not used by the |
| 1719 | turtle graphics classes). The docstring dictionary will be written to the |
| 1720 | Python script :file:`{filename}.py`. It is intended to serve as a template |
| 1721 | for translation of the docstrings into different languages. |
| 1722 | |
| 1723 | If you (or your students) want to use :mod:`turtle` with online help in your |
| 1724 | native language, you have to translate the docstrings and save the resulting |
| 1725 | file as e.g. :file:`turtle_docstringdict_german.py`. |
| 1726 | |
| 1727 | If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary |
| 1728 | will be read in at import time and will replace the original English docstrings. |
| 1729 | |
| 1730 | At the time of this writing there are docstring dictionaries in German and in |
| 1731 | Italian. (Requests please to glingl@aon.at.) |
| 1732 | |
| 1733 | |
| 1734 | |
| 1735 | How to configure Screen and Turtles |
| 1736 | ----------------------------------- |
| 1737 | |
| 1738 | The built-in default configuration mimics the appearance and behaviour of the |
| 1739 | old turtle module in order to retain best possible compatibility with it. |
| 1740 | |
| 1741 | If you want to use a different configuration which better reflects the features |
| 1742 | of this module or which better fits to your needs, e.g. for use in a classroom, |
| 1743 | you can prepare a configuration file ``turtle.cfg`` which will be read at import |
| 1744 | time and modify the configuration according to its settings. |
| 1745 | |
| 1746 | The built in configuration would correspond to the following turtle.cfg:: |
| 1747 | |
| 1748 | width = 0.5 |
| 1749 | height = 0.75 |
| 1750 | leftright = None |
| 1751 | topbottom = None |
| 1752 | canvwidth = 400 |
| 1753 | canvheight = 300 |
| 1754 | mode = standard |
| 1755 | colormode = 1.0 |
| 1756 | delay = 10 |
| 1757 | undobuffersize = 1000 |
| 1758 | shape = classic |
| 1759 | pencolor = black |
| 1760 | fillcolor = black |
| 1761 | resizemode = noresize |
| 1762 | visible = True |
| 1763 | language = english |
| 1764 | exampleturtle = turtle |
| 1765 | examplescreen = screen |
| 1766 | title = Python Turtle Graphics |
| 1767 | using_IDLE = False |
| 1768 | |
| 1769 | Short explanation of selected entries: |
| 1770 | |
| 1771 | - The first four lines correspond to the arguments of the :meth:`Screen.setup` |
| 1772 | method. |
| 1773 | - Line 5 and 6 correspond to the arguments of the method |
| 1774 | :meth:`Screen.screensize`. |
| 1775 | - *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more |
| 1776 | info try ``help(shape)``. |
| 1777 | - If you want to use no fillcolor (i.e. make the turtle transparent), you have |
| 1778 | to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in |
| 1779 | the cfg-file). |
| 1780 | - If you want to reflect the turtle its state, you have to use ``resizemode = |
| 1781 | auto``. |
| 1782 | - If you set e.g. ``language = italian`` the docstringdict |
| 1783 | :file:`turtle_docstringdict_italian.py` will be loaded at import time (if |
| 1784 | present on the import path, e.g. in the same directory as :mod:`turtle`. |
| 1785 | - The entries *exampleturtle* and *examplescreen* define the names of these |
| 1786 | objects as they occur in the docstrings. The transformation of |
| 1787 | method-docstrings to function-docstrings will delete these names from the |
| 1788 | docstrings. |
| 1789 | - *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n |
| 1790 | switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the |
| 1791 | mainloop. |
| 1792 | |
| 1793 | There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is |
| 1794 | stored and an additional one in the current working directory. The latter will |
| 1795 | override the settings of the first one. |
| 1796 | |
| 1797 | The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can |
| 1798 | study it as an example and see its effects when running the demos (preferably |
| 1799 | not from within the demo-viewer). |
| 1800 | |
| 1801 | |
| 1802 | Demo scripts |
| 1803 | ============ |
| 1804 | |
| 1805 | There is a set of demo scripts in the turtledemo directory located in the |
| 1806 | :file:`Demo/turtle` directory in the source distribution. |
| 1807 | |
| 1808 | It contains: |
| 1809 | |
| 1810 | - a set of 15 demo scripts demonstrating differet features of the new module |
| 1811 | :mod:`turtle` |
| 1812 | - a demo viewer :file:`turtleDemo.py` which can be used to view the sourcecode |
| 1813 | of the scripts and run them at the same time. 14 of the examples can be |
| 1814 | accessed via the Examples menu; all of them can also be run standalone. |
| 1815 | - The example :file:`turtledemo_two_canvases.py` demonstrates the simultaneous |
| 1816 | use of two canvases with the turtle module. Therefore it only can be run |
| 1817 | standalone. |
| 1818 | - There is a :file:`turtle.cfg` file in this directory, which also serves as an |
| 1819 | example for how to write and use such files. |
| 1820 | |
| 1821 | The demoscripts are: |
| 1822 | |
| 1823 | +----------------+------------------------------+-----------------------+ |
| 1824 | | Name | Description | Features | |
| 1825 | +----------------+------------------------------+-----------------------+ |
| 1826 | | bytedesign | complex classical | :func:`tracer`, delay,| |
| 1827 | | | turtlegraphics pattern | :func:`update` | |
| 1828 | +----------------+------------------------------+-----------------------+ |
| 1829 | | chaos | graphs verhust dynamics, | world coordinates | |
| 1830 | | | proves that you must not | | |
| 1831 | | | trust computers' computations| | |
| 1832 | +----------------+------------------------------+-----------------------+ |
| 1833 | | clock | analog clock showing time | turtles as clock's | |
| 1834 | | | of your computer | hands, ontimer | |
| 1835 | +----------------+------------------------------+-----------------------+ |
| 1836 | | colormixer | experiment with r, g, b | :func:`ondrag` | |
| 1837 | +----------------+------------------------------+-----------------------+ |
| 1838 | | fractalcurves | Hilbert & Koch curves | recursion | |
| 1839 | +----------------+------------------------------+-----------------------+ |
| 1840 | | lindenmayer | ethnomathematics | L-System | |
| 1841 | | | (indian kolams) | | |
| 1842 | +----------------+------------------------------+-----------------------+ |
| 1843 | | minimal_hanoi | Towers of Hanoi | Rectangular Turtles | |
| 1844 | | | | as Hanoi discs | |
| 1845 | | | | (shape, shapesize) | |
| 1846 | +----------------+------------------------------+-----------------------+ |
| 1847 | | paint | super minimalistic | :func:`onclick` | |
| 1848 | | | drawing program | | |
| 1849 | +----------------+------------------------------+-----------------------+ |
| 1850 | | peace | elementary | turtle: appearance | |
| 1851 | | | | and animation | |
| 1852 | +----------------+------------------------------+-----------------------+ |
| 1853 | | penrose | aperiodic tiling with | :func:`stamp` | |
| 1854 | | | kites and darts | | |
| 1855 | +----------------+------------------------------+-----------------------+ |
| 1856 | | planet_and_moon| simulation of | compound shapes, | |
| 1857 | | | gravitational system | :class:`Vec2D` | |
| 1858 | +----------------+------------------------------+-----------------------+ |
| 1859 | | tree | a (graphical) breadth | :func:`clone` | |
| 1860 | | | first tree (using generators)| | |
| 1861 | +----------------+------------------------------+-----------------------+ |
| 1862 | | wikipedia | a pattern from the wikipedia | :func:`clone`, | |
| 1863 | | | article on turtle graphics | :func:`undo` | |
| 1864 | +----------------+------------------------------+-----------------------+ |
| 1865 | | yingyang | another elementary example | :func:`circle` | |
| 1866 | +----------------+------------------------------+-----------------------+ |
| 1867 | |
| 1868 | Have fun! |
| 1869 | |
| 1870 | |
| 1871 | Changes since Python 2.6 |
| 1872 | ======================== |
| 1873 | |
| 1874 | - The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and |
| 1875 | :meth:`Turtle.window_height` have been eliminated. |
| 1876 | Methods with these names and functionality are now available only |
| 1877 | as methods of :class:`Screen`. The functions derived from these remain |
| 1878 | available. (In fact already in Python 2.6 these methods were merely |
| 1879 | duplications of the corresponding |
| 1880 | :class:`TurtleScreen`/:class:`Screen`-methods.) |
| 1881 | |
| 1882 | - The method :meth:`Turtle.fill` has been eliminated. |
| 1883 | The behaviour of :meth:`begin_fill` and :meth:`end_fill` |
| 1884 | have changed slightly: now every filling-process must be completed with an |
| 1885 | ``end_fill()`` call. |
| 1886 | |
| 1887 | - A method :meth:`Turtle.filling` has been added. It returns a boolean |
| 1888 | value: ``True`` if a filling process is under way, ``False`` otherwise. |
| 1889 | This behaviour corresponds to a ``fill()`` call without arguments in |
| 1890 | Python 2.6 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1891 | |