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