blob: c6ba3165ca74ce33bb987ef49a633b1740af3b29 [file] [log] [blame]
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001========================================
2:mod:`turtle` --- Turtle graphics for Tk
3========================================
Georg Brandl116aa622007-08-15 14:28:22 +00004
Georg Brandl23d11d32008-09-21 07:50:52 +00005.. module:: turtle
Georg Brandl2ee470f2008-07-16 12:55:28 +00006 :synopsis: Turtle graphics for Tk
7.. sectionauthor:: Gregor Lingl <gregor.lingl@aon.at>
8
R. David Murrayf877feb2009-05-05 02:08:52 +00009.. testsetup:: default
10
11 from turtle import *
12 turtle = Turtle()
13
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000014Introduction
15============
16
17Turtle graphics is a popular way for introducing programming to kids. It was
18part of the original Logo programming language developed by Wally Feurzig and
19Seymour Papert in 1966.
20
21Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it the
22command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
23direction it is facing, drawing a line as it moves. Give it the command
24``turtle.left(25)``, and it rotates in-place 25 degrees clockwise.
25
26By combining together these and similar commands, intricate shapes and pictures
27can easily be drawn.
28
29The :mod:`turtle` module is an extended reimplementation of the same-named
30module from the Python standard distribution up to version Python 2.5.
31
32It tries to keep the merits of the old turtle module and to be (nearly) 100%
33compatible with it. This means in the first place to enable the learning
34programmer to use all the commands, classes and methods interactively when using
35the module from within IDLE run with the ``-n`` switch.
36
37The turtle module provides turtle graphics primitives, in both object-oriented
Ezio Melotti8209fcc2010-03-14 09:53:34 +000038and procedure-oriented ways. Because it uses :mod:`tkinter` for the underlying
Ezio Melotti890c1932009-12-19 23:33:46 +000039graphics, it needs a version of Python installed with Tk support.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000040
41The object-oriented interface uses essentially two+two classes:
42
431. The :class:`TurtleScreen` class defines graphics windows as a playground for
Ezio Melotti8209fcc2010-03-14 09:53:34 +000044 the drawing turtles. Its constructor needs a :class:`tkinter.Canvas` or a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000045 :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is
46 used as part of some application.
47
Martin v. Löwis601149b2008-09-29 22:19:08 +000048 The function :func:`Screen` returns a singleton object of a
49 :class:`TurtleScreen` subclass. This function should be used when
50 :mod:`turtle` is used as a standalone tool for doing graphics.
51 As a singleton object, inheriting from its class is not possible.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000052
53 All methods of TurtleScreen/Screen also exist as functions, i.e. as part of
54 the procedure-oriented interface.
55
562. :class:`RawTurtle` (alias: :class:`RawPen`) defines Turtle objects which draw
57 on a :class:`TurtleScreen`. Its constructor needs a Canvas, ScrolledCanvas
58 or TurtleScreen as argument, so the RawTurtle objects know where to draw.
59
60 Derived from RawTurtle is the subclass :class:`Turtle` (alias: :class:`Pen`),
61 which draws on "the" :class:`Screen` - instance which is automatically
62 created, if not already present.
63
64 All methods of RawTurtle/Turtle also exist as functions, i.e. part of the
65 procedure-oriented interface.
66
67The procedural interface provides functions which are derived from the methods
68of the classes :class:`Screen` and :class:`Turtle`. They have the same names as
Georg Brandlae2dbe22009-03-13 19:04:40 +000069the corresponding methods. A screen object is automatically created whenever a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000070function derived from a Screen method is called. An (unnamed) turtle object is
71automatically created whenever any of the functions derived from a Turtle method
72is called.
73
74To use multiple turtles an a screen one has to use the object-oriented interface.
75
76.. note::
77 In the following documentation the argument list for functions is given.
78 Methods, of course, have the additional first argument *self* which is
79 omitted here.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000082Overview over available Turtle and Screen methods
83=================================================
84
85Turtle methods
86--------------
87
88Turtle motion
89 Move and draw
90 | :func:`forward` | :func:`fd`
91 | :func:`backward` | :func:`bk` | :func:`back`
92 | :func:`right` | :func:`rt`
93 | :func:`left` | :func:`lt`
94 | :func:`goto` | :func:`setpos` | :func:`setposition`
95 | :func:`setx`
96 | :func:`sety`
97 | :func:`setheading` | :func:`seth`
98 | :func:`home`
99 | :func:`circle`
100 | :func:`dot`
101 | :func:`stamp`
102 | :func:`clearstamp`
103 | :func:`clearstamps`
104 | :func:`undo`
105 | :func:`speed`
106
107 Tell Turtle's state
108 | :func:`position` | :func:`pos`
109 | :func:`towards`
110 | :func:`xcor`
111 | :func:`ycor`
112 | :func:`heading`
113 | :func:`distance`
114
115 Setting and measurement
116 | :func:`degrees`
117 | :func:`radians`
118
119Pen control
120 Drawing state
121 | :func:`pendown` | :func:`pd` | :func:`down`
122 | :func:`penup` | :func:`pu` | :func:`up`
123 | :func:`pensize` | :func:`width`
124 | :func:`pen`
125 | :func:`isdown`
126
127 Color control
128 | :func:`color`
129 | :func:`pencolor`
130 | :func:`fillcolor`
131
132 Filling
133 | :func:`filling`
134 | :func:`begin_fill`
135 | :func:`end_fill`
136
137 More drawing control
138 | :func:`reset`
139 | :func:`clear`
140 | :func:`write`
141
142Turtle state
143 Visibility
144 | :func:`showturtle` | :func:`st`
145 | :func:`hideturtle` | :func:`ht`
146 | :func:`isvisible`
147
148 Appearance
149 | :func:`shape`
150 | :func:`resizemode`
151 | :func:`shapesize` | :func:`turtlesize`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000152 | :func:`shearfactor`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000153 | :func:`settiltangle`
154 | :func:`tiltangle`
155 | :func:`tilt`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000156 | :func:`shapetransform`
157 | :func:`get_shapepoly`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000158
159Using events
160 | :func:`onclick`
161 | :func:`onrelease`
162 | :func:`ondrag`
163
164Special Turtle methods
165 | :func:`begin_poly`
166 | :func:`end_poly`
167 | :func:`get_poly`
168 | :func:`clone`
169 | :func:`getturtle` | :func:`getpen`
170 | :func:`getscreen`
171 | :func:`setundobuffer`
172 | :func:`undobufferentries`
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000175Methods of TurtleScreen/Screen
176------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000177
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000178Window control
179 | :func:`bgcolor`
180 | :func:`bgpic`
181 | :func:`clear` | :func:`clearscreen`
182 | :func:`reset` | :func:`resetscreen`
183 | :func:`screensize`
184 | :func:`setworldcoordinates`
Georg Brandl116aa622007-08-15 14:28:22 +0000185
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000186Animation control
187 | :func:`delay`
188 | :func:`tracer`
189 | :func:`update`
190
191Using screen events
192 | :func:`listen`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000193 | :func:`onkey` | :func:`onkeyrelease`
194 | :func:`onkeypress`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000195 | :func:`onclick` | :func:`onscreenclick`
196 | :func:`ontimer`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000197 | :func:`mainloop`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000198
199Settings and special methods
200 | :func:`mode`
201 | :func:`colormode`
202 | :func:`getcanvas`
203 | :func:`getshapes`
204 | :func:`register_shape` | :func:`addshape`
205 | :func:`turtles`
206 | :func:`window_height`
207 | :func:`window_width`
208
Georg Brandleaa84ef2009-05-05 08:14:33 +0000209Input methods
210 | :func:`textinput`
211 | :func:`numinput`
212
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000213Methods specific to Screen
214 | :func:`bye`
215 | :func:`exitonclick`
216 | :func:`setup`
217 | :func:`title`
Georg Brandl116aa622007-08-15 14:28:22 +0000218
219
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000220Methods of RawTurtle/Turtle and corresponding functions
221=======================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000222
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000223Most of the examples in this section refer to a Turtle instance called
224``turtle``.
Georg Brandl116aa622007-08-15 14:28:22 +0000225
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000226Turtle motion
227-------------
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229.. function:: forward(distance)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000230 fd(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000231
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000232 :param distance: a number (integer or float)
233
234 Move the turtle forward by the specified *distance*, in the direction the
235 turtle is headed.
236
R. David Murrayf877feb2009-05-05 02:08:52 +0000237 .. doctest::
238
239 >>> turtle.position()
240 (0.00,0.00)
241 >>> turtle.forward(25)
242 >>> turtle.position()
243 (25.00,0.00)
244 >>> turtle.forward(-75)
245 >>> turtle.position()
246 (-50.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000247
248
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000249.. function:: back(distance)
250 bk(distance)
251 backward(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000252
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000253 :param distance: a number
Georg Brandl116aa622007-08-15 14:28:22 +0000254
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000255 Move the turtle backward by *distance*, opposite to the direction the
256 turtle is headed. Do not change the turtle's heading.
Georg Brandl116aa622007-08-15 14:28:22 +0000257
R. David Murrayf877feb2009-05-05 02:08:52 +0000258 .. doctest::
259 :hide:
260
261 >>> turtle.goto(0, 0)
262
263 .. doctest::
264
265 >>> turtle.position()
266 (0.00,0.00)
267 >>> turtle.backward(30)
268 >>> turtle.position()
269 (-30.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271
272.. function:: right(angle)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000273 rt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000275 :param angle: a number (integer or float)
276
277 Turn turtle right by *angle* units. (Units are by default degrees, but
278 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
279 orientation depends on the turtle mode, see :func:`mode`.
280
R. David Murrayf877feb2009-05-05 02:08:52 +0000281 .. doctest::
282 :hide:
283
284 >>> turtle.setheading(22)
285
286 .. doctest::
287
288 >>> turtle.heading()
289 22.0
290 >>> turtle.right(45)
291 >>> turtle.heading()
292 337.0
Georg Brandl116aa622007-08-15 14:28:22 +0000293
294
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000295.. function:: left(angle)
296 lt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000297
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000298 :param angle: a number (integer or float)
Georg Brandl116aa622007-08-15 14:28:22 +0000299
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000300 Turn turtle left by *angle* units. (Units are by default degrees, but
301 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
302 orientation depends on the turtle mode, see :func:`mode`.
Georg Brandl116aa622007-08-15 14:28:22 +0000303
R. David Murrayf877feb2009-05-05 02:08:52 +0000304 .. doctest::
305 :hide:
306
307 >>> turtle.setheading(22)
308
309 .. doctest::
310
311 >>> turtle.heading()
312 22.0
313 >>> turtle.left(45)
314 >>> turtle.heading()
315 67.0
316
Georg Brandl116aa622007-08-15 14:28:22 +0000317
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000318.. function:: goto(x, y=None)
319 setpos(x, y=None)
320 setposition(x, y=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000321
R. David Murrayf877feb2009-05-05 02:08:52 +0000322 :param x: a number or a pair/vector of numbers
323 :param y: a number or ``None``
Georg Brandl116aa622007-08-15 14:28:22 +0000324
R. David Murrayf877feb2009-05-05 02:08:52 +0000325 If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D`
326 (e.g. as returned by :func:`pos`).
Georg Brandl116aa622007-08-15 14:28:22 +0000327
R. David Murrayf877feb2009-05-05 02:08:52 +0000328 Move turtle to an absolute position. If the pen is down, draw line. Do
329 not change the turtle's orientation.
Georg Brandl116aa622007-08-15 14:28:22 +0000330
R. David Murrayf877feb2009-05-05 02:08:52 +0000331 .. doctest::
332 :hide:
333
334 >>> turtle.goto(0, 0)
335
336 .. doctest::
337
338 >>> tp = turtle.pos()
339 >>> tp
340 (0.00,0.00)
341 >>> turtle.setpos(60,30)
342 >>> turtle.pos()
343 (60.00,30.00)
344 >>> turtle.setpos((20,80))
345 >>> turtle.pos()
346 (20.00,80.00)
347 >>> turtle.setpos(tp)
348 >>> turtle.pos()
349 (0.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000350
Georg Brandl116aa622007-08-15 14:28:22 +0000351
352.. function:: setx(x)
353
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000354 :param x: a number (integer or float)
355
356 Set the turtle's first coordinate to *x*, leave second coordinate
357 unchanged.
358
R. David Murrayf877feb2009-05-05 02:08:52 +0000359 .. doctest::
360 :hide:
361
362 >>> turtle.goto(0, 240)
363
364 .. doctest::
365
366 >>> turtle.position()
367 (0.00,240.00)
368 >>> turtle.setx(10)
369 >>> turtle.position()
370 (10.00,240.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000371
Georg Brandl116aa622007-08-15 14:28:22 +0000372
373.. function:: sety(y)
374
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000375 :param y: a number (integer or float)
376
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000377 Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000378
R. David Murrayf877feb2009-05-05 02:08:52 +0000379 .. doctest::
380 :hide:
381
382 >>> turtle.goto(0, 40)
383
384 .. doctest::
385
386 >>> turtle.position()
387 (0.00,40.00)
388 >>> turtle.sety(-10)
389 >>> turtle.position()
390 (0.00,-10.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000391
Georg Brandl116aa622007-08-15 14:28:22 +0000392
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000393.. function:: setheading(to_angle)
394 seth(to_angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000396 :param to_angle: a number (integer or float)
397
398 Set the orientation of the turtle to *to_angle*. Here are some common
399 directions in degrees:
400
401 =================== ====================
402 standard mode logo mode
403 =================== ====================
404 0 - east 0 - north
405 90 - north 90 - east
406 180 - west 180 - south
407 270 - south 270 - west
408 =================== ====================
409
R. David Murrayf877feb2009-05-05 02:08:52 +0000410 .. doctest::
411
412 >>> turtle.setheading(90)
413 >>> turtle.heading()
414 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000415
416
417.. function:: home()
418
419 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
420 its start-orientation (which depends on the mode, see :func:`mode`).
421
R. David Murrayf877feb2009-05-05 02:08:52 +0000422 .. doctest::
423 :hide:
424
425 >>> turtle.setheading(90)
426 >>> turtle.goto(0, -10)
427
428 .. doctest::
429
430 >>> turtle.heading()
431 90.0
432 >>> turtle.position()
433 (0.00,-10.00)
434 >>> turtle.home()
435 >>> turtle.position()
436 (0.00,0.00)
437 >>> turtle.heading()
438 0.0
439
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000440
441.. function:: circle(radius, extent=None, steps=None)
442
443 :param radius: a number
444 :param extent: a number (or ``None``)
445 :param steps: an integer (or ``None``)
446
447 Draw a circle with given *radius*. The center is *radius* units left of
448 the turtle; *extent* -- an angle -- determines which part of the circle
449 is drawn. If *extent* is not given, draw the entire circle. If *extent*
450 is not a full circle, one endpoint of the arc is the current pen
451 position. Draw the arc in counterclockwise direction if *radius* is
452 positive, otherwise in clockwise direction. Finally the direction of the
453 turtle is changed by the amount of *extent*.
454
455 As the circle is approximated by an inscribed regular polygon, *steps*
456 determines the number of steps to use. If not given, it will be
457 calculated automatically. May be used to draw regular polygons.
458
R. David Murrayf877feb2009-05-05 02:08:52 +0000459 .. doctest::
460
461 >>> turtle.home()
462 >>> turtle.position()
463 (0.00,0.00)
464 >>> turtle.heading()
465 0.0
466 >>> turtle.circle(50)
467 >>> turtle.position()
468 (-0.00,0.00)
469 >>> turtle.heading()
470 0.0
471 >>> turtle.circle(120, 180) # draw a semicircle
472 >>> turtle.position()
473 (0.00,240.00)
474 >>> turtle.heading()
475 180.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000476
477
478.. function:: dot(size=None, *color)
479
480 :param size: an integer >= 1 (if given)
481 :param color: a colorstring or a numeric color tuple
482
483 Draw a circular dot with diameter *size*, using *color*. If *size* is
484 not given, the maximum of pensize+4 and 2*pensize is used.
485
R. David Murrayf877feb2009-05-05 02:08:52 +0000486
487 .. doctest::
488
489 >>> turtle.home()
490 >>> turtle.dot()
491 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
492 >>> turtle.position()
493 (100.00,-0.00)
494 >>> turtle.heading()
495 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000496
497
498.. function:: stamp()
499
500 Stamp a copy of the turtle shape onto the canvas at the current turtle
501 position. Return a stamp_id for that stamp, which can be used to delete
502 it by calling ``clearstamp(stamp_id)``.
503
R. David Murrayf877feb2009-05-05 02:08:52 +0000504 .. doctest::
505
506 >>> turtle.color("blue")
507 >>> turtle.stamp()
508 11
509 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000510
511
512.. function:: clearstamp(stampid)
513
514 :param stampid: an integer, must be return value of previous
515 :func:`stamp` call
516
517 Delete stamp with given *stampid*.
518
R. David Murrayf877feb2009-05-05 02:08:52 +0000519 .. doctest::
520
521 >>> turtle.position()
522 (150.00,-0.00)
523 >>> turtle.color("blue")
524 >>> astamp = turtle.stamp()
525 >>> turtle.fd(50)
526 >>> turtle.position()
527 (200.00,-0.00)
528 >>> turtle.clearstamp(astamp)
529 >>> turtle.position()
530 (200.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000531
532
533.. function:: clearstamps(n=None)
534
535 :param n: an integer (or ``None``)
536
537 Delete all or first/last *n* of turtle's stamps. If *n* is None, delete
538 all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete
539 last *n* stamps.
540
R. David Murrayf877feb2009-05-05 02:08:52 +0000541 .. doctest::
542
543 >>> for i in range(8):
544 ... turtle.stamp(); turtle.fd(30)
545 13
546 14
547 15
548 16
549 17
550 18
551 19
552 20
553 >>> turtle.clearstamps(2)
554 >>> turtle.clearstamps(-2)
555 >>> turtle.clearstamps()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000556
557
558.. function:: undo()
559
560 Undo (repeatedly) the last turtle action(s). Number of available
561 undo actions is determined by the size of the undobuffer.
562
R. David Murrayf877feb2009-05-05 02:08:52 +0000563 .. doctest::
564
565 >>> for i in range(4):
566 ... turtle.fd(50); turtle.lt(80)
567 ...
568 >>> for i in range(8):
569 ... turtle.undo()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000570
571
572.. function:: speed(speed=None)
573
574 :param speed: an integer in the range 0..10 or a speedstring (see below)
575
576 Set the turtle's speed to an integer value in the range 0..10. If no
577 argument is given, return current speed.
578
579 If input is a number greater than 10 or smaller than 0.5, speed is set
580 to 0. Speedstrings are mapped to speedvalues as follows:
581
582 * "fastest": 0
583 * "fast": 10
584 * "normal": 6
585 * "slow": 3
586 * "slowest": 1
587
588 Speeds from 1 to 10 enforce increasingly faster animation of line drawing
589 and turtle turning.
590
591 Attention: *speed* = 0 means that *no* animation takes
592 place. forward/back makes turtle jump and likewise left/right make the
593 turtle turn instantly.
594
R. David Murrayf877feb2009-05-05 02:08:52 +0000595 .. doctest::
596
597 >>> turtle.speed()
598 3
599 >>> turtle.speed('normal')
600 >>> turtle.speed()
601 6
602 >>> turtle.speed(9)
603 >>> turtle.speed()
604 9
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000605
606
607Tell Turtle's state
608-------------------
609
610.. function:: position()
611 pos()
612
613 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
614
R. David Murrayf877feb2009-05-05 02:08:52 +0000615 .. doctest::
616
617 >>> turtle.pos()
618 (440.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000619
620
621.. function:: towards(x, y=None)
622
623 :param x: a number or a pair/vector of numbers or a turtle instance
624 :param y: a number if *x* is a number, else ``None``
625
626 Return the angle between the line from turtle position to position specified
627 by (x,y), the vector or the other turtle. This depends on the turtle's start
628 orientation which depends on the mode - "standard"/"world" or "logo").
629
R. David Murrayf877feb2009-05-05 02:08:52 +0000630 .. doctest::
631
632 >>> turtle.goto(10, 10)
633 >>> turtle.towards(0,0)
634 225.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000635
636
637.. function:: xcor()
638
639 Return the turtle's x coordinate.
640
R. David Murrayf877feb2009-05-05 02:08:52 +0000641 .. doctest::
642
643 >>> turtle.home()
644 >>> turtle.left(50)
645 >>> turtle.forward(100)
646 >>> turtle.pos()
647 (64.28,76.60)
Ezio Melotti713e0422009-09-13 08:13:21 +0000648 >>> print(turtle.xcor())
R. David Murrayf877feb2009-05-05 02:08:52 +0000649 64.2787609687
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000650
651
652.. function:: ycor()
653
654 Return the turtle's y coordinate.
655
R. David Murrayf877feb2009-05-05 02:08:52 +0000656 .. doctest::
657
658 >>> turtle.home()
659 >>> turtle.left(60)
660 >>> turtle.forward(100)
Ezio Melotti713e0422009-09-13 08:13:21 +0000661 >>> print(turtle.pos())
R. David Murrayf877feb2009-05-05 02:08:52 +0000662 (50.00,86.60)
Ezio Melotti713e0422009-09-13 08:13:21 +0000663 >>> print(turtle.ycor())
R. David Murrayf877feb2009-05-05 02:08:52 +0000664 86.6025403784
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000665
666
667.. function:: heading()
668
669 Return the turtle's current heading (value depends on the turtle mode, see
670 :func:`mode`).
671
R. David Murrayf877feb2009-05-05 02:08:52 +0000672 .. doctest::
673
674 >>> turtle.home()
675 >>> turtle.left(67)
676 >>> turtle.heading()
677 67.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000678
679
680.. function:: distance(x, y=None)
681
682 :param x: a number or a pair/vector of numbers or a turtle instance
683 :param y: a number if *x* is a number, else ``None``
684
685 Return the distance from the turtle to (x,y), the given vector, or the given
686 other turtle, in turtle step units.
687
R. David Murrayf877feb2009-05-05 02:08:52 +0000688 .. doctest::
689
690 >>> turtle.home()
691 >>> turtle.distance(30,40)
692 50.0
693 >>> turtle.distance((30,40))
694 50.0
695 >>> joe = Turtle()
696 >>> joe.forward(77)
697 >>> turtle.distance(joe)
698 77.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000699
700
701Settings for measurement
702------------------------
703
704.. function:: degrees(fullcircle=360.0)
705
706 :param fullcircle: a number
707
708 Set angle measurement units, i.e. set number of "degrees" for a full circle.
709 Default value is 360 degrees.
710
R. David Murrayf877feb2009-05-05 02:08:52 +0000711 .. doctest::
712
713 >>> turtle.home()
714 >>> turtle.left(90)
715 >>> turtle.heading()
716 90.0
717 >>> turtle.degrees(400.0) # angle measurement in gon
718 >>> turtle.heading()
719 100.0
720 >>> turtle.degrees(360)
721 >>> turtle.heading()
722 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000723
724
725.. function:: radians()
726
727 Set the angle measurement units to radians. Equivalent to
728 ``degrees(2*math.pi)``.
729
R. David Murrayf877feb2009-05-05 02:08:52 +0000730 .. doctest::
731
732 >>> turtle.home()
733 >>> turtle.left(90)
734 >>> turtle.heading()
735 90.0
736 >>> turtle.radians()
737 >>> turtle.heading()
738 1.5707963267948966
739
740 .. doctest::
741 :hide:
742
743 >>> turtle.degrees(360)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000744
745
746Pen control
747-----------
748
749Drawing state
750~~~~~~~~~~~~~
751
752.. function:: pendown()
753 pd()
754 down()
755
756 Pull the pen down -- drawing when moving.
757
758
759.. function:: penup()
760 pu()
761 up()
762
763 Pull the pen up -- no drawing when moving.
764
765
766.. function:: pensize(width=None)
767 width(width=None)
768
769 :param width: a positive number
770
771 Set the line thickness to *width* or return it. If resizemode is set to
772 "auto" and turtleshape is a polygon, that polygon is drawn with the same line
773 thickness. If no argument is given, the current pensize is returned.
774
R. David Murrayf877feb2009-05-05 02:08:52 +0000775 .. doctest::
776
777 >>> turtle.pensize()
778 1
779 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000780
781
782.. function:: pen(pen=None, **pendict)
783
784 :param pen: a dictionary with some or all of the below listed keys
785 :param pendict: one or more keyword-arguments with the below listed keys as keywords
786
787 Return or set the pen's attributes in a "pen-dictionary" with the following
788 key/value pairs:
789
790 * "shown": True/False
791 * "pendown": True/False
792 * "pencolor": color-string or color-tuple
793 * "fillcolor": color-string or color-tuple
794 * "pensize": positive number
795 * "speed": number in range 0..10
796 * "resizemode": "auto" or "user" or "noresize"
797 * "stretchfactor": (positive number, positive number)
798 * "outline": positive number
799 * "tilt": number
800
R. David Murrayf877feb2009-05-05 02:08:52 +0000801 This dictionary can be used as argument for a subsequent call to :func:`pen`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000802 to restore the former pen-state. Moreover one or more of these attributes
803 can be provided as keyword-arguments. This can be used to set several pen
804 attributes in one statement.
805
R. David Murrayf877feb2009-05-05 02:08:52 +0000806 .. doctest::
807 :options: +NORMALIZE_WHITESPACE
808
809 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
810 >>> sorted(turtle.pen().items())
811 [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
812 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
813 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
814 >>> penstate=turtle.pen()
815 >>> turtle.color("yellow", "")
816 >>> turtle.penup()
817 >>> sorted(turtle.pen().items())
818 [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow'),
819 ('pendown', False), ('pensize', 10), ('resizemode', 'noresize'),
820 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
821 >>> turtle.pen(penstate, fillcolor="green")
822 >>> sorted(turtle.pen().items())
823 [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red'),
824 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
825 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000826
827
828.. function:: isdown()
829
830 Return ``True`` if pen is down, ``False`` if it's up.
831
R. David Murrayf877feb2009-05-05 02:08:52 +0000832 .. doctest::
833
834 >>> turtle.penup()
835 >>> turtle.isdown()
836 False
837 >>> turtle.pendown()
838 >>> turtle.isdown()
839 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000840
841
842Color control
843~~~~~~~~~~~~~
844
845.. function:: pencolor(*args)
846
847 Return or set the pencolor.
848
849 Four input formats are allowed:
850
851 ``pencolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000852 Return the current pencolor as color specification string or
853 as a tuple (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000854 color/pencolor/fillcolor call.
855
856 ``pencolor(colorstring)``
857 Set pencolor to *colorstring*, which is a Tk color specification string,
858 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
859
860 ``pencolor((r, g, b))``
861 Set pencolor to the RGB color represented by the tuple of *r*, *g*, and
862 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
863 colormode is either 1.0 or 255 (see :func:`colormode`).
864
865 ``pencolor(r, g, b)``
866 Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of
867 *r*, *g*, and *b* must be in the range 0..colormode.
868
869 If turtleshape is a polygon, the outline of that polygon is drawn with the
870 newly set pencolor.
871
R. David Murrayf877feb2009-05-05 02:08:52 +0000872 .. doctest::
873
874 >>> colormode()
875 1.0
876 >>> turtle.pencolor()
877 'red'
878 >>> turtle.pencolor("brown")
879 >>> turtle.pencolor()
880 'brown'
881 >>> tup = (0.2, 0.8, 0.55)
882 >>> turtle.pencolor(tup)
883 >>> turtle.pencolor()
Mark Dickinsond1cc39d2009-06-28 21:00:42 +0000884 (0.2, 0.8, 0.5490196078431373)
R. David Murrayf877feb2009-05-05 02:08:52 +0000885 >>> colormode(255)
886 >>> turtle.pencolor()
887 (51, 204, 140)
888 >>> turtle.pencolor('#32c18f')
889 >>> turtle.pencolor()
890 (50, 193, 143)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000891
892
893.. function:: fillcolor(*args)
894
895 Return or set the fillcolor.
896
897 Four input formats are allowed:
898
899 ``fillcolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000900 Return the current fillcolor as color specification string, possibly
901 in tuple format (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000902 color/pencolor/fillcolor call.
903
904 ``fillcolor(colorstring)``
905 Set fillcolor to *colorstring*, which is a Tk color specification string,
906 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
907
908 ``fillcolor((r, g, b))``
909 Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and
910 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
911 colormode is either 1.0 or 255 (see :func:`colormode`).
912
913 ``fillcolor(r, g, b)``
914 Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of
915 *r*, *g*, and *b* must be in the range 0..colormode.
916
917 If turtleshape is a polygon, the interior of that polygon is drawn
918 with the newly set fillcolor.
919
R. David Murrayf877feb2009-05-05 02:08:52 +0000920 .. doctest::
921
922 >>> turtle.fillcolor("violet")
923 >>> turtle.fillcolor()
924 'violet'
925 >>> col = turtle.pencolor()
926 >>> col
927 (50, 193, 143)
928 >>> turtle.fillcolor(col)
929 >>> turtle.fillcolor()
930 (50, 193, 143)
931 >>> turtle.fillcolor('#ffffff')
932 >>> turtle.fillcolor()
933 (255, 255, 255)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000934
935
936.. function:: color(*args)
937
938 Return or set pencolor and fillcolor.
939
940 Several input formats are allowed. They use 0 to 3 arguments as
941 follows:
942
943 ``color()``
944 Return the current pencolor and the current fillcolor as a pair of color
R. David Murrayf877feb2009-05-05 02:08:52 +0000945 specification strings or tuples as returned by :func:`pencolor` and
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000946 :func:`fillcolor`.
947
948 ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)``
949 Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the
950 given value.
951
952 ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))``
953 Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)``
954 and analogously if the other input format is used.
955
956 If turtleshape is a polygon, outline and interior of that polygon is drawn
957 with the newly set colors.
958
R. David Murrayf877feb2009-05-05 02:08:52 +0000959 .. doctest::
960
961 >>> turtle.color("red", "green")
962 >>> turtle.color()
963 ('red', 'green')
964 >>> color("#285078", "#a0c8f0")
965 >>> color()
966 ((40, 80, 120), (160, 200, 240))
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000967
968
969See also: Screen method :func:`colormode`.
970
971
972Filling
973~~~~~~~
974
R. David Murrayf877feb2009-05-05 02:08:52 +0000975.. doctest::
976 :hide:
977
978 >>> turtle.home()
979
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000980.. function:: filling()
981
982 Return fillstate (``True`` if filling, ``False`` else).
983
R. David Murrayf877feb2009-05-05 02:08:52 +0000984 .. doctest::
985
986 >>> turtle.begin_fill()
987 >>> if turtle.filling():
988 ... turtle.pensize(5)
989 ... else:
990 ... turtle.pensize(3)
991
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000992
993
994.. function:: begin_fill()
995
996 To be called just before drawing a shape to be filled.
997
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000998
999.. function:: end_fill()
1000
1001 Fill the shape drawn after the last call to :func:`begin_fill`.
1002
R. David Murrayf877feb2009-05-05 02:08:52 +00001003 .. doctest::
1004
1005 >>> turtle.color("black", "red")
1006 >>> turtle.begin_fill()
1007 >>> turtle.circle(80)
1008 >>> turtle.end_fill()
1009
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001010
1011More drawing control
1012~~~~~~~~~~~~~~~~~~~~
1013
1014.. function:: reset()
1015
1016 Delete the turtle's drawings from the screen, re-center the turtle and set
1017 variables to the default values.
1018
R. David Murrayf877feb2009-05-05 02:08:52 +00001019 .. doctest::
1020
1021 >>> turtle.goto(0,-22)
1022 >>> turtle.left(100)
1023 >>> turtle.position()
1024 (0.00,-22.00)
1025 >>> turtle.heading()
1026 100.0
1027 >>> turtle.reset()
1028 >>> turtle.position()
1029 (0.00,0.00)
1030 >>> turtle.heading()
1031 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001032
1033
1034.. function:: clear()
1035
1036 Delete the turtle's drawings from the screen. Do not move turtle. State and
1037 position of the turtle as well as drawings of other turtles are not affected.
1038
1039
1040.. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal"))
1041
1042 :param arg: object to be written to the TurtleScreen
1043 :param move: True/False
1044 :param align: one of the strings "left", "center" or right"
1045 :param font: a triple (fontname, fontsize, fonttype)
1046
1047 Write text - the string representation of *arg* - at the current turtle
1048 position according to *align* ("left", "center" or right") and with the given
1049 font. If *move* is True, the pen is moved to the bottom-right corner of the
1050 text. By default, *move* is False.
1051
1052 >>> turtle.write("Home = ", True, align="center")
1053 >>> turtle.write((0,0), True)
1054
1055
1056Turtle state
1057------------
1058
1059Visibility
1060~~~~~~~~~~
1061
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001062.. function:: hideturtle()
1063 ht()
1064
1065 Make the turtle invisible. It's a good idea to do this while you're in the
1066 middle of doing some complex drawing, because hiding the turtle speeds up the
1067 drawing observably.
1068
R. David Murrayf877feb2009-05-05 02:08:52 +00001069 .. doctest::
1070
1071 >>> turtle.hideturtle()
1072
1073
1074.. function:: showturtle()
1075 st()
1076
1077 Make the turtle visible.
1078
1079 .. doctest::
1080
1081 >>> turtle.showturtle()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001082
1083
1084.. function:: isvisible()
1085
1086 Return True if the Turtle is shown, False if it's hidden.
1087
1088 >>> turtle.hideturtle()
R. David Murrayf877feb2009-05-05 02:08:52 +00001089 >>> turtle.isvisible()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001090 False
R. David Murrayf877feb2009-05-05 02:08:52 +00001091 >>> turtle.showturtle()
1092 >>> turtle.isvisible()
1093 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001094
1095
1096Appearance
1097~~~~~~~~~~
1098
1099.. function:: shape(name=None)
1100
1101 :param name: a string which is a valid shapename
1102
1103 Set turtle shape to shape with given *name* or, if name is not given, return
1104 name of current shape. Shape with *name* must exist in the TurtleScreen's
1105 shape dictionary. Initially there are the following polygon shapes: "arrow",
1106 "turtle", "circle", "square", "triangle", "classic". To learn about how to
1107 deal with shapes see Screen method :func:`register_shape`.
1108
R. David Murrayf877feb2009-05-05 02:08:52 +00001109 .. doctest::
1110
1111 >>> turtle.shape()
1112 'classic'
1113 >>> turtle.shape("turtle")
1114 >>> turtle.shape()
1115 'turtle'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001116
1117
1118.. function:: resizemode(rmode=None)
1119
1120 :param rmode: one of the strings "auto", "user", "noresize"
1121
1122 Set resizemode to one of the values: "auto", "user", "noresize". If *rmode*
1123 is not given, return current resizemode. Different resizemodes have the
1124 following effects:
1125
1126 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
1127 - "user": adapts the appearance of the turtle according to the values of
1128 stretchfactor and outlinewidth (outline), which are set by
1129 :func:`shapesize`.
1130 - "noresize": no adaption of the turtle's appearance takes place.
1131
1132 resizemode("user") is called by :func:`shapesize` when used with arguments.
1133
R. David Murrayf877feb2009-05-05 02:08:52 +00001134 .. doctest::
1135
1136 >>> turtle.resizemode()
1137 'noresize'
1138 >>> turtle.resizemode("auto")
1139 >>> turtle.resizemode()
1140 'auto'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001141
1142
1143.. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None)
R. David Murray7fcd3de2009-06-25 14:26:19 +00001144 turtlesize(stretch_wid=None, stretch_len=None, outline=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001145
1146 :param stretch_wid: positive number
1147 :param stretch_len: positive number
1148 :param outline: positive number
1149
1150 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set
1151 resizemode to "user". If and only if resizemode is set to "user", the turtle
1152 will be displayed stretched according to its stretchfactors: *stretch_wid* is
1153 stretchfactor perpendicular to its orientation, *stretch_len* is
1154 stretchfactor in direction of its orientation, *outline* determines the width
1155 of the shapes's outline.
1156
R. David Murrayf877feb2009-05-05 02:08:52 +00001157 .. doctest::
1158
1159 >>> turtle.shapesize()
1160 (1, 1, 1)
1161 >>> turtle.resizemode("user")
1162 >>> turtle.shapesize(5, 5, 12)
1163 >>> turtle.shapesize()
1164 (5, 5, 12)
1165 >>> turtle.shapesize(outline=8)
1166 >>> turtle.shapesize()
1167 (5, 5, 8)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001168
1169
R. David Murray7fcd3de2009-06-25 14:26:19 +00001170.. function:: shearfactor(shear=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001171
1172 :param shear: number (optional)
1173
1174 Set or return the current shearfactor. Shear the turtleshape according to
1175 the given shearfactor shear, which is the tangent of the shear angle.
1176 Do *not* change the turtle's heading (direction of movement).
1177 If shear is not given: return the current shearfactor, i. e. the
1178 tangent of the shear angle, by which lines parallel to the
1179 heading of the turtle are sheared.
1180
1181 .. doctest::
1182
1183 >>> turtle.shape("circle")
1184 >>> turtle.shapesize(5,2)
1185 >>> turtle.shearfactor(0.5)
1186 >>> turtle.shearfactor()
1187 >>> 0.5
1188
1189
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001190.. function:: tilt(angle)
1191
1192 :param angle: a number
1193
1194 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
1195 change the turtle's heading (direction of movement).
1196
R. David Murrayf877feb2009-05-05 02:08:52 +00001197 .. doctest::
1198
1199 >>> turtle.reset()
1200 >>> turtle.shape("circle")
1201 >>> turtle.shapesize(5,2)
1202 >>> turtle.tilt(30)
1203 >>> turtle.fd(50)
1204 >>> turtle.tilt(30)
1205 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001206
1207
1208.. function:: settiltangle(angle)
1209
1210 :param angle: a number
1211
1212 Rotate the turtleshape to point in the direction specified by *angle*,
1213 regardless of its current tilt-angle. *Do not* change the turtle's heading
1214 (direction of movement).
1215
R. David Murrayf877feb2009-05-05 02:08:52 +00001216 .. doctest::
1217
1218 >>> turtle.reset()
1219 >>> turtle.shape("circle")
1220 >>> turtle.shapesize(5,2)
1221 >>> turtle.settiltangle(45)
1222 >>> turtle.fd(50)
1223 >>> turtle.settiltangle(-45)
1224 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001225
Ezio Melottidb01a3e2010-02-14 03:12:04 +00001226 .. deprecated:: 3.1
1227
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001228
Georg Brandleaa84ef2009-05-05 08:14:33 +00001229.. function:: tiltangle(angle=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001230
Georg Brandleaa84ef2009-05-05 08:14:33 +00001231 :param angle: a number (optional)
1232
1233 Set or return the current tilt-angle. If angle is given, rotate the
1234 turtleshape to point in the direction specified by angle,
1235 regardless of its current tilt-angle. Do *not* change the turtle's
1236 heading (direction of movement).
1237 If angle is not given: return the current tilt-angle, i. e. the angle
1238 between the orientation of the turtleshape and the heading of the
1239 turtle (its direction of movement).
1240
R. David Murrayf877feb2009-05-05 02:08:52 +00001241 .. doctest::
1242
1243 >>> turtle.reset()
1244 >>> turtle.shape("circle")
1245 >>> turtle.shapesize(5,2)
1246 >>> turtle.tilt(45)
1247 >>> turtle.tiltangle()
1248 45.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001249
1250
Georg Brandleaa84ef2009-05-05 08:14:33 +00001251.. function:: shapetransform(t11=None, t12=None, t21=None, t22=None)
1252
1253 :param t11: a number (optional)
1254 :param t12: a number (optional)
1255 :param t21: a number (optional)
1256 :param t12: a number (optional)
1257
1258 Set or return the current transformation matrix of the turtle shape.
1259
1260 If none of the matrix elements are given, return the transformation
1261 matrix as a tuple of 4 elements.
1262 Otherwise set the given elements and transform the turtleshape
1263 according to the matrix consisting of first row t11, t12 and
1264 second row t21, 22. The determinant t11 * t22 - t12 * t21 must not be
1265 zero, otherwise an error is raised.
1266 Modify stretchfactor, shearfactor and tiltangle according to the
1267 given matrix.
1268
1269 .. doctest::
1270
1271 >>> turtle.shape("square")
1272 >>> turtle.shapesize(4,2)
1273 >>> turtle.shearfactor(-0.5)
1274 >>> turtle.shapetransform()
1275 >>> (4.0, -1.0, -0.0, 2.0)
1276
1277
R. David Murray7fcd3de2009-06-25 14:26:19 +00001278.. function:: get_shapepoly()
Georg Brandleaa84ef2009-05-05 08:14:33 +00001279
1280 Return the current shape polygon as tuple of coordinate pairs. This
1281 can be used to define a new shape or components of a compound shape.
1282
1283 .. doctest::
1284
1285 >>> turtle.shape("square")
1286 >>> turtle.shapetransform(4, -1, 0, 2)
1287 >>> turtle.get_shapepoly()
1288 ((50, -20), (30, 20), (-50, 20), (-30, -20))
1289
1290
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001291Using events
1292------------
1293
1294.. function:: onclick(fun, btn=1, add=None)
1295
1296 :param fun: a function with two arguments which will be called with the
1297 coordinates of the clicked point on the canvas
1298 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1299 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1300 added, otherwise it will replace a former binding
1301
1302 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1303 existing bindings are removed. Example for the anonymous turtle, i.e. the
1304 procedural way:
1305
R. David Murrayf877feb2009-05-05 02:08:52 +00001306 .. doctest::
1307
1308 >>> def turn(x, y):
1309 ... left(180)
1310 ...
1311 >>> onclick(turn) # Now clicking into the turtle will turn it.
1312 >>> onclick(None) # event-binding will be removed
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001313
1314
1315.. function:: onrelease(fun, btn=1, add=None)
1316
1317 :param fun: a function with two arguments which will be called with the
1318 coordinates of the clicked point on the canvas
1319 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1320 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1321 added, otherwise it will replace a former binding
1322
1323 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1324 ``None``, existing bindings are removed.
1325
R. David Murrayf877feb2009-05-05 02:08:52 +00001326 .. doctest::
1327
1328 >>> class MyTurtle(Turtle):
1329 ... def glow(self,x,y):
1330 ... self.fillcolor("red")
1331 ... def unglow(self,x,y):
1332 ... self.fillcolor("")
1333 ...
1334 >>> turtle = MyTurtle()
1335 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1336 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001337
1338
1339.. function:: ondrag(fun, btn=1, add=None)
1340
1341 :param fun: a function with two arguments which will be called with the
1342 coordinates of the clicked point on the canvas
1343 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1344 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1345 added, otherwise it will replace a former binding
1346
1347 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1348 existing bindings are removed.
1349
1350 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1351 mouse-click event on that turtle.
1352
R. David Murrayf877feb2009-05-05 02:08:52 +00001353 .. doctest::
1354
1355 >>> turtle.ondrag(turtle.goto)
1356
1357 Subsequently, clicking and dragging the Turtle will move it across
1358 the screen thereby producing handdrawings (if pen is down).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001359
1360
1361Special Turtle methods
1362----------------------
1363
1364.. function:: begin_poly()
1365
1366 Start recording the vertices of a polygon. Current turtle position is first
1367 vertex of polygon.
1368
1369
1370.. function:: end_poly()
1371
1372 Stop recording the vertices of a polygon. Current turtle position is last
1373 vertex of polygon. This will be connected with the first vertex.
1374
1375
1376.. function:: get_poly()
1377
1378 Return the last recorded polygon.
1379
R. David Murrayf877feb2009-05-05 02:08:52 +00001380 .. doctest::
1381
1382 >>> turtle.home()
1383 >>> turtle.begin_poly()
1384 >>> turtle.fd(100)
1385 >>> turtle.left(20)
1386 >>> turtle.fd(30)
1387 >>> turtle.left(60)
1388 >>> turtle.fd(50)
1389 >>> turtle.end_poly()
1390 >>> p = turtle.get_poly()
1391 >>> register_shape("myFavouriteShape", p)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001392
1393
1394.. function:: clone()
1395
1396 Create and return a clone of the turtle with same position, heading and
1397 turtle properties.
1398
R. David Murrayf877feb2009-05-05 02:08:52 +00001399 .. doctest::
1400
1401 >>> mick = Turtle()
1402 >>> joe = mick.clone()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001403
1404
1405.. function:: getturtle()
R. David Murray7fcd3de2009-06-25 14:26:19 +00001406 getpen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001407
1408 Return the Turtle object itself. Only reasonable use: as a function to
1409 return the "anonymous turtle":
1410
R. David Murrayf877feb2009-05-05 02:08:52 +00001411 .. doctest::
1412
1413 >>> pet = getturtle()
1414 >>> pet.fd(50)
1415 >>> pet
1416 <turtle.Turtle object at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001417
1418
1419.. function:: getscreen()
1420
1421 Return the :class:`TurtleScreen` object the turtle is drawing on.
1422 TurtleScreen methods can then be called for that object.
1423
R. David Murrayf877feb2009-05-05 02:08:52 +00001424 .. doctest::
1425
1426 >>> ts = turtle.getscreen()
1427 >>> ts
1428 <turtle._Screen object at 0x...>
1429 >>> ts.bgcolor("pink")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001430
1431
1432.. function:: setundobuffer(size)
1433
1434 :param size: an integer or ``None``
1435
1436 Set or disable undobuffer. If *size* is an integer an empty undobuffer of
1437 given size is installed. *size* gives the maximum number of turtle actions
1438 that can be undone by the :func:`undo` method/function. If *size* is
1439 ``None``, the undobuffer is disabled.
1440
R. David Murrayf877feb2009-05-05 02:08:52 +00001441 .. doctest::
1442
1443 >>> turtle.setundobuffer(42)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001444
1445
1446.. function:: undobufferentries()
1447
1448 Return number of entries in the undobuffer.
1449
R. David Murrayf877feb2009-05-05 02:08:52 +00001450 .. doctest::
1451
1452 >>> while undobufferentries():
1453 ... undo()
1454
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001455
1456
1457.. _compoundshapes:
1458
1459Excursus about the use of compound shapes
1460-----------------------------------------
1461
1462To use compound turtle shapes, which consist of several polygons of different
1463color, you must use the helper class :class:`Shape` explicitly as described
1464below:
1465
14661. Create an empty Shape object of type "compound".
14672. Add as many components to this object as desired, using the
1468 :meth:`addcomponent` method.
1469
1470 For example:
1471
R. David Murrayf877feb2009-05-05 02:08:52 +00001472 .. doctest::
1473
1474 >>> s = Shape("compound")
1475 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1476 >>> s.addcomponent(poly1, "red", "blue")
1477 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1478 >>> s.addcomponent(poly2, "blue", "red")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001479
14803. Now add the Shape to the Screen's shapelist and use it:
1481
R. David Murrayf877feb2009-05-05 02:08:52 +00001482 .. doctest::
1483
1484 >>> register_shape("myshape", s)
1485 >>> shape("myshape")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001486
1487
1488.. note::
1489
1490 The :class:`Shape` class is used internally by the :func:`register_shape`
1491 method in different ways. The application programmer has to deal with the
1492 Shape class *only* when using compound shapes like shown above!
1493
1494
1495Methods of TurtleScreen/Screen and corresponding functions
1496==========================================================
1497
1498Most of the examples in this section refer to a TurtleScreen instance called
1499``screen``.
1500
R. David Murrayf877feb2009-05-05 02:08:52 +00001501.. doctest::
1502 :hide:
1503
1504 >>> screen = Screen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001505
1506Window control
1507--------------
1508
1509.. function:: bgcolor(*args)
1510
1511 :param args: a color string or three numbers in the range 0..colormode or a
1512 3-tuple of such numbers
1513
1514 Set or return background color of the TurtleScreen.
1515
R. David Murrayf877feb2009-05-05 02:08:52 +00001516 .. doctest::
1517
1518 >>> screen.bgcolor("orange")
1519 >>> screen.bgcolor()
1520 'orange'
1521 >>> screen.bgcolor("#800080")
1522 >>> screen.bgcolor()
1523 (128, 0, 128)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001524
1525
1526.. function:: bgpic(picname=None)
1527
1528 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
1529
1530 Set background image or return name of current backgroundimage. If *picname*
1531 is a filename, set the corresponding image as background. If *picname* is
1532 ``"nopic"``, delete background image, if present. If *picname* is ``None``,
R. David Murrayf877feb2009-05-05 02:08:52 +00001533 return the filename of the current backgroundimage. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001534
R. David Murrayf877feb2009-05-05 02:08:52 +00001535 >>> screen.bgpic()
1536 'nopic'
1537 >>> screen.bgpic("landscape.gif")
1538 >>> screen.bgpic()
1539 "landscape.gif"
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001540
1541
1542.. function:: clear()
1543 clearscreen()
1544
1545 Delete all drawings and all turtles from the TurtleScreen. Reset the now
1546 empty TurtleScreen to its initial state: white background, no background
1547 image, no event bindings and tracing on.
1548
1549 .. note::
1550 This TurtleScreen method is available as a global function only under the
1551 name ``clearscreen``. The global function ``clear`` is another one
1552 derived from the Turtle method ``clear``.
1553
1554
1555.. function:: reset()
1556 resetscreen()
1557
1558 Reset all Turtles on the Screen to their initial state.
1559
1560 .. note::
1561 This TurtleScreen method is available as a global function only under the
1562 name ``resetscreen``. The global function ``reset`` is another one
1563 derived from the Turtle method ``reset``.
1564
1565
1566.. function:: screensize(canvwidth=None, canvheight=None, bg=None)
1567
Georg Brandlff2ad0e2009-04-27 16:51:45 +00001568 :param canvwidth: positive integer, new width of canvas in pixels
1569 :param canvheight: positive integer, new height of canvas in pixels
1570 :param bg: colorstring or color-tuple, new background color
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001571
1572 If no arguments are given, return current (canvaswidth, canvasheight). Else
1573 resize the canvas the turtles are drawing on. Do not alter the drawing
1574 window. To observe hidden parts of the canvas, use the scrollbars. With this
1575 method, one can make visible those parts of a drawing which were outside the
1576 canvas before.
1577
R. David Murrayf877feb2009-05-05 02:08:52 +00001578 >>> screen.screensize()
1579 (400, 300)
1580 >>> screen.screensize(2000,1500)
1581 >>> screen.screensize()
1582 (2000, 1500)
1583
1584 e.g. to search for an erroneously escaped turtle ;-)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001585
1586
1587.. function:: setworldcoordinates(llx, lly, urx, ury)
1588
1589 :param llx: a number, x-coordinate of lower left corner of canvas
1590 :param lly: a number, y-coordinate of lower left corner of canvas
1591 :param urx: a number, x-coordinate of upper right corner of canvas
1592 :param ury: a number, y-coordinate of upper right corner of canvas
1593
1594 Set up user-defined coordinate system and switch to mode "world" if
1595 necessary. This performs a ``screen.reset()``. If mode "world" is already
1596 active, all drawings are redrawn according to the new coordinates.
1597
1598 **ATTENTION**: in user-defined coordinate systems angles may appear
1599 distorted.
1600
R. David Murrayf877feb2009-05-05 02:08:52 +00001601 .. doctest::
1602
1603 >>> screen.reset()
1604 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1605 >>> for _ in range(72):
1606 ... left(10)
1607 ...
1608 >>> for _ in range(8):
1609 ... left(45); fd(2) # a regular octagon
1610
1611 .. doctest::
1612 :hide:
1613
1614 >>> screen.reset()
1615 >>> for t in turtles():
1616 ... t.reset()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001617
1618
1619Animation control
1620-----------------
1621
1622.. function:: delay(delay=None)
1623
1624 :param delay: positive integer
1625
1626 Set or return the drawing *delay* in milliseconds. (This is approximately
Georg Brandl2ee470f2008-07-16 12:55:28 +00001627 the time interval between two consecutive canvas updates.) The longer the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001628 drawing delay, the slower the animation.
1629
1630 Optional argument:
1631
R. David Murrayf877feb2009-05-05 02:08:52 +00001632 .. doctest::
1633
1634 >>> screen.delay()
1635 10
1636 >>> screen.delay(5)
1637 >>> screen.delay()
1638 5
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001639
1640
1641.. function:: tracer(n=None, delay=None)
1642
1643 :param n: nonnegative integer
1644 :param delay: nonnegative integer
1645
1646 Turn turtle animation on/off and set delay for update drawings. If *n* is
1647 given, only each n-th regular screen update is really performed. (Can be
1648 used to accelerate the drawing of complex graphics.) Second argument sets
1649 delay value (see :func:`delay`).
1650
R. David Murrayf877feb2009-05-05 02:08:52 +00001651 .. doctest::
1652
1653 >>> screen.tracer(8, 25)
1654 >>> dist = 2
1655 >>> for i in range(200):
1656 ... fd(dist)
1657 ... rt(90)
1658 ... dist += 2
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001659
1660
1661.. function:: update()
1662
1663 Perform a TurtleScreen update. To be used when tracer is turned off.
1664
1665See also the RawTurtle/Turtle method :func:`speed`.
1666
1667
1668Using screen events
1669-------------------
1670
1671.. function:: listen(xdummy=None, ydummy=None)
1672
1673 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1674 are provided in order to be able to pass :func:`listen` to the onclick method.
1675
1676
1677.. function:: onkey(fun, key)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001678 onkeyrelease(fun, key)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001679
1680 :param fun: a function with no arguments or ``None``
1681 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1682
1683 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1684 are removed. Remark: in order to be able to register key-events, TurtleScreen
1685 must have the focus. (See method :func:`listen`.)
1686
R. David Murrayf877feb2009-05-05 02:08:52 +00001687 .. doctest::
1688
1689 >>> def f():
1690 ... fd(50)
1691 ... lt(60)
1692 ...
1693 >>> screen.onkey(f, "Up")
1694 >>> screen.listen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001695
1696
R. David Murray7fcd3de2009-06-25 14:26:19 +00001697.. function:: onkeypress(fun, key=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001698
1699 :param fun: a function with no arguments or ``None``
1700 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1701
1702 Bind *fun* to key-press event of key if key is given,
1703 or to any key-press-event if no key is given.
1704 Remark: in order to be able to register key-events, TurtleScreen
1705 must have focus. (See method :func:`listen`.)
1706
1707 .. doctest::
1708
1709 >>> def f():
1710 ... fd(50)
1711 ...
1712 >>> screen.onkey(f, "Up")
1713 >>> screen.listen()
1714
1715
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001716.. function:: onclick(fun, btn=1, add=None)
1717 onscreenclick(fun, btn=1, add=None)
1718
1719 :param fun: a function with two arguments which will be called with the
1720 coordinates of the clicked point on the canvas
1721 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1722 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1723 added, otherwise it will replace a former binding
1724
1725 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1726 existing bindings are removed.
1727
1728 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1729 named turtle:
1730
R. David Murrayf877feb2009-05-05 02:08:52 +00001731 .. doctest::
1732
1733 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1734 >>> # make the turtle move to the clicked point.
1735 >>> screen.onclick(None) # remove event binding again
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001736
1737 .. note::
1738 This TurtleScreen method is available as a global function only under the
1739 name ``onscreenclick``. The global function ``onclick`` is another one
1740 derived from the Turtle method ``onclick``.
1741
1742
1743.. function:: ontimer(fun, t=0)
1744
1745 :param fun: a function with no arguments
1746 :param t: a number >= 0
1747
1748 Install a timer that calls *fun* after *t* milliseconds.
1749
R. David Murrayf877feb2009-05-05 02:08:52 +00001750 .. doctest::
1751
1752 >>> running = True
1753 >>> def f():
1754 ... if running:
1755 ... fd(50)
1756 ... lt(60)
1757 ... screen.ontimer(f, 250)
1758 >>> f() ### makes the turtle march around
1759 >>> running = False
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001760
1761
Georg Brandleaa84ef2009-05-05 08:14:33 +00001762.. function:: mainloop()
1763
1764 Starts event loop - calling Tkinter's mainloop function.
1765 Must be the last statement in a turtle graphics program.
1766 Must *not* be used if a script is run from within IDLE in -n mode
1767 (No subprocess) - for interactive use of turtle graphics. ::
1768
1769 >>> screen.mainloop()
1770
1771
1772Input methods
1773-------------
1774
1775.. function:: textinput(title, prompt)
1776
1777 :param title: string
1778 :param prompt: string
1779
1780 Pop up a dialog window for input of a string. Parameter title is
1781 the title of the dialog window, propmt is a text mostly describing
1782 what information to input.
1783 Return the string input. If the dialog is canceled, return None. ::
1784
1785 >>> screen.textinput("NIM", "Name of first player:")
1786
1787
R. David Murray7fcd3de2009-06-25 14:26:19 +00001788.. function:: numinput(title, prompt, default=None, minval=None, maxval=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001789
1790 :param title: string
1791 :param prompt: string
1792 :param default: number (optional)
1793 :param prompt: number (optional)
1794 :param prompt: number (optional)
1795
1796 Pop up a dialog window for input of a number. title is the title of the
1797 dialog window, prompt is a text mostly describing what numerical information
1798 to input. default: default value, minval: minimum value for imput,
1799 maxval: maximum value for input
1800 The number input must be in the range minval .. maxval if these are
1801 given. If not, a hint is issued and the dialog remains open for
1802 correction.
1803 Return the number input. If the dialog is canceled, return None. ::
1804
1805 >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000)
1806
1807
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001808Settings and special methods
1809----------------------------
1810
1811.. function:: mode(mode=None)
1812
1813 :param mode: one of the strings "standard", "logo" or "world"
1814
1815 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1816 is not given, current mode is returned.
1817
1818 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1819 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1820 "world coordinates". **Attention**: in this mode angles appear distorted if
1821 ``x/y`` unit-ratio doesn't equal 1.
1822
1823 ============ ========================= ===================
1824 Mode Initial turtle heading positive angles
1825 ============ ========================= ===================
1826 "standard" to the right (east) counterclockwise
1827 "logo" upward (north) clockwise
1828 ============ ========================= ===================
1829
R. David Murrayf877feb2009-05-05 02:08:52 +00001830 .. doctest::
1831
1832 >>> mode("logo") # resets turtle heading to north
1833 >>> mode()
1834 'logo'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001835
1836
1837.. function:: colormode(cmode=None)
1838
1839 :param cmode: one of the values 1.0 or 255
1840
1841 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1842 values of color triples have to be in the range 0..\ *cmode*.
1843
R. David Murrayf877feb2009-05-05 02:08:52 +00001844 .. doctest::
1845
1846 >>> screen.colormode(1)
1847 >>> turtle.pencolor(240, 160, 80)
1848 Traceback (most recent call last):
1849 ...
1850 TurtleGraphicsError: bad color sequence: (240, 160, 80)
1851 >>> screen.colormode()
1852 1.0
1853 >>> screen.colormode(255)
1854 >>> screen.colormode()
1855 255
1856 >>> turtle.pencolor(240,160,80)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001857
1858
1859.. function:: getcanvas()
1860
1861 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1862 do with a Tkinter Canvas.
1863
R. David Murrayf877feb2009-05-05 02:08:52 +00001864 .. doctest::
1865
1866 >>> cv = screen.getcanvas()
1867 >>> cv
1868 <turtle.ScrolledCanvas instance at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001869
1870
1871.. function:: getshapes()
1872
1873 Return a list of names of all currently available turtle shapes.
1874
R. David Murrayf877feb2009-05-05 02:08:52 +00001875 .. doctest::
1876
1877 >>> screen.getshapes()
1878 ['arrow', 'blank', 'circle', ..., 'turtle']
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001879
1880
1881.. function:: register_shape(name, shape=None)
1882 addshape(name, shape=None)
1883
1884 There are three different ways to call this function:
1885
1886 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
R. David Murrayf877feb2009-05-05 02:08:52 +00001887 corresponding image shape. ::
1888
1889 >>> screen.register_shape("turtle.gif")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001890
1891 .. note::
1892 Image shapes *do not* rotate when turning the turtle, so they do not
1893 display the heading of the turtle!
1894
1895 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1896 coordinates: Install the corresponding polygon shape.
1897
R. David Murrayf877feb2009-05-05 02:08:52 +00001898 .. doctest::
1899
1900 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
1901
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001902 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
1903 object: Install the corresponding compound shape.
1904
1905 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1906 shapes can be used by issuing the command ``shape(shapename)``.
1907
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001908
1909.. function:: turtles()
1910
1911 Return the list of turtles on the screen.
1912
R. David Murrayf877feb2009-05-05 02:08:52 +00001913 .. doctest::
1914
1915 >>> for turtle in screen.turtles():
1916 ... turtle.color("red")
Georg Brandl116aa622007-08-15 14:28:22 +00001917
Georg Brandl116aa622007-08-15 14:28:22 +00001918
1919.. function:: window_height()
1920
R. David Murrayf877feb2009-05-05 02:08:52 +00001921 Return the height of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001922
R. David Murrayf877feb2009-05-05 02:08:52 +00001923 >>> screen.window_height()
1924 480
Georg Brandl116aa622007-08-15 14:28:22 +00001925
Georg Brandl116aa622007-08-15 14:28:22 +00001926
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001927.. function:: window_width()
1928
R. David Murrayf877feb2009-05-05 02:08:52 +00001929 Return the width of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001930
R. David Murrayf877feb2009-05-05 02:08:52 +00001931 >>> screen.window_width()
1932 640
Georg Brandl116aa622007-08-15 14:28:22 +00001933
1934
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001935.. _screenspecific:
Georg Brandl116aa622007-08-15 14:28:22 +00001936
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001937Methods specific to Screen, not inherited from TurtleScreen
1938-----------------------------------------------------------
1939
1940.. function:: bye()
1941
1942 Shut the turtlegraphics window.
Georg Brandl116aa622007-08-15 14:28:22 +00001943
1944
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001945.. function:: exitonclick()
Georg Brandl116aa622007-08-15 14:28:22 +00001946
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001947 Bind bye() method to mouse clicks on the Screen.
Georg Brandl116aa622007-08-15 14:28:22 +00001948
1949
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001950 If the value "using_IDLE" in the configuration dictionary is ``False``
1951 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
1952 (no subprocess) is used, this value should be set to ``True`` in
1953 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1954 client script.
Georg Brandl116aa622007-08-15 14:28:22 +00001955
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001956
1957.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
1958
1959 Set the size and position of the main window. Default values of arguments
1960 are stored in the configuration dicionary and can be changed via a
1961 :file:`turtle.cfg` file.
1962
1963 :param width: if an integer, a size in pixels, if a float, a fraction of the
1964 screen; default is 50% of screen
1965 :param height: if an integer, the height in pixels, if a float, a fraction of
1966 the screen; default is 75% of screen
1967 :param startx: if positive, starting position in pixels from the left
1968 edge of the screen, if negative from the right edge, if None,
1969 center window horizontally
1970 :param startx: if positive, starting position in pixels from the top
1971 edge of the screen, if negative from the bottom edge, if None,
1972 center window vertically
1973
R. David Murrayf877feb2009-05-05 02:08:52 +00001974 .. doctest::
1975
1976 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1977 >>> # sets window to 200x200 pixels, in upper left of screen
1978 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1979 >>> # sets window to 75% of screen by 50% of screen and centers
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001980
1981
1982.. function:: title(titlestring)
1983
1984 :param titlestring: a string that is shown in the titlebar of the turtle
1985 graphics window
1986
1987 Set title of turtle window to *titlestring*.
1988
R. David Murrayf877feb2009-05-05 02:08:52 +00001989 .. doctest::
1990
1991 >>> screen.title("Welcome to the turtle zoo!")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001992
1993
1994The public classes of the module :mod:`turtle`
1995==============================================
1996
1997
1998.. class:: RawTurtle(canvas)
1999 RawPen(canvas)
2000
Ezio Melotti8209fcc2010-03-14 09:53:34 +00002001 :param canvas: a :class:`tkinter.Canvas`, a :class:`ScrolledCanvas` or a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002002 :class:`TurtleScreen`
2003
R. David Murrayf877feb2009-05-05 02:08:52 +00002004 Create a turtle. The turtle has all methods described above as "methods of
2005 Turtle/RawTurtle".
Georg Brandl116aa622007-08-15 14:28:22 +00002006
2007
2008.. class:: Turtle()
2009
R. David Murrayf877feb2009-05-05 02:08:52 +00002010 Subclass of RawTurtle, has the same interface but draws on a default
2011 :class:`Screen` object created automatically when needed for the first time.
Georg Brandl116aa622007-08-15 14:28:22 +00002012
2013
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002014.. class:: TurtleScreen(cv)
Georg Brandl116aa622007-08-15 14:28:22 +00002015
Ezio Melotti8209fcc2010-03-14 09:53:34 +00002016 :param cv: a :class:`tkinter.Canvas`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002017
2018 Provides screen oriented methods like :func:`setbg` etc. that are described
2019 above.
2020
2021.. class:: Screen()
2022
2023 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
2024
Georg Brandl48310cd2009-01-03 21:18:54 +00002025
Benjamin Peterson8f6713f2009-11-13 02:29:35 +00002026.. class:: ScrolledCanvas(master)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002027
2028 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
2029 a Tkinter-canvas with scrollbars added
2030
2031 Used by class Screen, which thus automatically provides a ScrolledCanvas as
2032 playground for the turtles.
2033
2034.. class:: Shape(type_, data)
2035
2036 :param type\_: one of the strings "polygon", "image", "compound"
2037
2038 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
2039 specification:
Georg Brandl116aa622007-08-15 14:28:22 +00002040
2041
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002042 =========== ===========
2043 *type_* *data*
2044 =========== ===========
2045 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
2046 "image" an image (in this form only used internally!)
Georg Brandlae2dbe22009-03-13 19:04:40 +00002047 "compound" ``None`` (a compound shape has to be constructed using the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002048 :meth:`addcomponent` method)
2049 =========== ===========
Georg Brandl48310cd2009-01-03 21:18:54 +00002050
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002051 .. method:: addcomponent(poly, fill, outline=None)
Georg Brandl116aa622007-08-15 14:28:22 +00002052
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002053 :param poly: a polygon, i.e. a tuple of pairs of numbers
2054 :param fill: a color the *poly* will be filled with
2055 :param outline: a color for the poly's outline (if given)
Georg Brandl48310cd2009-01-03 21:18:54 +00002056
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002057 Example:
Georg Brandl116aa622007-08-15 14:28:22 +00002058
R. David Murrayf877feb2009-05-05 02:08:52 +00002059 .. doctest::
2060
2061 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
2062 >>> s = Shape("compound")
2063 >>> s.addcomponent(poly, "red", "blue")
2064 >>> # ... add more components and then use register_shape()
Georg Brandl116aa622007-08-15 14:28:22 +00002065
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002066 See :ref:`compoundshapes`.
Georg Brandl116aa622007-08-15 14:28:22 +00002067
2068
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002069.. class:: Vec2D(x, y)
Georg Brandl116aa622007-08-15 14:28:22 +00002070
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002071 A two-dimensional vector class, used as a helper class for implementing
2072 turtle graphics. May be useful for turtle graphics programs too. Derived
2073 from tuple, so a vector is a tuple!
2074
2075 Provides (for *a*, *b* vectors, *k* number):
2076
2077 * ``a + b`` vector addition
2078 * ``a - b`` vector subtraction
2079 * ``a * b`` inner product
2080 * ``k * a`` and ``a * k`` multiplication with scalar
2081 * ``abs(a)`` absolute value of a
2082 * ``a.rotate(angle)`` rotation
2083
2084
2085Help and configuration
2086======================
2087
2088How to use help
2089---------------
2090
2091The public methods of the Screen and Turtle classes are documented extensively
2092via docstrings. So these can be used as online-help via the Python help
2093facilities:
2094
2095- When using IDLE, tooltips show the signatures and first lines of the
2096 docstrings of typed in function-/method calls.
2097
2098- Calling :func:`help` on methods or functions displays the docstrings::
2099
2100 >>> help(Screen.bgcolor)
2101 Help on method bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002102
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002103 bgcolor(self, *args) unbound turtle.Screen method
2104 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002105
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002106 Arguments (if given): a color string or three numbers
2107 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002108
2109
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002110 >>> screen.bgcolor("orange")
2111 >>> screen.bgcolor()
2112 "orange"
2113 >>> screen.bgcolor(0.5,0,0.5)
2114 >>> screen.bgcolor()
2115 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002116
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002117 >>> help(Turtle.penup)
2118 Help on method penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002119
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002120 penup(self) unbound turtle.Turtle method
2121 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002122
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002123 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002124
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002125 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002126
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002127 >>> turtle.penup()
2128
2129- The docstrings of the functions which are derived from methods have a modified
2130 form::
2131
2132 >>> help(bgcolor)
2133 Help on function bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002134
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002135 bgcolor(*args)
2136 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002137
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002138 Arguments (if given): a color string or three numbers
2139 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002140
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002141 Example::
Georg Brandl48310cd2009-01-03 21:18:54 +00002142
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002143 >>> bgcolor("orange")
2144 >>> bgcolor()
2145 "orange"
2146 >>> bgcolor(0.5,0,0.5)
2147 >>> bgcolor()
2148 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002149
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002150 >>> help(penup)
2151 Help on function penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002152
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002153 penup()
2154 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002155
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002156 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002157
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002158 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002159
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002160 Example:
2161 >>> penup()
2162
2163These modified docstrings are created automatically together with the function
2164definitions that are derived from the methods at import time.
2165
2166
2167Translation of docstrings into different languages
2168--------------------------------------------------
2169
2170There is a utility to create a dictionary the keys of which are the method names
2171and the values of which are the docstrings of the public methods of the classes
2172Screen and Turtle.
2173
2174.. function:: write_docstringdict(filename="turtle_docstringdict")
2175
2176 :param filename: a string, used as filename
2177
2178 Create and write docstring-dictionary to a Python script with the given
2179 filename. This function has to be called explicitly (it is not used by the
2180 turtle graphics classes). The docstring dictionary will be written to the
2181 Python script :file:`{filename}.py`. It is intended to serve as a template
2182 for translation of the docstrings into different languages.
2183
2184If you (or your students) want to use :mod:`turtle` with online help in your
2185native language, you have to translate the docstrings and save the resulting
2186file as e.g. :file:`turtle_docstringdict_german.py`.
2187
2188If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2189will be read in at import time and will replace the original English docstrings.
2190
2191At the time of this writing there are docstring dictionaries in German and in
2192Italian. (Requests please to glingl@aon.at.)
2193
2194
2195
2196How to configure Screen and Turtles
2197-----------------------------------
2198
2199The built-in default configuration mimics the appearance and behaviour of the
2200old turtle module in order to retain best possible compatibility with it.
2201
2202If you want to use a different configuration which better reflects the features
2203of this module or which better fits to your needs, e.g. for use in a classroom,
2204you can prepare a configuration file ``turtle.cfg`` which will be read at import
2205time and modify the configuration according to its settings.
2206
2207The built in configuration would correspond to the following turtle.cfg::
2208
2209 width = 0.5
2210 height = 0.75
2211 leftright = None
2212 topbottom = None
2213 canvwidth = 400
2214 canvheight = 300
2215 mode = standard
2216 colormode = 1.0
2217 delay = 10
2218 undobuffersize = 1000
2219 shape = classic
2220 pencolor = black
2221 fillcolor = black
2222 resizemode = noresize
2223 visible = True
2224 language = english
2225 exampleturtle = turtle
2226 examplescreen = screen
2227 title = Python Turtle Graphics
2228 using_IDLE = False
2229
2230Short explanation of selected entries:
2231
2232- The first four lines correspond to the arguments of the :meth:`Screen.setup`
2233 method.
2234- Line 5 and 6 correspond to the arguments of the method
2235 :meth:`Screen.screensize`.
2236- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2237 info try ``help(shape)``.
2238- If you want to use no fillcolor (i.e. make the turtle transparent), you have
2239 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
2240 the cfg-file).
2241- If you want to reflect the turtle its state, you have to use ``resizemode =
2242 auto``.
2243- If you set e.g. ``language = italian`` the docstringdict
2244 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
2245 present on the import path, e.g. in the same directory as :mod:`turtle`.
2246- The entries *exampleturtle* and *examplescreen* define the names of these
2247 objects as they occur in the docstrings. The transformation of
2248 method-docstrings to function-docstrings will delete these names from the
2249 docstrings.
2250- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
2251 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
2252 mainloop.
2253
2254There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2255stored and an additional one in the current working directory. The latter will
2256override the settings of the first one.
2257
2258The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can
2259study it as an example and see its effects when running the demos (preferably
2260not from within the demo-viewer).
2261
2262
2263Demo scripts
2264============
2265
2266There is a set of demo scripts in the turtledemo directory located in the
2267:file:`Demo/turtle` directory in the source distribution.
2268
2269It contains:
2270
Georg Brandlae2dbe22009-03-13 19:04:40 +00002271- a set of 15 demo scripts demonstrating different features of the new module
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002272 :mod:`turtle`
2273- a demo viewer :file:`turtleDemo.py` which can be used to view the sourcecode
2274 of the scripts and run them at the same time. 14 of the examples can be
2275 accessed via the Examples menu; all of them can also be run standalone.
2276- The example :file:`turtledemo_two_canvases.py` demonstrates the simultaneous
2277 use of two canvases with the turtle module. Therefore it only can be run
2278 standalone.
2279- There is a :file:`turtle.cfg` file in this directory, which also serves as an
2280 example for how to write and use such files.
2281
2282The demoscripts are:
2283
2284+----------------+------------------------------+-----------------------+
2285| Name | Description | Features |
2286+----------------+------------------------------+-----------------------+
2287| bytedesign | complex classical | :func:`tracer`, delay,|
2288| | turtlegraphics pattern | :func:`update` |
2289+----------------+------------------------------+-----------------------+
2290| chaos | graphs verhust dynamics, | world coordinates |
2291| | proves that you must not | |
2292| | trust computers' computations| |
2293+----------------+------------------------------+-----------------------+
2294| clock | analog clock showing time | turtles as clock's |
2295| | of your computer | hands, ontimer |
2296+----------------+------------------------------+-----------------------+
2297| colormixer | experiment with r, g, b | :func:`ondrag` |
2298+----------------+------------------------------+-----------------------+
2299| fractalcurves | Hilbert & Koch curves | recursion |
2300+----------------+------------------------------+-----------------------+
2301| lindenmayer | ethnomathematics | L-System |
2302| | (indian kolams) | |
2303+----------------+------------------------------+-----------------------+
2304| minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
2305| | | as Hanoi discs |
2306| | | (shape, shapesize) |
2307+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002308| nim | play the classical nim game | turtles as nimsticks, |
2309| | with three heaps of sticks | event driven (mouse, |
2310| | against the computer. | keyboard) |
2311+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002312| paint | super minimalistic | :func:`onclick` |
2313| | drawing program | |
2314+----------------+------------------------------+-----------------------+
2315| peace | elementary | turtle: appearance |
2316| | | and animation |
2317+----------------+------------------------------+-----------------------+
2318| penrose | aperiodic tiling with | :func:`stamp` |
2319| | kites and darts | |
2320+----------------+------------------------------+-----------------------+
2321| planet_and_moon| simulation of | compound shapes, |
2322| | gravitational system | :class:`Vec2D` |
2323+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002324| round_dance | dancing turtles rotating | compound shapes, clone|
2325| | pairwise in opposite | shapesize, tilt, |
2326| | direction | get_polyshape, update |
2327+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002328| tree | a (graphical) breadth | :func:`clone` |
2329| | first tree (using generators)| |
2330+----------------+------------------------------+-----------------------+
2331| wikipedia | a pattern from the wikipedia | :func:`clone`, |
2332| | article on turtle graphics | :func:`undo` |
2333+----------------+------------------------------+-----------------------+
2334| yingyang | another elementary example | :func:`circle` |
2335+----------------+------------------------------+-----------------------+
2336
2337Have fun!
2338
2339
2340Changes since Python 2.6
2341========================
2342
Georg Brandl48310cd2009-01-03 21:18:54 +00002343- The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and
2344 :meth:`Turtle.window_height` have been eliminated.
2345 Methods with these names and functionality are now available only
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002346 as methods of :class:`Screen`. The functions derived from these remain
Georg Brandl48310cd2009-01-03 21:18:54 +00002347 available. (In fact already in Python 2.6 these methods were merely
2348 duplications of the corresponding
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002349 :class:`TurtleScreen`/:class:`Screen`-methods.)
2350
Georg Brandl48310cd2009-01-03 21:18:54 +00002351- The method :meth:`Turtle.fill` has been eliminated.
2352 The behaviour of :meth:`begin_fill` and :meth:`end_fill`
2353 have changed slightly: now every filling-process must be completed with an
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002354 ``end_fill()`` call.
Georg Brandl48310cd2009-01-03 21:18:54 +00002355
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002356- A method :meth:`Turtle.filling` has been added. It returns a boolean
2357 value: ``True`` if a filling process is under way, ``False`` otherwise.
2358 This behaviour corresponds to a ``fill()`` call without arguments in
Georg Brandl23d11d32008-09-21 07:50:52 +00002359 Python 2.6.
Georg Brandl116aa622007-08-15 14:28:22 +00002360
Georg Brandleaa84ef2009-05-05 08:14:33 +00002361Changes since Python 3.0
2362========================
2363
2364- The methods :meth:`Turtle.shearfactor`, :meth:`Turtle.shapetransform` and
2365 :meth:`Turtle.get_shapepoly` have been added. Thus the full range of
2366 regular linear transforms is now available for transforming turtle shapes.
2367 :meth:`Turtle.tiltangle` has been enhanced in functionality: it now can
2368 be used to get or set the tiltangle. :meth:`Turtle.settiltangle` has been
2369 deprecated.
2370
2371- The method :meth:`Screen.onkeypress` has been added as a complement to
2372 :meth:`Screen.onkey` which in fact binds actions to the keyrelease event.
2373 Accordingly the latter has got an alias: :meth:`Screen.onkeyrelease`.
2374
2375- The method :meth:`Screen.mainloop` has been added. So when working only
2376 with Screen and Turtle objects one must not additonally import
2377 :func:`mainloop` anymore.
2378
2379- Two input methods has been added :meth:`Screen.textinput` and
2380 :meth:`Screen.numinput`. These popup input dialogs and return
2381 strings and numbers respectively.
2382
2383- Two example scripts :file:`tdemo_nim.py` and :file:`tdemo_round_dance.py`
2384 have been added to the Demo directory (source distribution only). As usual
2385 they can be viewed and executed within the demo viewer :file:`turtleDemo.py`.
2386
R. David Murrayf877feb2009-05-05 02:08:52 +00002387
2388.. doctest::
2389 :hide:
2390
2391 >>> for turtle in turtles():
2392 ... turtle.reset()
2393 >>> turtle.penup()
2394 >>> turtle.goto(-200,25)
2395 >>> turtle.pendown()
2396 >>> turtle.write("No one expects the Spanish Inquisition!",
2397 ... font=("Arial", 20, "normal"))
2398 >>> turtle.penup()
2399 >>> turtle.goto(-100,-50)
2400 >>> turtle.pendown()
2401 >>> turtle.write("Our two chief Turtles are...",
2402 ... font=("Arial", 16, "normal"))
2403 >>> turtle.penup()
2404 >>> turtle.goto(-450,-75)
2405 >>> turtle.write(str(turtles()))