blob: 041b651bbcaf8a98f6141d169bac36996695ca4f [file] [log] [blame]
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00001=================================
2:mod:`turtle` --- Turtle graphics
3=================================
Georg Brandl116aa622007-08-15 14:28:22 +00004
Georg Brandl23d11d32008-09-21 07:50:52 +00005.. module:: turtle
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00006 :synopsis: An educational framework for simple graphics applications
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Georg Brandl2ee470f2008-07-16 12:55:28 +00008.. sectionauthor:: Gregor Lingl <gregor.lingl@aon.at>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/turtle.py`
11
R. David Murrayf877feb2009-05-05 02:08:52 +000012.. testsetup:: default
13
14 from turtle import *
15 turtle = Turtle()
16
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040017--------------
18
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000019Introduction
20============
21
22Turtle graphics is a popular way for introducing programming to kids. It was
23part of the original Logo programming language developed by Wally Feurzig and
24Seymour Papert in 1966.
25
Sandro Tosi2a389e42011-08-07 17:12:19 +020026Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000027command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
28direction it is facing, drawing a line as it moves. Give it the command
Sandro Tosi2a389e42011-08-07 17:12:19 +020029``turtle.right(25)``, and it rotates in-place 25 degrees clockwise.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000030
Alexander Belopolsky14fb7992010-11-09 18:40:03 +000031.. 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öwis97cf99f2008-06-10 04:44:07 +000041By combining together these and similar commands, intricate shapes and pictures
42can easily be drawn.
43
44The :mod:`turtle` module is an extended reimplementation of the same-named
45module from the Python standard distribution up to version Python 2.5.
46
47It tries to keep the merits of the old turtle module and to be (nearly) 100%
48compatible with it. This means in the first place to enable the learning
49programmer to use all the commands, classes and methods interactively when using
50the module from within IDLE run with the ``-n`` switch.
51
52The turtle module provides turtle graphics primitives, in both object-oriented
Ezio Melotti1a263ad2010-03-14 09:51:37 +000053and procedure-oriented ways. Because it uses :mod:`tkinter` for the underlying
Ezio Melotti0639d5a2009-12-19 23:26:38 +000054graphics, it needs a version of Python installed with Tk support.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000055
56The object-oriented interface uses essentially two+two classes:
57
581. The :class:`TurtleScreen` class defines graphics windows as a playground for
Ezio Melotti1a263ad2010-03-14 09:51:37 +000059 the drawing turtles. Its constructor needs a :class:`tkinter.Canvas` or a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000060 :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is
61 used as part of some application.
62
Martin v. Löwis601149b2008-09-29 22:19:08 +000063 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öwis97cf99f2008-06-10 04:44:07 +000067
68 All methods of TurtleScreen/Screen also exist as functions, i.e. as part of
69 the procedure-oriented interface.
70
712. :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 Belopolsky435d3062010-10-19 21:07:52 +000076 which draws on "the" :class:`Screen` instance which is automatically
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000077 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
82The procedural interface provides functions which are derived from the methods
83of the classes :class:`Screen` and :class:`Turtle`. They have the same names as
Georg Brandlae2dbe22009-03-13 19:04:40 +000084the corresponding methods. A screen object is automatically created whenever a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000085function derived from a Screen method is called. An (unnamed) turtle object is
86automatically created whenever any of the functions derived from a Turtle method
87is called.
88
Alexander Belopolsky435d3062010-10-19 21:07:52 +000089To use multiple turtles on a screen one has to use the object-oriented interface.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000090
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 Brandl116aa622007-08-15 14:28:22 +000095
96
Alexander Belopolsky435d3062010-10-19 21:07:52 +000097Overview of available Turtle and Screen methods
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000098=================================================
99
100Turtle methods
101--------------
102
103Turtle 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
134Pen 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
157Turtle 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 Brandleaa84ef2009-05-05 08:14:33 +0000167 | :func:`shearfactor`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000168 | :func:`settiltangle`
169 | :func:`tiltangle`
170 | :func:`tilt`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000171 | :func:`shapetransform`
172 | :func:`get_shapepoly`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000173
174Using events
175 | :func:`onclick`
176 | :func:`onrelease`
177 | :func:`ondrag`
178
179Special 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 Brandl116aa622007-08-15 14:28:22 +0000188
189
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000190Methods of TurtleScreen/Screen
191------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000192
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000193Window control
194 | :func:`bgcolor`
195 | :func:`bgpic`
196 | :func:`clear` | :func:`clearscreen`
197 | :func:`reset` | :func:`resetscreen`
198 | :func:`screensize`
199 | :func:`setworldcoordinates`
Georg Brandl116aa622007-08-15 14:28:22 +0000200
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000201Animation control
202 | :func:`delay`
203 | :func:`tracer`
204 | :func:`update`
205
206Using screen events
207 | :func:`listen`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000208 | :func:`onkey` | :func:`onkeyrelease`
209 | :func:`onkeypress`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000210 | :func:`onclick` | :func:`onscreenclick`
211 | :func:`ontimer`
Sandro Tosie3484552011-10-31 10:12:43 +0100212 | :func:`mainloop` | :func:`done`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000213
214Settings 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 Brandleaa84ef2009-05-05 08:14:33 +0000224Input methods
225 | :func:`textinput`
226 | :func:`numinput`
227
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000228Methods specific to Screen
229 | :func:`bye`
230 | :func:`exitonclick`
231 | :func:`setup`
232 | :func:`title`
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000235Methods of RawTurtle/Turtle and corresponding functions
236=======================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000237
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000238Most of the examples in this section refer to a Turtle instance called
239``turtle``.
Georg Brandl116aa622007-08-15 14:28:22 +0000240
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000241Turtle motion
242-------------
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244.. function:: forward(distance)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000245 fd(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000246
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000247 :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 Murrayf877feb2009-05-05 02:08:52 +0000252 .. doctest::
253
254 >>> turtle.position()
255 (0.00,0.00)
256 >>> turtle.forward(25)
257 >>> turtle.position()
258 (25.00,0.00)
259 >>> turtle.forward(-75)
260 >>> turtle.position()
261 (-50.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000262
263
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000264.. function:: back(distance)
265 bk(distance)
266 backward(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000267
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000268 :param distance: a number
Georg Brandl116aa622007-08-15 14:28:22 +0000269
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000270 Move the turtle backward by *distance*, opposite to the direction the
271 turtle is headed. Do not change the turtle's heading.
Georg Brandl116aa622007-08-15 14:28:22 +0000272
R. David Murrayf877feb2009-05-05 02:08:52 +0000273 .. doctest::
274 :hide:
275
276 >>> turtle.goto(0, 0)
277
278 .. doctest::
279
280 >>> turtle.position()
281 (0.00,0.00)
282 >>> turtle.backward(30)
283 >>> turtle.position()
284 (-30.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000285
286
287.. function:: right(angle)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000288 rt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000289
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000290 :param angle: a number (integer or float)
291
292 Turn turtle right by *angle* units. (Units are by default degrees, but
293 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
294 orientation depends on the turtle mode, see :func:`mode`.
295
R. David Murrayf877feb2009-05-05 02:08:52 +0000296 .. doctest::
297 :hide:
298
299 >>> turtle.setheading(22)
300
301 .. doctest::
302
303 >>> turtle.heading()
304 22.0
305 >>> turtle.right(45)
306 >>> turtle.heading()
307 337.0
Georg Brandl116aa622007-08-15 14:28:22 +0000308
309
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000310.. function:: left(angle)
311 lt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000312
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000313 :param angle: a number (integer or float)
Georg Brandl116aa622007-08-15 14:28:22 +0000314
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000315 Turn turtle left by *angle* units. (Units are by default degrees, but
316 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
317 orientation depends on the turtle mode, see :func:`mode`.
Georg Brandl116aa622007-08-15 14:28:22 +0000318
R. David Murrayf877feb2009-05-05 02:08:52 +0000319 .. doctest::
320 :hide:
321
322 >>> turtle.setheading(22)
323
324 .. doctest::
325
326 >>> turtle.heading()
327 22.0
328 >>> turtle.left(45)
329 >>> turtle.heading()
330 67.0
331
Georg Brandl116aa622007-08-15 14:28:22 +0000332
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000333.. function:: goto(x, y=None)
334 setpos(x, y=None)
335 setposition(x, y=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000336
R. David Murrayf877feb2009-05-05 02:08:52 +0000337 :param x: a number or a pair/vector of numbers
338 :param y: a number or ``None``
Georg Brandl116aa622007-08-15 14:28:22 +0000339
R. David Murrayf877feb2009-05-05 02:08:52 +0000340 If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D`
341 (e.g. as returned by :func:`pos`).
Georg Brandl116aa622007-08-15 14:28:22 +0000342
R. David Murrayf877feb2009-05-05 02:08:52 +0000343 Move turtle to an absolute position. If the pen is down, draw line. Do
344 not change the turtle's orientation.
Georg Brandl116aa622007-08-15 14:28:22 +0000345
R. David Murrayf877feb2009-05-05 02:08:52 +0000346 .. doctest::
347 :hide:
348
349 >>> turtle.goto(0, 0)
350
351 .. doctest::
352
353 >>> tp = turtle.pos()
354 >>> tp
355 (0.00,0.00)
356 >>> turtle.setpos(60,30)
357 >>> turtle.pos()
358 (60.00,30.00)
359 >>> turtle.setpos((20,80))
360 >>> turtle.pos()
361 (20.00,80.00)
362 >>> turtle.setpos(tp)
363 >>> turtle.pos()
364 (0.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000365
Georg Brandl116aa622007-08-15 14:28:22 +0000366
367.. function:: setx(x)
368
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000369 :param x: a number (integer or float)
370
371 Set the turtle's first coordinate to *x*, leave second coordinate
372 unchanged.
373
R. David Murrayf877feb2009-05-05 02:08:52 +0000374 .. doctest::
375 :hide:
376
377 >>> turtle.goto(0, 240)
378
379 .. doctest::
380
381 >>> turtle.position()
382 (0.00,240.00)
383 >>> turtle.setx(10)
384 >>> turtle.position()
385 (10.00,240.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Georg Brandl116aa622007-08-15 14:28:22 +0000387
388.. function:: sety(y)
389
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000390 :param y: a number (integer or float)
391
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000392 Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000393
R. David Murrayf877feb2009-05-05 02:08:52 +0000394 .. doctest::
395 :hide:
396
397 >>> turtle.goto(0, 40)
398
399 .. doctest::
400
401 >>> turtle.position()
402 (0.00,40.00)
403 >>> turtle.sety(-10)
404 >>> turtle.position()
405 (0.00,-10.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000406
Georg Brandl116aa622007-08-15 14:28:22 +0000407
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000408.. function:: setheading(to_angle)
409 seth(to_angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000410
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000411 :param to_angle: a number (integer or float)
412
413 Set the orientation of the turtle to *to_angle*. Here are some common
414 directions in degrees:
415
416 =================== ====================
417 standard mode logo mode
418 =================== ====================
419 0 - east 0 - north
420 90 - north 90 - east
421 180 - west 180 - south
422 270 - south 270 - west
423 =================== ====================
424
R. David Murrayf877feb2009-05-05 02:08:52 +0000425 .. doctest::
426
427 >>> turtle.setheading(90)
428 >>> turtle.heading()
429 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000430
431
432.. function:: home()
433
434 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
435 its start-orientation (which depends on the mode, see :func:`mode`).
436
R. David Murrayf877feb2009-05-05 02:08:52 +0000437 .. doctest::
438 :hide:
439
440 >>> turtle.setheading(90)
441 >>> turtle.goto(0, -10)
442
443 .. doctest::
444
445 >>> turtle.heading()
446 90.0
447 >>> turtle.position()
448 (0.00,-10.00)
449 >>> turtle.home()
450 >>> turtle.position()
451 (0.00,0.00)
452 >>> turtle.heading()
453 0.0
454
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000455
456.. function:: circle(radius, extent=None, steps=None)
457
458 :param radius: a number
459 :param extent: a number (or ``None``)
460 :param steps: an integer (or ``None``)
461
462 Draw a circle with given *radius*. The center is *radius* units left of
463 the turtle; *extent* -- an angle -- determines which part of the circle
464 is drawn. If *extent* is not given, draw the entire circle. If *extent*
465 is not a full circle, one endpoint of the arc is the current pen
466 position. Draw the arc in counterclockwise direction if *radius* is
467 positive, otherwise in clockwise direction. Finally the direction of the
468 turtle is changed by the amount of *extent*.
469
470 As the circle is approximated by an inscribed regular polygon, *steps*
471 determines the number of steps to use. If not given, it will be
472 calculated automatically. May be used to draw regular polygons.
473
R. David Murrayf877feb2009-05-05 02:08:52 +0000474 .. doctest::
475
476 >>> turtle.home()
477 >>> turtle.position()
478 (0.00,0.00)
479 >>> turtle.heading()
480 0.0
481 >>> turtle.circle(50)
482 >>> turtle.position()
483 (-0.00,0.00)
484 >>> turtle.heading()
485 0.0
486 >>> turtle.circle(120, 180) # draw a semicircle
487 >>> turtle.position()
488 (0.00,240.00)
489 >>> turtle.heading()
490 180.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000491
492
493.. function:: dot(size=None, *color)
494
495 :param size: an integer >= 1 (if given)
496 :param color: a colorstring or a numeric color tuple
497
498 Draw a circular dot with diameter *size*, using *color*. If *size* is
499 not given, the maximum of pensize+4 and 2*pensize is used.
500
R. David Murrayf877feb2009-05-05 02:08:52 +0000501
502 .. doctest::
503
504 >>> turtle.home()
505 >>> turtle.dot()
506 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
507 >>> turtle.position()
508 (100.00,-0.00)
509 >>> turtle.heading()
510 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000511
512
513.. function:: stamp()
514
515 Stamp a copy of the turtle shape onto the canvas at the current turtle
516 position. Return a stamp_id for that stamp, which can be used to delete
517 it by calling ``clearstamp(stamp_id)``.
518
R. David Murrayf877feb2009-05-05 02:08:52 +0000519 .. doctest::
520
521 >>> turtle.color("blue")
522 >>> turtle.stamp()
523 11
524 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000525
526
527.. function:: clearstamp(stampid)
528
529 :param stampid: an integer, must be return value of previous
530 :func:`stamp` call
531
532 Delete stamp with given *stampid*.
533
R. David Murrayf877feb2009-05-05 02:08:52 +0000534 .. doctest::
535
536 >>> turtle.position()
537 (150.00,-0.00)
538 >>> turtle.color("blue")
539 >>> astamp = turtle.stamp()
540 >>> turtle.fd(50)
541 >>> turtle.position()
542 (200.00,-0.00)
543 >>> turtle.clearstamp(astamp)
544 >>> turtle.position()
545 (200.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000546
547
548.. function:: clearstamps(n=None)
549
550 :param n: an integer (or ``None``)
551
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300552 Delete all or first/last *n* of turtle's stamps. If *n* is ``None``, delete
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000553 all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete
554 last *n* stamps.
555
R. David Murrayf877feb2009-05-05 02:08:52 +0000556 .. doctest::
557
558 >>> for i in range(8):
559 ... turtle.stamp(); turtle.fd(30)
560 13
561 14
562 15
563 16
564 17
565 18
566 19
567 20
568 >>> turtle.clearstamps(2)
569 >>> turtle.clearstamps(-2)
570 >>> turtle.clearstamps()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000571
572
573.. function:: undo()
574
575 Undo (repeatedly) the last turtle action(s). Number of available
576 undo actions is determined by the size of the undobuffer.
577
R. David Murrayf877feb2009-05-05 02:08:52 +0000578 .. doctest::
579
580 >>> for i in range(4):
581 ... turtle.fd(50); turtle.lt(80)
582 ...
583 >>> for i in range(8):
584 ... turtle.undo()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000585
586
587.. function:: speed(speed=None)
588
589 :param speed: an integer in the range 0..10 or a speedstring (see below)
590
591 Set the turtle's speed to an integer value in the range 0..10. If no
592 argument is given, return current speed.
593
594 If input is a number greater than 10 or smaller than 0.5, speed is set
595 to 0. Speedstrings are mapped to speedvalues as follows:
596
597 * "fastest": 0
598 * "fast": 10
599 * "normal": 6
600 * "slow": 3
601 * "slowest": 1
602
603 Speeds from 1 to 10 enforce increasingly faster animation of line drawing
604 and turtle turning.
605
606 Attention: *speed* = 0 means that *no* animation takes
607 place. forward/back makes turtle jump and likewise left/right make the
608 turtle turn instantly.
609
R. David Murrayf877feb2009-05-05 02:08:52 +0000610 .. doctest::
611
612 >>> turtle.speed()
613 3
614 >>> turtle.speed('normal')
615 >>> turtle.speed()
616 6
617 >>> turtle.speed(9)
618 >>> turtle.speed()
619 9
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000620
621
622Tell Turtle's state
623-------------------
624
625.. function:: position()
626 pos()
627
628 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
629
R. David Murrayf877feb2009-05-05 02:08:52 +0000630 .. doctest::
631
632 >>> turtle.pos()
633 (440.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000634
635
636.. function:: towards(x, y=None)
637
638 :param x: a number or a pair/vector of numbers or a turtle instance
639 :param y: a number if *x* is a number, else ``None``
640
641 Return the angle between the line from turtle position to position specified
642 by (x,y), the vector or the other turtle. This depends on the turtle's start
643 orientation which depends on the mode - "standard"/"world" or "logo").
644
R. David Murrayf877feb2009-05-05 02:08:52 +0000645 .. doctest::
646
647 >>> turtle.goto(10, 10)
648 >>> turtle.towards(0,0)
649 225.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000650
651
652.. function:: xcor()
653
654 Return the turtle's x coordinate.
655
R. David Murrayf877feb2009-05-05 02:08:52 +0000656 .. doctest::
657
658 >>> turtle.home()
659 >>> turtle.left(50)
660 >>> turtle.forward(100)
661 >>> turtle.pos()
662 (64.28,76.60)
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000663 >>> print(round(turtle.xcor(), 5))
664 64.27876
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000665
666
667.. function:: ycor()
668
669 Return the turtle's y coordinate.
670
R. David Murrayf877feb2009-05-05 02:08:52 +0000671 .. doctest::
672
673 >>> turtle.home()
674 >>> turtle.left(60)
675 >>> turtle.forward(100)
Ezio Melotti985e24d2009-09-13 07:54:02 +0000676 >>> print(turtle.pos())
R. David Murrayf877feb2009-05-05 02:08:52 +0000677 (50.00,86.60)
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000678 >>> print(round(turtle.ycor(), 5))
679 86.60254
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000680
681
682.. function:: heading()
683
684 Return the turtle's current heading (value depends on the turtle mode, see
685 :func:`mode`).
686
R. David Murrayf877feb2009-05-05 02:08:52 +0000687 .. doctest::
688
689 >>> turtle.home()
690 >>> turtle.left(67)
691 >>> turtle.heading()
692 67.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000693
694
695.. function:: distance(x, y=None)
696
697 :param x: a number or a pair/vector of numbers or a turtle instance
698 :param y: a number if *x* is a number, else ``None``
699
700 Return the distance from the turtle to (x,y), the given vector, or the given
701 other turtle, in turtle step units.
702
R. David Murrayf877feb2009-05-05 02:08:52 +0000703 .. doctest::
704
705 >>> turtle.home()
706 >>> turtle.distance(30,40)
707 50.0
708 >>> turtle.distance((30,40))
709 50.0
710 >>> joe = Turtle()
711 >>> joe.forward(77)
712 >>> turtle.distance(joe)
713 77.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000714
715
716Settings for measurement
717------------------------
718
719.. function:: degrees(fullcircle=360.0)
720
721 :param fullcircle: a number
722
723 Set angle measurement units, i.e. set number of "degrees" for a full circle.
724 Default value is 360 degrees.
725
R. David Murrayf877feb2009-05-05 02:08:52 +0000726 .. doctest::
727
728 >>> turtle.home()
729 >>> turtle.left(90)
730 >>> turtle.heading()
731 90.0
Alexander Belopolsky3cdfb122010-10-29 17:16:49 +0000732
733 Change angle measurement unit to grad (also known as gon,
734 grade, or gradian and equals 1/100-th of the right angle.)
735 >>> turtle.degrees(400.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000736 >>> turtle.heading()
737 100.0
738 >>> turtle.degrees(360)
739 >>> turtle.heading()
740 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000741
742
743.. function:: radians()
744
745 Set the angle measurement units to radians. Equivalent to
746 ``degrees(2*math.pi)``.
747
R. David Murrayf877feb2009-05-05 02:08:52 +0000748 .. doctest::
749
750 >>> turtle.home()
751 >>> turtle.left(90)
752 >>> turtle.heading()
753 90.0
754 >>> turtle.radians()
755 >>> turtle.heading()
756 1.5707963267948966
757
758 .. doctest::
759 :hide:
760
761 >>> turtle.degrees(360)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000762
763
764Pen control
765-----------
766
767Drawing state
768~~~~~~~~~~~~~
769
770.. function:: pendown()
771 pd()
772 down()
773
774 Pull the pen down -- drawing when moving.
775
776
777.. function:: penup()
778 pu()
779 up()
780
781 Pull the pen up -- no drawing when moving.
782
783
784.. function:: pensize(width=None)
785 width(width=None)
786
787 :param width: a positive number
788
789 Set the line thickness to *width* or return it. If resizemode is set to
790 "auto" and turtleshape is a polygon, that polygon is drawn with the same line
791 thickness. If no argument is given, the current pensize is returned.
792
R. David Murrayf877feb2009-05-05 02:08:52 +0000793 .. doctest::
794
795 >>> turtle.pensize()
796 1
797 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000798
799
800.. function:: pen(pen=None, **pendict)
801
802 :param pen: a dictionary with some or all of the below listed keys
803 :param pendict: one or more keyword-arguments with the below listed keys as keywords
804
805 Return or set the pen's attributes in a "pen-dictionary" with the following
806 key/value pairs:
807
808 * "shown": True/False
809 * "pendown": True/False
810 * "pencolor": color-string or color-tuple
811 * "fillcolor": color-string or color-tuple
812 * "pensize": positive number
813 * "speed": number in range 0..10
814 * "resizemode": "auto" or "user" or "noresize"
815 * "stretchfactor": (positive number, positive number)
816 * "outline": positive number
817 * "tilt": number
818
R. David Murrayf877feb2009-05-05 02:08:52 +0000819 This dictionary can be used as argument for a subsequent call to :func:`pen`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000820 to restore the former pen-state. Moreover one or more of these attributes
821 can be provided as keyword-arguments. This can be used to set several pen
822 attributes in one statement.
823
R. David Murrayf877feb2009-05-05 02:08:52 +0000824 .. doctest::
825 :options: +NORMALIZE_WHITESPACE
826
827 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
828 >>> sorted(turtle.pen().items())
829 [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
830 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000831 ('shearfactor', 0.0), ('shown', True), ('speed', 9),
832 ('stretchfactor', (1.0, 1.0)), ('tilt', 0.0)]
R. David Murrayf877feb2009-05-05 02:08:52 +0000833 >>> penstate=turtle.pen()
834 >>> turtle.color("yellow", "")
835 >>> turtle.penup()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000836 >>> sorted(turtle.pen().items())[:3]
837 [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow')]
R. David Murrayf877feb2009-05-05 02:08:52 +0000838 >>> turtle.pen(penstate, fillcolor="green")
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000839 >>> sorted(turtle.pen().items())[:3]
840 [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red')]
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000841
842.. function:: isdown()
843
844 Return ``True`` if pen is down, ``False`` if it's up.
845
R. David Murrayf877feb2009-05-05 02:08:52 +0000846 .. doctest::
847
848 >>> turtle.penup()
849 >>> turtle.isdown()
850 False
851 >>> turtle.pendown()
852 >>> turtle.isdown()
853 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000854
855
856Color control
857~~~~~~~~~~~~~
858
859.. function:: pencolor(*args)
860
861 Return or set the pencolor.
862
863 Four input formats are allowed:
864
865 ``pencolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000866 Return the current pencolor as color specification string or
867 as a tuple (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000868 color/pencolor/fillcolor call.
869
870 ``pencolor(colorstring)``
871 Set pencolor to *colorstring*, which is a Tk color specification string,
872 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
873
874 ``pencolor((r, g, b))``
875 Set pencolor to the RGB color represented by the tuple of *r*, *g*, and
876 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
877 colormode is either 1.0 or 255 (see :func:`colormode`).
878
879 ``pencolor(r, g, b)``
880 Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of
881 *r*, *g*, and *b* must be in the range 0..colormode.
882
883 If turtleshape is a polygon, the outline of that polygon is drawn with the
884 newly set pencolor.
885
R. David Murrayf877feb2009-05-05 02:08:52 +0000886 .. doctest::
887
888 >>> colormode()
889 1.0
890 >>> turtle.pencolor()
891 'red'
892 >>> turtle.pencolor("brown")
893 >>> turtle.pencolor()
894 'brown'
895 >>> tup = (0.2, 0.8, 0.55)
896 >>> turtle.pencolor(tup)
897 >>> turtle.pencolor()
Mark Dickinson5a55b612009-06-28 20:59:42 +0000898 (0.2, 0.8, 0.5490196078431373)
R. David Murrayf877feb2009-05-05 02:08:52 +0000899 >>> colormode(255)
900 >>> turtle.pencolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000901 (51.0, 204.0, 140.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000902 >>> turtle.pencolor('#32c18f')
903 >>> turtle.pencolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000904 (50.0, 193.0, 143.0)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000905
906
907.. function:: fillcolor(*args)
908
909 Return or set the fillcolor.
910
911 Four input formats are allowed:
912
913 ``fillcolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000914 Return the current fillcolor as color specification string, possibly
915 in tuple format (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000916 color/pencolor/fillcolor call.
917
918 ``fillcolor(colorstring)``
919 Set fillcolor to *colorstring*, which is a Tk color specification string,
920 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
921
922 ``fillcolor((r, g, b))``
923 Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and
924 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
925 colormode is either 1.0 or 255 (see :func:`colormode`).
926
927 ``fillcolor(r, g, b)``
928 Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of
929 *r*, *g*, and *b* must be in the range 0..colormode.
930
931 If turtleshape is a polygon, the interior of that polygon is drawn
932 with the newly set fillcolor.
933
R. David Murrayf877feb2009-05-05 02:08:52 +0000934 .. doctest::
935
936 >>> turtle.fillcolor("violet")
937 >>> turtle.fillcolor()
938 'violet'
Marco Buttu7b2491a2017-04-13 16:17:59 +0200939 >>> turtle.pencolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000940 (50.0, 193.0, 143.0)
Marco Buttu7b2491a2017-04-13 16:17:59 +0200941 >>> turtle.fillcolor((50, 193, 143)) # Integers, not floats
R. David Murrayf877feb2009-05-05 02:08:52 +0000942 >>> turtle.fillcolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000943 (50.0, 193.0, 143.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000944 >>> turtle.fillcolor('#ffffff')
945 >>> turtle.fillcolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000946 (255.0, 255.0, 255.0)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000947
948
949.. function:: color(*args)
950
951 Return or set pencolor and fillcolor.
952
953 Several input formats are allowed. They use 0 to 3 arguments as
954 follows:
955
956 ``color()``
957 Return the current pencolor and the current fillcolor as a pair of color
R. David Murrayf877feb2009-05-05 02:08:52 +0000958 specification strings or tuples as returned by :func:`pencolor` and
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000959 :func:`fillcolor`.
960
961 ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)``
962 Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the
963 given value.
964
965 ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))``
966 Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)``
967 and analogously if the other input format is used.
968
969 If turtleshape is a polygon, outline and interior of that polygon is drawn
970 with the newly set colors.
971
R. David Murrayf877feb2009-05-05 02:08:52 +0000972 .. doctest::
973
974 >>> turtle.color("red", "green")
975 >>> turtle.color()
976 ('red', 'green')
977 >>> color("#285078", "#a0c8f0")
978 >>> color()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000979 ((40.0, 80.0, 120.0), (160.0, 200.0, 240.0))
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000980
981
982See also: Screen method :func:`colormode`.
983
984
985Filling
986~~~~~~~
987
R. David Murrayf877feb2009-05-05 02:08:52 +0000988.. doctest::
989 :hide:
990
991 >>> turtle.home()
992
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000993.. function:: filling()
994
995 Return fillstate (``True`` if filling, ``False`` else).
996
R. David Murrayf877feb2009-05-05 02:08:52 +0000997 .. doctest::
998
999 >>> turtle.begin_fill()
1000 >>> if turtle.filling():
1001 ... turtle.pensize(5)
1002 ... else:
1003 ... turtle.pensize(3)
1004
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001005
1006
1007.. function:: begin_fill()
1008
1009 To be called just before drawing a shape to be filled.
1010
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001011
1012.. function:: end_fill()
1013
1014 Fill the shape drawn after the last call to :func:`begin_fill`.
1015
R. David Murrayf877feb2009-05-05 02:08:52 +00001016 .. doctest::
1017
1018 >>> turtle.color("black", "red")
1019 >>> turtle.begin_fill()
1020 >>> turtle.circle(80)
1021 >>> turtle.end_fill()
1022
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001023
1024More drawing control
1025~~~~~~~~~~~~~~~~~~~~
1026
1027.. function:: reset()
1028
1029 Delete the turtle's drawings from the screen, re-center the turtle and set
1030 variables to the default values.
1031
R. David Murrayf877feb2009-05-05 02:08:52 +00001032 .. doctest::
1033
1034 >>> turtle.goto(0,-22)
1035 >>> turtle.left(100)
1036 >>> turtle.position()
1037 (0.00,-22.00)
1038 >>> turtle.heading()
1039 100.0
1040 >>> turtle.reset()
1041 >>> turtle.position()
1042 (0.00,0.00)
1043 >>> turtle.heading()
1044 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001045
1046
1047.. function:: clear()
1048
1049 Delete the turtle's drawings from the screen. Do not move turtle. State and
1050 position of the turtle as well as drawings of other turtles are not affected.
1051
1052
1053.. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal"))
1054
1055 :param arg: object to be written to the TurtleScreen
1056 :param move: True/False
1057 :param align: one of the strings "left", "center" or right"
1058 :param font: a triple (fontname, fontsize, fonttype)
1059
1060 Write text - the string representation of *arg* - at the current turtle
1061 position according to *align* ("left", "center" or right") and with the given
Serhiy Storchakafbc1c262013-11-29 12:17:13 +02001062 font. If *move* is true, the pen is moved to the bottom-right corner of the
1063 text. By default, *move* is ``False``.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001064
1065 >>> turtle.write("Home = ", True, align="center")
1066 >>> turtle.write((0,0), True)
1067
1068
1069Turtle state
1070------------
1071
1072Visibility
1073~~~~~~~~~~
1074
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001075.. function:: hideturtle()
1076 ht()
1077
1078 Make the turtle invisible. It's a good idea to do this while you're in the
1079 middle of doing some complex drawing, because hiding the turtle speeds up the
1080 drawing observably.
1081
R. David Murrayf877feb2009-05-05 02:08:52 +00001082 .. doctest::
1083
1084 >>> turtle.hideturtle()
1085
1086
1087.. function:: showturtle()
1088 st()
1089
1090 Make the turtle visible.
1091
1092 .. doctest::
1093
1094 >>> turtle.showturtle()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001095
1096
1097.. function:: isvisible()
1098
Serhiy Storchakafbc1c262013-11-29 12:17:13 +02001099 Return ``True`` if the Turtle is shown, ``False`` if it's hidden.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001100
1101 >>> turtle.hideturtle()
R. David Murrayf877feb2009-05-05 02:08:52 +00001102 >>> turtle.isvisible()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001103 False
R. David Murrayf877feb2009-05-05 02:08:52 +00001104 >>> turtle.showturtle()
1105 >>> turtle.isvisible()
1106 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001107
1108
1109Appearance
1110~~~~~~~~~~
1111
1112.. function:: shape(name=None)
1113
1114 :param name: a string which is a valid shapename
1115
1116 Set turtle shape to shape with given *name* or, if name is not given, return
1117 name of current shape. Shape with *name* must exist in the TurtleScreen's
1118 shape dictionary. Initially there are the following polygon shapes: "arrow",
1119 "turtle", "circle", "square", "triangle", "classic". To learn about how to
1120 deal with shapes see Screen method :func:`register_shape`.
1121
R. David Murrayf877feb2009-05-05 02:08:52 +00001122 .. doctest::
1123
1124 >>> turtle.shape()
1125 'classic'
1126 >>> turtle.shape("turtle")
1127 >>> turtle.shape()
1128 'turtle'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001129
1130
1131.. function:: resizemode(rmode=None)
1132
1133 :param rmode: one of the strings "auto", "user", "noresize"
1134
1135 Set resizemode to one of the values: "auto", "user", "noresize". If *rmode*
1136 is not given, return current resizemode. Different resizemodes have the
1137 following effects:
1138
1139 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
1140 - "user": adapts the appearance of the turtle according to the values of
1141 stretchfactor and outlinewidth (outline), which are set by
1142 :func:`shapesize`.
1143 - "noresize": no adaption of the turtle's appearance takes place.
1144
1145 resizemode("user") is called by :func:`shapesize` when used with arguments.
1146
R. David Murrayf877feb2009-05-05 02:08:52 +00001147 .. doctest::
1148
1149 >>> turtle.resizemode()
1150 'noresize'
1151 >>> turtle.resizemode("auto")
1152 >>> turtle.resizemode()
1153 'auto'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001154
1155
1156.. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None)
R. David Murray7fcd3de2009-06-25 14:26:19 +00001157 turtlesize(stretch_wid=None, stretch_len=None, outline=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001158
1159 :param stretch_wid: positive number
1160 :param stretch_len: positive number
1161 :param outline: positive number
1162
1163 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set
1164 resizemode to "user". If and only if resizemode is set to "user", the turtle
1165 will be displayed stretched according to its stretchfactors: *stretch_wid* is
1166 stretchfactor perpendicular to its orientation, *stretch_len* is
1167 stretchfactor in direction of its orientation, *outline* determines the width
1168 of the shapes's outline.
1169
R. David Murrayf877feb2009-05-05 02:08:52 +00001170 .. doctest::
1171
1172 >>> turtle.shapesize()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001173 (1.0, 1.0, 1)
R. David Murrayf877feb2009-05-05 02:08:52 +00001174 >>> turtle.resizemode("user")
1175 >>> turtle.shapesize(5, 5, 12)
1176 >>> turtle.shapesize()
1177 (5, 5, 12)
1178 >>> turtle.shapesize(outline=8)
1179 >>> turtle.shapesize()
1180 (5, 5, 8)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001181
1182
R. David Murray7fcd3de2009-06-25 14:26:19 +00001183.. function:: shearfactor(shear=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001184
1185 :param shear: number (optional)
1186
1187 Set or return the current shearfactor. Shear the turtleshape according to
1188 the given shearfactor shear, which is the tangent of the shear angle.
1189 Do *not* change the turtle's heading (direction of movement).
1190 If shear is not given: return the current shearfactor, i. e. the
1191 tangent of the shear angle, by which lines parallel to the
1192 heading of the turtle are sheared.
1193
1194 .. doctest::
1195
1196 >>> turtle.shape("circle")
1197 >>> turtle.shapesize(5,2)
1198 >>> turtle.shearfactor(0.5)
1199 >>> turtle.shearfactor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001200 0.5
Georg Brandleaa84ef2009-05-05 08:14:33 +00001201
1202
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001203.. function:: tilt(angle)
1204
1205 :param angle: a number
1206
1207 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
1208 change the turtle's heading (direction of movement).
1209
R. David Murrayf877feb2009-05-05 02:08:52 +00001210 .. doctest::
1211
1212 >>> turtle.reset()
1213 >>> turtle.shape("circle")
1214 >>> turtle.shapesize(5,2)
1215 >>> turtle.tilt(30)
1216 >>> turtle.fd(50)
1217 >>> turtle.tilt(30)
1218 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001219
1220
1221.. function:: settiltangle(angle)
1222
1223 :param angle: a number
1224
1225 Rotate the turtleshape to point in the direction specified by *angle*,
1226 regardless of its current tilt-angle. *Do not* change the turtle's heading
1227 (direction of movement).
1228
R. David Murrayf877feb2009-05-05 02:08:52 +00001229 .. doctest::
1230
1231 >>> turtle.reset()
1232 >>> turtle.shape("circle")
1233 >>> turtle.shapesize(5,2)
1234 >>> turtle.settiltangle(45)
1235 >>> turtle.fd(50)
1236 >>> turtle.settiltangle(-45)
1237 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001238
Ezio Melotti4e511282010-02-14 03:11:06 +00001239 .. deprecated:: 3.1
1240
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001241
Georg Brandleaa84ef2009-05-05 08:14:33 +00001242.. function:: tiltangle(angle=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001243
Georg Brandleaa84ef2009-05-05 08:14:33 +00001244 :param angle: a number (optional)
1245
1246 Set or return the current tilt-angle. If angle is given, rotate the
1247 turtleshape to point in the direction specified by angle,
1248 regardless of its current tilt-angle. Do *not* change the turtle's
1249 heading (direction of movement).
1250 If angle is not given: return the current tilt-angle, i. e. the angle
1251 between the orientation of the turtleshape and the heading of the
1252 turtle (its direction of movement).
1253
R. David Murrayf877feb2009-05-05 02:08:52 +00001254 .. doctest::
1255
1256 >>> turtle.reset()
1257 >>> turtle.shape("circle")
1258 >>> turtle.shapesize(5,2)
1259 >>> turtle.tilt(45)
1260 >>> turtle.tiltangle()
1261 45.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001262
1263
Georg Brandleaa84ef2009-05-05 08:14:33 +00001264.. function:: shapetransform(t11=None, t12=None, t21=None, t22=None)
1265
1266 :param t11: a number (optional)
1267 :param t12: a number (optional)
1268 :param t21: a number (optional)
1269 :param t12: a number (optional)
1270
1271 Set or return the current transformation matrix of the turtle shape.
1272
1273 If none of the matrix elements are given, return the transformation
1274 matrix as a tuple of 4 elements.
1275 Otherwise set the given elements and transform the turtleshape
1276 according to the matrix consisting of first row t11, t12 and
1277 second row t21, 22. The determinant t11 * t22 - t12 * t21 must not be
1278 zero, otherwise an error is raised.
1279 Modify stretchfactor, shearfactor and tiltangle according to the
1280 given matrix.
1281
1282 .. doctest::
1283
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001284 >>> turtle = Turtle()
Georg Brandleaa84ef2009-05-05 08:14:33 +00001285 >>> turtle.shape("square")
1286 >>> turtle.shapesize(4,2)
1287 >>> turtle.shearfactor(-0.5)
1288 >>> turtle.shapetransform()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001289 (4.0, -1.0, -0.0, 2.0)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001290
1291
R. David Murray7fcd3de2009-06-25 14:26:19 +00001292.. function:: get_shapepoly()
Georg Brandleaa84ef2009-05-05 08:14:33 +00001293
1294 Return the current shape polygon as tuple of coordinate pairs. This
1295 can be used to define a new shape or components of a compound shape.
1296
1297 .. doctest::
1298
1299 >>> turtle.shape("square")
1300 >>> turtle.shapetransform(4, -1, 0, 2)
1301 >>> turtle.get_shapepoly()
1302 ((50, -20), (30, 20), (-50, 20), (-30, -20))
1303
1304
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001305Using events
1306------------
1307
1308.. function:: onclick(fun, btn=1, add=None)
1309
1310 :param fun: a function with two arguments which will be called with the
1311 coordinates of the clicked point on the canvas
1312 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1313 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1314 added, otherwise it will replace a former binding
1315
1316 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1317 existing bindings are removed. Example for the anonymous turtle, i.e. the
1318 procedural way:
1319
R. David Murrayf877feb2009-05-05 02:08:52 +00001320 .. doctest::
1321
1322 >>> def turn(x, y):
1323 ... left(180)
1324 ...
1325 >>> onclick(turn) # Now clicking into the turtle will turn it.
1326 >>> onclick(None) # event-binding will be removed
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001327
1328
1329.. function:: onrelease(fun, btn=1, add=None)
1330
1331 :param fun: a function with two arguments which will be called with the
1332 coordinates of the clicked point on the canvas
1333 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1334 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1335 added, otherwise it will replace a former binding
1336
1337 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1338 ``None``, existing bindings are removed.
1339
R. David Murrayf877feb2009-05-05 02:08:52 +00001340 .. doctest::
1341
1342 >>> class MyTurtle(Turtle):
1343 ... def glow(self,x,y):
1344 ... self.fillcolor("red")
1345 ... def unglow(self,x,y):
1346 ... self.fillcolor("")
1347 ...
1348 >>> turtle = MyTurtle()
1349 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1350 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001351
1352
1353.. function:: ondrag(fun, btn=1, add=None)
1354
1355 :param fun: a function with two arguments which will be called with the
1356 coordinates of the clicked point on the canvas
1357 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1358 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1359 added, otherwise it will replace a former binding
1360
1361 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1362 existing bindings are removed.
1363
1364 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1365 mouse-click event on that turtle.
1366
R. David Murrayf877feb2009-05-05 02:08:52 +00001367 .. doctest::
1368
1369 >>> turtle.ondrag(turtle.goto)
1370
1371 Subsequently, clicking and dragging the Turtle will move it across
1372 the screen thereby producing handdrawings (if pen is down).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001373
1374
1375Special Turtle methods
1376----------------------
1377
1378.. function:: begin_poly()
1379
1380 Start recording the vertices of a polygon. Current turtle position is first
1381 vertex of polygon.
1382
1383
1384.. function:: end_poly()
1385
1386 Stop recording the vertices of a polygon. Current turtle position is last
1387 vertex of polygon. This will be connected with the first vertex.
1388
1389
1390.. function:: get_poly()
1391
1392 Return the last recorded polygon.
1393
R. David Murrayf877feb2009-05-05 02:08:52 +00001394 .. doctest::
1395
1396 >>> turtle.home()
1397 >>> turtle.begin_poly()
1398 >>> turtle.fd(100)
1399 >>> turtle.left(20)
1400 >>> turtle.fd(30)
1401 >>> turtle.left(60)
1402 >>> turtle.fd(50)
1403 >>> turtle.end_poly()
1404 >>> p = turtle.get_poly()
1405 >>> register_shape("myFavouriteShape", p)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001406
1407
1408.. function:: clone()
1409
1410 Create and return a clone of the turtle with same position, heading and
1411 turtle properties.
1412
R. David Murrayf877feb2009-05-05 02:08:52 +00001413 .. doctest::
1414
1415 >>> mick = Turtle()
1416 >>> joe = mick.clone()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001417
1418
1419.. function:: getturtle()
R. David Murray7fcd3de2009-06-25 14:26:19 +00001420 getpen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001421
1422 Return the Turtle object itself. Only reasonable use: as a function to
1423 return the "anonymous turtle":
1424
R. David Murrayf877feb2009-05-05 02:08:52 +00001425 .. doctest::
1426
1427 >>> pet = getturtle()
1428 >>> pet.fd(50)
1429 >>> pet
1430 <turtle.Turtle object at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001431
1432
1433.. function:: getscreen()
1434
1435 Return the :class:`TurtleScreen` object the turtle is drawing on.
1436 TurtleScreen methods can then be called for that object.
1437
R. David Murrayf877feb2009-05-05 02:08:52 +00001438 .. doctest::
1439
1440 >>> ts = turtle.getscreen()
1441 >>> ts
1442 <turtle._Screen object at 0x...>
1443 >>> ts.bgcolor("pink")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001444
1445
1446.. function:: setundobuffer(size)
1447
1448 :param size: an integer or ``None``
1449
1450 Set or disable undobuffer. If *size* is an integer an empty undobuffer of
1451 given size is installed. *size* gives the maximum number of turtle actions
1452 that can be undone by the :func:`undo` method/function. If *size* is
1453 ``None``, the undobuffer is disabled.
1454
R. David Murrayf877feb2009-05-05 02:08:52 +00001455 .. doctest::
1456
1457 >>> turtle.setundobuffer(42)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001458
1459
1460.. function:: undobufferentries()
1461
1462 Return number of entries in the undobuffer.
1463
R. David Murrayf877feb2009-05-05 02:08:52 +00001464 .. doctest::
1465
1466 >>> while undobufferentries():
1467 ... undo()
1468
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001469
1470
1471.. _compoundshapes:
1472
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001473Compound shapes
Alexander Belopolsky41f56f02010-10-21 18:15:39 +00001474---------------
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001475
1476To use compound turtle shapes, which consist of several polygons of different
1477color, you must use the helper class :class:`Shape` explicitly as described
1478below:
1479
14801. Create an empty Shape object of type "compound".
14812. Add as many components to this object as desired, using the
1482 :meth:`addcomponent` method.
1483
1484 For example:
1485
R. David Murrayf877feb2009-05-05 02:08:52 +00001486 .. doctest::
1487
1488 >>> s = Shape("compound")
1489 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1490 >>> s.addcomponent(poly1, "red", "blue")
1491 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1492 >>> s.addcomponent(poly2, "blue", "red")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001493
14943. Now add the Shape to the Screen's shapelist and use it:
1495
R. David Murrayf877feb2009-05-05 02:08:52 +00001496 .. doctest::
1497
1498 >>> register_shape("myshape", s)
1499 >>> shape("myshape")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001500
1501
1502.. note::
1503
1504 The :class:`Shape` class is used internally by the :func:`register_shape`
1505 method in different ways. The application programmer has to deal with the
1506 Shape class *only* when using compound shapes like shown above!
1507
1508
1509Methods of TurtleScreen/Screen and corresponding functions
1510==========================================================
1511
1512Most of the examples in this section refer to a TurtleScreen instance called
1513``screen``.
1514
R. David Murrayf877feb2009-05-05 02:08:52 +00001515.. doctest::
1516 :hide:
1517
1518 >>> screen = Screen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001519
1520Window control
1521--------------
1522
1523.. function:: bgcolor(*args)
1524
1525 :param args: a color string or three numbers in the range 0..colormode or a
1526 3-tuple of such numbers
1527
Alexander Belopolsky3cdfb122010-10-29 17:16:49 +00001528
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001529 Set or return background color of the TurtleScreen.
1530
R. David Murrayf877feb2009-05-05 02:08:52 +00001531 .. doctest::
1532
1533 >>> screen.bgcolor("orange")
1534 >>> screen.bgcolor()
1535 'orange'
1536 >>> screen.bgcolor("#800080")
1537 >>> screen.bgcolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001538 (128.0, 0.0, 128.0)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001539
1540
1541.. function:: bgpic(picname=None)
1542
1543 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
1544
1545 Set background image or return name of current backgroundimage. If *picname*
1546 is a filename, set the corresponding image as background. If *picname* is
1547 ``"nopic"``, delete background image, if present. If *picname* is ``None``,
R. David Murrayf877feb2009-05-05 02:08:52 +00001548 return the filename of the current backgroundimage. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001549
R. David Murrayf877feb2009-05-05 02:08:52 +00001550 >>> screen.bgpic()
1551 'nopic'
1552 >>> screen.bgpic("landscape.gif")
1553 >>> screen.bgpic()
1554 "landscape.gif"
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001555
1556
1557.. function:: clear()
1558 clearscreen()
1559
1560 Delete all drawings and all turtles from the TurtleScreen. Reset the now
1561 empty TurtleScreen to its initial state: white background, no background
1562 image, no event bindings and tracing on.
1563
1564 .. note::
1565 This TurtleScreen method is available as a global function only under the
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001566 name ``clearscreen``. The global function ``clear`` is a different one
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001567 derived from the Turtle method ``clear``.
1568
1569
1570.. function:: reset()
1571 resetscreen()
1572
1573 Reset all Turtles on the Screen to their initial state.
1574
1575 .. note::
1576 This TurtleScreen method is available as a global function only under the
1577 name ``resetscreen``. The global function ``reset`` is another one
1578 derived from the Turtle method ``reset``.
1579
1580
1581.. function:: screensize(canvwidth=None, canvheight=None, bg=None)
1582
Georg Brandlff2ad0e2009-04-27 16:51:45 +00001583 :param canvwidth: positive integer, new width of canvas in pixels
1584 :param canvheight: positive integer, new height of canvas in pixels
1585 :param bg: colorstring or color-tuple, new background color
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001586
1587 If no arguments are given, return current (canvaswidth, canvasheight). Else
1588 resize the canvas the turtles are drawing on. Do not alter the drawing
1589 window. To observe hidden parts of the canvas, use the scrollbars. With this
1590 method, one can make visible those parts of a drawing which were outside the
1591 canvas before.
1592
R. David Murrayf877feb2009-05-05 02:08:52 +00001593 >>> screen.screensize()
1594 (400, 300)
1595 >>> screen.screensize(2000,1500)
1596 >>> screen.screensize()
1597 (2000, 1500)
1598
1599 e.g. to search for an erroneously escaped turtle ;-)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001600
1601
1602.. function:: setworldcoordinates(llx, lly, urx, ury)
1603
1604 :param llx: a number, x-coordinate of lower left corner of canvas
1605 :param lly: a number, y-coordinate of lower left corner of canvas
1606 :param urx: a number, x-coordinate of upper right corner of canvas
1607 :param ury: a number, y-coordinate of upper right corner of canvas
1608
1609 Set up user-defined coordinate system and switch to mode "world" if
1610 necessary. This performs a ``screen.reset()``. If mode "world" is already
1611 active, all drawings are redrawn according to the new coordinates.
1612
1613 **ATTENTION**: in user-defined coordinate systems angles may appear
1614 distorted.
1615
R. David Murrayf877feb2009-05-05 02:08:52 +00001616 .. doctest::
1617
1618 >>> screen.reset()
1619 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1620 >>> for _ in range(72):
1621 ... left(10)
1622 ...
1623 >>> for _ in range(8):
1624 ... left(45); fd(2) # a regular octagon
1625
1626 .. doctest::
1627 :hide:
1628
1629 >>> screen.reset()
1630 >>> for t in turtles():
1631 ... t.reset()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001632
1633
1634Animation control
1635-----------------
1636
1637.. function:: delay(delay=None)
1638
1639 :param delay: positive integer
1640
1641 Set or return the drawing *delay* in milliseconds. (This is approximately
Georg Brandl2ee470f2008-07-16 12:55:28 +00001642 the time interval between two consecutive canvas updates.) The longer the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001643 drawing delay, the slower the animation.
1644
1645 Optional argument:
1646
R. David Murrayf877feb2009-05-05 02:08:52 +00001647 .. doctest::
1648
1649 >>> screen.delay()
1650 10
1651 >>> screen.delay(5)
1652 >>> screen.delay()
1653 5
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001654
1655
1656.. function:: tracer(n=None, delay=None)
1657
1658 :param n: nonnegative integer
1659 :param delay: nonnegative integer
1660
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001661 Turn turtle animation on/off and set delay for update drawings. If
1662 *n* is given, only each n-th regular screen update is really
1663 performed. (Can be used to accelerate the drawing of complex
1664 graphics.) When called without arguments, returns the currently
1665 stored value of n. Second argument sets delay value (see
1666 :func:`delay`).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001667
R. David Murrayf877feb2009-05-05 02:08:52 +00001668 .. doctest::
1669
1670 >>> screen.tracer(8, 25)
1671 >>> dist = 2
1672 >>> for i in range(200):
1673 ... fd(dist)
1674 ... rt(90)
1675 ... dist += 2
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001676
1677
1678.. function:: update()
1679
1680 Perform a TurtleScreen update. To be used when tracer is turned off.
1681
1682See also the RawTurtle/Turtle method :func:`speed`.
1683
1684
1685Using screen events
1686-------------------
1687
1688.. function:: listen(xdummy=None, ydummy=None)
1689
1690 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1691 are provided in order to be able to pass :func:`listen` to the onclick method.
1692
1693
1694.. function:: onkey(fun, key)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001695 onkeyrelease(fun, key)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001696
1697 :param fun: a function with no arguments or ``None``
1698 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1699
1700 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1701 are removed. Remark: in order to be able to register key-events, TurtleScreen
1702 must have the focus. (See method :func:`listen`.)
1703
R. David Murrayf877feb2009-05-05 02:08:52 +00001704 .. doctest::
1705
1706 >>> def f():
1707 ... fd(50)
1708 ... lt(60)
1709 ...
1710 >>> screen.onkey(f, "Up")
1711 >>> screen.listen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001712
1713
R. David Murray7fcd3de2009-06-25 14:26:19 +00001714.. function:: onkeypress(fun, key=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001715
1716 :param fun: a function with no arguments or ``None``
1717 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1718
1719 Bind *fun* to key-press event of key if key is given,
1720 or to any key-press-event if no key is given.
1721 Remark: in order to be able to register key-events, TurtleScreen
1722 must have focus. (See method :func:`listen`.)
1723
1724 .. doctest::
1725
1726 >>> def f():
1727 ... fd(50)
1728 ...
1729 >>> screen.onkey(f, "Up")
1730 >>> screen.listen()
1731
1732
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001733.. function:: onclick(fun, btn=1, add=None)
1734 onscreenclick(fun, btn=1, add=None)
1735
1736 :param fun: a function with two arguments which will be called with the
1737 coordinates of the clicked point on the canvas
1738 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1739 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1740 added, otherwise it will replace a former binding
1741
1742 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1743 existing bindings are removed.
1744
1745 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1746 named turtle:
1747
R. David Murrayf877feb2009-05-05 02:08:52 +00001748 .. doctest::
1749
1750 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1751 >>> # make the turtle move to the clicked point.
1752 >>> screen.onclick(None) # remove event binding again
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001753
1754 .. note::
1755 This TurtleScreen method is available as a global function only under the
1756 name ``onscreenclick``. The global function ``onclick`` is another one
1757 derived from the Turtle method ``onclick``.
1758
1759
1760.. function:: ontimer(fun, t=0)
1761
1762 :param fun: a function with no arguments
1763 :param t: a number >= 0
1764
1765 Install a timer that calls *fun* after *t* milliseconds.
1766
R. David Murrayf877feb2009-05-05 02:08:52 +00001767 .. doctest::
1768
1769 >>> running = True
1770 >>> def f():
1771 ... if running:
1772 ... fd(50)
1773 ... lt(60)
1774 ... screen.ontimer(f, 250)
1775 >>> f() ### makes the turtle march around
1776 >>> running = False
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001777
1778
Georg Brandleaa84ef2009-05-05 08:14:33 +00001779.. function:: mainloop()
Sandro Tosie3484552011-10-31 10:12:43 +01001780 done()
Georg Brandleaa84ef2009-05-05 08:14:33 +00001781
1782 Starts event loop - calling Tkinter's mainloop function.
1783 Must be the last statement in a turtle graphics program.
1784 Must *not* be used if a script is run from within IDLE in -n mode
1785 (No subprocess) - for interactive use of turtle graphics. ::
1786
1787 >>> screen.mainloop()
1788
1789
1790Input methods
1791-------------
1792
1793.. function:: textinput(title, prompt)
1794
1795 :param title: string
1796 :param prompt: string
1797
1798 Pop up a dialog window for input of a string. Parameter title is
delirious-lettuce3378b202017-05-19 14:37:57 -06001799 the title of the dialog window, prompt is a text mostly describing
Georg Brandleaa84ef2009-05-05 08:14:33 +00001800 what information to input.
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03001801 Return the string input. If the dialog is canceled, return ``None``. ::
Georg Brandleaa84ef2009-05-05 08:14:33 +00001802
1803 >>> screen.textinput("NIM", "Name of first player:")
1804
1805
R. David Murray7fcd3de2009-06-25 14:26:19 +00001806.. function:: numinput(title, prompt, default=None, minval=None, maxval=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001807
1808 :param title: string
1809 :param prompt: string
1810 :param default: number (optional)
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001811 :param minval: number (optional)
1812 :param maxval: number (optional)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001813
1814 Pop up a dialog window for input of a number. title is the title of the
1815 dialog window, prompt is a text mostly describing what numerical information
Donald Stufft8b852f12014-05-20 12:58:38 -04001816 to input. default: default value, minval: minimum value for input,
Georg Brandleaa84ef2009-05-05 08:14:33 +00001817 maxval: maximum value for input
1818 The number input must be in the range minval .. maxval if these are
1819 given. If not, a hint is issued and the dialog remains open for
1820 correction.
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03001821 Return the number input. If the dialog is canceled, return ``None``. ::
Georg Brandleaa84ef2009-05-05 08:14:33 +00001822
1823 >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000)
1824
1825
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001826Settings and special methods
1827----------------------------
1828
1829.. function:: mode(mode=None)
1830
1831 :param mode: one of the strings "standard", "logo" or "world"
1832
1833 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1834 is not given, current mode is returned.
1835
1836 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1837 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1838 "world coordinates". **Attention**: in this mode angles appear distorted if
1839 ``x/y`` unit-ratio doesn't equal 1.
1840
1841 ============ ========================= ===================
1842 Mode Initial turtle heading positive angles
1843 ============ ========================= ===================
1844 "standard" to the right (east) counterclockwise
1845 "logo" upward (north) clockwise
1846 ============ ========================= ===================
1847
R. David Murrayf877feb2009-05-05 02:08:52 +00001848 .. doctest::
1849
1850 >>> mode("logo") # resets turtle heading to north
1851 >>> mode()
1852 'logo'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001853
1854
1855.. function:: colormode(cmode=None)
1856
1857 :param cmode: one of the values 1.0 or 255
1858
1859 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1860 values of color triples have to be in the range 0..\ *cmode*.
1861
R. David Murrayf877feb2009-05-05 02:08:52 +00001862 .. doctest::
1863
1864 >>> screen.colormode(1)
1865 >>> turtle.pencolor(240, 160, 80)
1866 Traceback (most recent call last):
1867 ...
1868 TurtleGraphicsError: bad color sequence: (240, 160, 80)
1869 >>> screen.colormode()
1870 1.0
1871 >>> screen.colormode(255)
1872 >>> screen.colormode()
1873 255
1874 >>> turtle.pencolor(240,160,80)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001875
1876
1877.. function:: getcanvas()
1878
1879 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1880 do with a Tkinter Canvas.
1881
R. David Murrayf877feb2009-05-05 02:08:52 +00001882 .. doctest::
1883
1884 >>> cv = screen.getcanvas()
1885 >>> cv
Serhiy Storchakabcc17462014-04-04 15:45:02 +03001886 <turtle.ScrolledCanvas object ...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001887
1888
1889.. function:: getshapes()
1890
1891 Return a list of names of all currently available turtle shapes.
1892
R. David Murrayf877feb2009-05-05 02:08:52 +00001893 .. doctest::
1894
1895 >>> screen.getshapes()
1896 ['arrow', 'blank', 'circle', ..., 'turtle']
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001897
1898
1899.. function:: register_shape(name, shape=None)
1900 addshape(name, shape=None)
1901
1902 There are three different ways to call this function:
1903
1904 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
R. David Murrayf877feb2009-05-05 02:08:52 +00001905 corresponding image shape. ::
1906
1907 >>> screen.register_shape("turtle.gif")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001908
1909 .. note::
1910 Image shapes *do not* rotate when turning the turtle, so they do not
1911 display the heading of the turtle!
1912
1913 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1914 coordinates: Install the corresponding polygon shape.
1915
R. David Murrayf877feb2009-05-05 02:08:52 +00001916 .. doctest::
1917
1918 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
1919
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001920 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
1921 object: Install the corresponding compound shape.
1922
1923 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1924 shapes can be used by issuing the command ``shape(shapename)``.
1925
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001926
1927.. function:: turtles()
1928
1929 Return the list of turtles on the screen.
1930
R. David Murrayf877feb2009-05-05 02:08:52 +00001931 .. doctest::
1932
1933 >>> for turtle in screen.turtles():
1934 ... turtle.color("red")
Georg Brandl116aa622007-08-15 14:28:22 +00001935
Georg Brandl116aa622007-08-15 14:28:22 +00001936
1937.. function:: window_height()
1938
R. David Murrayf877feb2009-05-05 02:08:52 +00001939 Return the height of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001940
R. David Murrayf877feb2009-05-05 02:08:52 +00001941 >>> screen.window_height()
1942 480
Georg Brandl116aa622007-08-15 14:28:22 +00001943
Georg Brandl116aa622007-08-15 14:28:22 +00001944
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001945.. function:: window_width()
1946
R. David Murrayf877feb2009-05-05 02:08:52 +00001947 Return the width of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001948
R. David Murrayf877feb2009-05-05 02:08:52 +00001949 >>> screen.window_width()
1950 640
Georg Brandl116aa622007-08-15 14:28:22 +00001951
1952
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001953.. _screenspecific:
Georg Brandl116aa622007-08-15 14:28:22 +00001954
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001955Methods specific to Screen, not inherited from TurtleScreen
1956-----------------------------------------------------------
1957
1958.. function:: bye()
1959
1960 Shut the turtlegraphics window.
Georg Brandl116aa622007-08-15 14:28:22 +00001961
1962
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001963.. function:: exitonclick()
Georg Brandl116aa622007-08-15 14:28:22 +00001964
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001965 Bind bye() method to mouse clicks on the Screen.
Georg Brandl116aa622007-08-15 14:28:22 +00001966
1967
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001968 If the value "using_IDLE" in the configuration dictionary is ``False``
1969 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
1970 (no subprocess) is used, this value should be set to ``True`` in
1971 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1972 client script.
Georg Brandl116aa622007-08-15 14:28:22 +00001973
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001974
1975.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
1976
1977 Set the size and position of the main window. Default values of arguments
Georg Brandl6faee4e2010-09-21 14:48:28 +00001978 are stored in the configuration dictionary and can be changed via a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001979 :file:`turtle.cfg` file.
1980
1981 :param width: if an integer, a size in pixels, if a float, a fraction of the
1982 screen; default is 50% of screen
1983 :param height: if an integer, the height in pixels, if a float, a fraction of
1984 the screen; default is 75% of screen
1985 :param startx: if positive, starting position in pixels from the left
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03001986 edge of the screen, if negative from the right edge, if ``None``,
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001987 center window horizontally
Zachary Ware8faecbf2014-07-16 14:48:48 -05001988 :param starty: if positive, starting position in pixels from the top
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03001989 edge of the screen, if negative from the bottom edge, if ``None``,
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001990 center window vertically
1991
R. David Murrayf877feb2009-05-05 02:08:52 +00001992 .. doctest::
1993
1994 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1995 >>> # sets window to 200x200 pixels, in upper left of screen
1996 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1997 >>> # sets window to 75% of screen by 50% of screen and centers
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001998
1999
2000.. function:: title(titlestring)
2001
2002 :param titlestring: a string that is shown in the titlebar of the turtle
2003 graphics window
2004
2005 Set title of turtle window to *titlestring*.
2006
R. David Murrayf877feb2009-05-05 02:08:52 +00002007 .. doctest::
2008
2009 >>> screen.title("Welcome to the turtle zoo!")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002010
2011
Alexander Belopolsky65095992010-11-01 15:45:34 +00002012Public classes
2013==============
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002014
2015
2016.. class:: RawTurtle(canvas)
2017 RawPen(canvas)
2018
Ezio Melotti1a263ad2010-03-14 09:51:37 +00002019 :param canvas: a :class:`tkinter.Canvas`, a :class:`ScrolledCanvas` or a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002020 :class:`TurtleScreen`
2021
R. David Murrayf877feb2009-05-05 02:08:52 +00002022 Create a turtle. The turtle has all methods described above as "methods of
2023 Turtle/RawTurtle".
Georg Brandl116aa622007-08-15 14:28:22 +00002024
2025
2026.. class:: Turtle()
2027
R. David Murrayf877feb2009-05-05 02:08:52 +00002028 Subclass of RawTurtle, has the same interface but draws on a default
2029 :class:`Screen` object created automatically when needed for the first time.
Georg Brandl116aa622007-08-15 14:28:22 +00002030
2031
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002032.. class:: TurtleScreen(cv)
Georg Brandl116aa622007-08-15 14:28:22 +00002033
Ezio Melotti1a263ad2010-03-14 09:51:37 +00002034 :param cv: a :class:`tkinter.Canvas`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002035
2036 Provides screen oriented methods like :func:`setbg` etc. that are described
2037 above.
2038
2039.. class:: Screen()
2040
2041 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
2042
Georg Brandl48310cd2009-01-03 21:18:54 +00002043
Benjamin Petersona0dfa822009-11-13 02:25:08 +00002044.. class:: ScrolledCanvas(master)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002045
2046 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
2047 a Tkinter-canvas with scrollbars added
2048
2049 Used by class Screen, which thus automatically provides a ScrolledCanvas as
2050 playground for the turtles.
2051
2052.. class:: Shape(type_, data)
2053
2054 :param type\_: one of the strings "polygon", "image", "compound"
2055
2056 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
2057 specification:
Georg Brandl116aa622007-08-15 14:28:22 +00002058
2059
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002060 =========== ===========
2061 *type_* *data*
2062 =========== ===========
2063 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
2064 "image" an image (in this form only used internally!)
Georg Brandlae2dbe22009-03-13 19:04:40 +00002065 "compound" ``None`` (a compound shape has to be constructed using the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002066 :meth:`addcomponent` method)
2067 =========== ===========
Georg Brandl48310cd2009-01-03 21:18:54 +00002068
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002069 .. method:: addcomponent(poly, fill, outline=None)
Georg Brandl116aa622007-08-15 14:28:22 +00002070
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002071 :param poly: a polygon, i.e. a tuple of pairs of numbers
2072 :param fill: a color the *poly* will be filled with
2073 :param outline: a color for the poly's outline (if given)
Georg Brandl48310cd2009-01-03 21:18:54 +00002074
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002075 Example:
Georg Brandl116aa622007-08-15 14:28:22 +00002076
R. David Murrayf877feb2009-05-05 02:08:52 +00002077 .. doctest::
2078
2079 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
2080 >>> s = Shape("compound")
2081 >>> s.addcomponent(poly, "red", "blue")
2082 >>> # ... add more components and then use register_shape()
Georg Brandl116aa622007-08-15 14:28:22 +00002083
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002084 See :ref:`compoundshapes`.
Georg Brandl116aa622007-08-15 14:28:22 +00002085
2086
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002087.. class:: Vec2D(x, y)
Georg Brandl116aa622007-08-15 14:28:22 +00002088
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002089 A two-dimensional vector class, used as a helper class for implementing
2090 turtle graphics. May be useful for turtle graphics programs too. Derived
2091 from tuple, so a vector is a tuple!
2092
2093 Provides (for *a*, *b* vectors, *k* number):
2094
2095 * ``a + b`` vector addition
2096 * ``a - b`` vector subtraction
2097 * ``a * b`` inner product
2098 * ``k * a`` and ``a * k`` multiplication with scalar
2099 * ``abs(a)`` absolute value of a
2100 * ``a.rotate(angle)`` rotation
2101
2102
2103Help and configuration
2104======================
2105
2106How to use help
2107---------------
2108
2109The public methods of the Screen and Turtle classes are documented extensively
2110via docstrings. So these can be used as online-help via the Python help
2111facilities:
2112
2113- When using IDLE, tooltips show the signatures and first lines of the
2114 docstrings of typed in function-/method calls.
2115
2116- Calling :func:`help` on methods or functions displays the docstrings::
2117
2118 >>> help(Screen.bgcolor)
2119 Help on method bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002120
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002121 bgcolor(self, *args) unbound turtle.Screen method
2122 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002123
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002124 Arguments (if given): a color string or three numbers
2125 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002126
2127
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002128 >>> screen.bgcolor("orange")
2129 >>> screen.bgcolor()
2130 "orange"
2131 >>> screen.bgcolor(0.5,0,0.5)
2132 >>> screen.bgcolor()
2133 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002134
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002135 >>> help(Turtle.penup)
2136 Help on method penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002137
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002138 penup(self) unbound turtle.Turtle method
2139 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002140
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002141 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002142
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002143 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002144
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002145 >>> turtle.penup()
2146
2147- The docstrings of the functions which are derived from methods have a modified
2148 form::
2149
2150 >>> help(bgcolor)
2151 Help on function bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002152
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002153 bgcolor(*args)
2154 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002155
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002156 Arguments (if given): a color string or three numbers
2157 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002158
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002159 Example::
Georg Brandl48310cd2009-01-03 21:18:54 +00002160
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002161 >>> bgcolor("orange")
2162 >>> bgcolor()
2163 "orange"
2164 >>> bgcolor(0.5,0,0.5)
2165 >>> bgcolor()
2166 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002167
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002168 >>> help(penup)
2169 Help on function penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002170
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002171 penup()
2172 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002173
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002174 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002175
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002176 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002177
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002178 Example:
2179 >>> penup()
2180
2181These modified docstrings are created automatically together with the function
2182definitions that are derived from the methods at import time.
2183
2184
2185Translation of docstrings into different languages
2186--------------------------------------------------
2187
2188There is a utility to create a dictionary the keys of which are the method names
2189and the values of which are the docstrings of the public methods of the classes
2190Screen and Turtle.
2191
2192.. function:: write_docstringdict(filename="turtle_docstringdict")
2193
2194 :param filename: a string, used as filename
2195
2196 Create and write docstring-dictionary to a Python script with the given
2197 filename. This function has to be called explicitly (it is not used by the
2198 turtle graphics classes). The docstring dictionary will be written to the
2199 Python script :file:`{filename}.py`. It is intended to serve as a template
2200 for translation of the docstrings into different languages.
2201
2202If you (or your students) want to use :mod:`turtle` with online help in your
2203native language, you have to translate the docstrings and save the resulting
2204file as e.g. :file:`turtle_docstringdict_german.py`.
2205
2206If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2207will be read in at import time and will replace the original English docstrings.
2208
2209At the time of this writing there are docstring dictionaries in German and in
2210Italian. (Requests please to glingl@aon.at.)
2211
2212
2213
2214How to configure Screen and Turtles
2215-----------------------------------
2216
2217The built-in default configuration mimics the appearance and behaviour of the
2218old turtle module in order to retain best possible compatibility with it.
2219
2220If you want to use a different configuration which better reflects the features
2221of this module or which better fits to your needs, e.g. for use in a classroom,
2222you can prepare a configuration file ``turtle.cfg`` which will be read at import
2223time and modify the configuration according to its settings.
2224
2225The built in configuration would correspond to the following turtle.cfg::
2226
2227 width = 0.5
2228 height = 0.75
2229 leftright = None
2230 topbottom = None
2231 canvwidth = 400
2232 canvheight = 300
2233 mode = standard
2234 colormode = 1.0
2235 delay = 10
2236 undobuffersize = 1000
2237 shape = classic
2238 pencolor = black
2239 fillcolor = black
2240 resizemode = noresize
2241 visible = True
2242 language = english
2243 exampleturtle = turtle
2244 examplescreen = screen
2245 title = Python Turtle Graphics
2246 using_IDLE = False
2247
2248Short explanation of selected entries:
2249
2250- The first four lines correspond to the arguments of the :meth:`Screen.setup`
2251 method.
2252- Line 5 and 6 correspond to the arguments of the method
2253 :meth:`Screen.screensize`.
2254- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2255 info try ``help(shape)``.
2256- If you want to use no fillcolor (i.e. make the turtle transparent), you have
2257 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
2258 the cfg-file).
2259- If you want to reflect the turtle its state, you have to use ``resizemode =
2260 auto``.
2261- If you set e.g. ``language = italian`` the docstringdict
2262 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
2263 present on the import path, e.g. in the same directory as :mod:`turtle`.
2264- The entries *exampleturtle* and *examplescreen* define the names of these
2265 objects as they occur in the docstrings. The transformation of
2266 method-docstrings to function-docstrings will delete these names from the
2267 docstrings.
2268- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
2269 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
2270 mainloop.
2271
2272There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2273stored and an additional one in the current working directory. The latter will
2274override the settings of the first one.
2275
Georg Brandl59b44722010-12-30 22:12:40 +00002276The :file:`Lib/turtledemo` directory contains a :file:`turtle.cfg` file. You can
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002277study it as an example and see its effects when running the demos (preferably
2278not from within the demo-viewer).
2279
2280
Terry Jan Reedy6e978d22014-10-02 00:16:31 -04002281:mod:`turtledemo` --- Demo scripts
2282==================================
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002283
Terry Jan Reedy6e978d22014-10-02 00:16:31 -04002284.. module:: turtledemo
2285 :synopsis: A viewer for example turtle scripts
2286
2287The :mod:`turtledemo` package includes a set of demo scripts. These
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002288scripts can be run and viewed using the supplied demo viewer as follows::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002289
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002290 python -m turtledemo
2291
Alexander Belopolskye1f849c2010-11-09 03:13:43 +00002292Alternatively, you can run the demo scripts individually. For example, ::
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002293
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002294 python -m turtledemo.bytedesign
2295
2296The :mod:`turtledemo` package directory contains:
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002297
Terry Jan Reedy6e978d22014-10-02 00:16:31 -04002298- A demo viewer :file:`__main__.py` which can be used to view the sourcecode
2299 of the scripts and run them at the same time.
2300- Multiple scripts demonstrating different features of the :mod:`turtle`
2301 module. Examples can be accessed via the Examples menu. They can also
2302 be run standalone.
2303- A :file:`turtle.cfg` file which serves as an example of how to write
2304 and use such files.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002305
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002306The demo scripts are:
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002307
Georg Brandl44ea77b2013-03-28 13:28:44 +01002308.. tabularcolumns:: |l|L|L|
2309
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002310+----------------+------------------------------+-----------------------+
2311| Name | Description | Features |
Georg Brandl44ea77b2013-03-28 13:28:44 +01002312+================+==============================+=======================+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002313| bytedesign | complex classical | :func:`tracer`, delay,|
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002314| | turtle graphics pattern | :func:`update` |
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002315+----------------+------------------------------+-----------------------+
Georg Brandlda227192011-03-06 10:53:55 +01002316| chaos | graphs Verhulst dynamics, | world coordinates |
2317| | shows that computer's | |
2318| | computations can generate | |
2319| | results sometimes against the| |
2320| | common sense expectations | |
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002321+----------------+------------------------------+-----------------------+
2322| clock | analog clock showing time | turtles as clock's |
2323| | of your computer | hands, ontimer |
2324+----------------+------------------------------+-----------------------+
2325| colormixer | experiment with r, g, b | :func:`ondrag` |
2326+----------------+------------------------------+-----------------------+
Terry Jan Reedy6e978d22014-10-02 00:16:31 -04002327| forest | 3 breadth-first trees | randomization |
2328+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002329| fractalcurves | Hilbert & Koch curves | recursion |
2330+----------------+------------------------------+-----------------------+
2331| lindenmayer | ethnomathematics | L-System |
2332| | (indian kolams) | |
2333+----------------+------------------------------+-----------------------+
2334| minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
2335| | | as Hanoi discs |
2336| | | (shape, shapesize) |
2337+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002338| nim | play the classical nim game | turtles as nimsticks, |
2339| | with three heaps of sticks | event driven (mouse, |
2340| | against the computer. | keyboard) |
2341+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002342| paint | super minimalistic | :func:`onclick` |
2343| | drawing program | |
2344+----------------+------------------------------+-----------------------+
2345| peace | elementary | turtle: appearance |
2346| | | and animation |
2347+----------------+------------------------------+-----------------------+
2348| penrose | aperiodic tiling with | :func:`stamp` |
2349| | kites and darts | |
2350+----------------+------------------------------+-----------------------+
2351| planet_and_moon| simulation of | compound shapes, |
2352| | gravitational system | :class:`Vec2D` |
2353+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002354| round_dance | dancing turtles rotating | compound shapes, clone|
2355| | pairwise in opposite | shapesize, tilt, |
Alexander Belopolskyc08f5442010-10-21 22:29:36 +00002356| | direction | get_shapepoly, update |
Georg Brandleaa84ef2009-05-05 08:14:33 +00002357+----------------+------------------------------+-----------------------+
Ethan Furman738f8052015-03-02 12:29:58 -08002358| sorting_animate| visual demonstration of | simple alignment, |
2359| | different sorting methods | randomization |
2360+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002361| tree | a (graphical) breadth | :func:`clone` |
2362| | first tree (using generators)| |
2363+----------------+------------------------------+-----------------------+
Terry Jan Reedy6e978d22014-10-02 00:16:31 -04002364| two_canvases | simple design | turtles on two |
2365| | | canvases |
2366+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002367| wikipedia | a pattern from the wikipedia | :func:`clone`, |
2368| | article on turtle graphics | :func:`undo` |
2369+----------------+------------------------------+-----------------------+
2370| yingyang | another elementary example | :func:`circle` |
2371+----------------+------------------------------+-----------------------+
2372
2373Have fun!
2374
2375
2376Changes since Python 2.6
2377========================
2378
Georg Brandl48310cd2009-01-03 21:18:54 +00002379- The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and
2380 :meth:`Turtle.window_height` have been eliminated.
2381 Methods with these names and functionality are now available only
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002382 as methods of :class:`Screen`. The functions derived from these remain
Georg Brandl48310cd2009-01-03 21:18:54 +00002383 available. (In fact already in Python 2.6 these methods were merely
2384 duplications of the corresponding
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002385 :class:`TurtleScreen`/:class:`Screen`-methods.)
2386
Georg Brandl48310cd2009-01-03 21:18:54 +00002387- The method :meth:`Turtle.fill` has been eliminated.
2388 The behaviour of :meth:`begin_fill` and :meth:`end_fill`
2389 have changed slightly: now every filling-process must be completed with an
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002390 ``end_fill()`` call.
Georg Brandl48310cd2009-01-03 21:18:54 +00002391
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002392- A method :meth:`Turtle.filling` has been added. It returns a boolean
2393 value: ``True`` if a filling process is under way, ``False`` otherwise.
2394 This behaviour corresponds to a ``fill()`` call without arguments in
Georg Brandl23d11d32008-09-21 07:50:52 +00002395 Python 2.6.
Georg Brandl116aa622007-08-15 14:28:22 +00002396
Georg Brandleaa84ef2009-05-05 08:14:33 +00002397Changes since Python 3.0
2398========================
2399
2400- The methods :meth:`Turtle.shearfactor`, :meth:`Turtle.shapetransform` and
2401 :meth:`Turtle.get_shapepoly` have been added. Thus the full range of
2402 regular linear transforms is now available for transforming turtle shapes.
2403 :meth:`Turtle.tiltangle` has been enhanced in functionality: it now can
2404 be used to get or set the tiltangle. :meth:`Turtle.settiltangle` has been
2405 deprecated.
2406
2407- The method :meth:`Screen.onkeypress` has been added as a complement to
2408 :meth:`Screen.onkey` which in fact binds actions to the keyrelease event.
2409 Accordingly the latter has got an alias: :meth:`Screen.onkeyrelease`.
2410
2411- The method :meth:`Screen.mainloop` has been added. So when working only
Donald Stufft8b852f12014-05-20 12:58:38 -04002412 with Screen and Turtle objects one must not additionally import
Georg Brandleaa84ef2009-05-05 08:14:33 +00002413 :func:`mainloop` anymore.
2414
2415- Two input methods has been added :meth:`Screen.textinput` and
2416 :meth:`Screen.numinput`. These popup input dialogs and return
2417 strings and numbers respectively.
2418
2419- Two example scripts :file:`tdemo_nim.py` and :file:`tdemo_round_dance.py`
Georg Brandl59b44722010-12-30 22:12:40 +00002420 have been added to the :file:`Lib/turtledemo` directory.
Georg Brandleaa84ef2009-05-05 08:14:33 +00002421
R. David Murrayf877feb2009-05-05 02:08:52 +00002422
2423.. doctest::
2424 :hide:
2425
2426 >>> for turtle in turtles():
2427 ... turtle.reset()
2428 >>> turtle.penup()
2429 >>> turtle.goto(-200,25)
2430 >>> turtle.pendown()
2431 >>> turtle.write("No one expects the Spanish Inquisition!",
2432 ... font=("Arial", 20, "normal"))
2433 >>> turtle.penup()
2434 >>> turtle.goto(-100,-50)
2435 >>> turtle.pendown()
2436 >>> turtle.write("Our two chief Turtles are...",
2437 ... font=("Arial", 16, "normal"))
2438 >>> turtle.penup()
2439 >>> turtle.goto(-450,-75)
2440 >>> turtle.write(str(turtles()))