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