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