blob: 7ef157cd086f199f9252ff4f513c15bb7d96c2fc [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 Melotti1a263ad2010-03-14 09:51:37 +000038and procedure-oriented ways. Because it uses :mod:`tkinter` for the underlying
Ezio Melotti0639d5a2009-12-19 23:26:38 +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 Melotti1a263ad2010-03-14 09:51:37 +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`),
Alexander Belopolsky435d3062010-10-19 21:07:52 +000061 which draws on "the" :class:`Screen` instance which is automatically
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000062 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
Alexander Belopolsky435d3062010-10-19 21:07:52 +000074To use multiple turtles on a screen one has to use the object-oriented interface.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000075
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
Alexander Belopolsky435d3062010-10-19 21:07:52 +000082Overview of available Turtle and Screen methods
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000083=================================================
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 Melotti985e24d2009-09-13 07:54:02 +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 Melotti985e24d2009-09-13 07:54:02 +0000661 >>> print(turtle.pos())
R. David Murrayf877feb2009-05-05 02:08:52 +0000662 (50.00,86.60)
Ezio Melotti985e24d2009-09-13 07:54:02 +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 Dickinson5a55b612009-06-28 20:59: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 Melotti4e511282010-02-14 03:11:06 +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
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001459Compound shapes
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001460-----------------------------------------
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
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001551 name ``clearscreen``. The global function ``clear`` is a different one
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001552 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
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001646 Turn turtle animation on/off and set delay for update drawings. If
1647 *n* is given, only each n-th regular screen update is really
1648 performed. (Can be used to accelerate the drawing of complex
1649 graphics.) When called without arguments, returns the currently
1650 stored value of n. Second argument sets delay value (see
1651 :func:`delay`).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001652
R. David Murrayf877feb2009-05-05 02:08:52 +00001653 .. doctest::
1654
1655 >>> screen.tracer(8, 25)
1656 >>> dist = 2
1657 >>> for i in range(200):
1658 ... fd(dist)
1659 ... rt(90)
1660 ... dist += 2
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001661
1662
1663.. function:: update()
1664
1665 Perform a TurtleScreen update. To be used when tracer is turned off.
1666
1667See also the RawTurtle/Turtle method :func:`speed`.
1668
1669
1670Using screen events
1671-------------------
1672
1673.. function:: listen(xdummy=None, ydummy=None)
1674
1675 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1676 are provided in order to be able to pass :func:`listen` to the onclick method.
1677
1678
1679.. function:: onkey(fun, key)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001680 onkeyrelease(fun, key)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001681
1682 :param fun: a function with no arguments or ``None``
1683 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1684
1685 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1686 are removed. Remark: in order to be able to register key-events, TurtleScreen
1687 must have the focus. (See method :func:`listen`.)
1688
R. David Murrayf877feb2009-05-05 02:08:52 +00001689 .. doctest::
1690
1691 >>> def f():
1692 ... fd(50)
1693 ... lt(60)
1694 ...
1695 >>> screen.onkey(f, "Up")
1696 >>> screen.listen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001697
1698
R. David Murray7fcd3de2009-06-25 14:26:19 +00001699.. function:: onkeypress(fun, key=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001700
1701 :param fun: a function with no arguments or ``None``
1702 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1703
1704 Bind *fun* to key-press event of key if key is given,
1705 or to any key-press-event if no key is given.
1706 Remark: in order to be able to register key-events, TurtleScreen
1707 must have focus. (See method :func:`listen`.)
1708
1709 .. doctest::
1710
1711 >>> def f():
1712 ... fd(50)
1713 ...
1714 >>> screen.onkey(f, "Up")
1715 >>> screen.listen()
1716
1717
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001718.. function:: onclick(fun, btn=1, add=None)
1719 onscreenclick(fun, btn=1, add=None)
1720
1721 :param fun: a function with two arguments which will be called with the
1722 coordinates of the clicked point on the canvas
1723 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1724 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1725 added, otherwise it will replace a former binding
1726
1727 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1728 existing bindings are removed.
1729
1730 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1731 named turtle:
1732
R. David Murrayf877feb2009-05-05 02:08:52 +00001733 .. doctest::
1734
1735 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1736 >>> # make the turtle move to the clicked point.
1737 >>> screen.onclick(None) # remove event binding again
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001738
1739 .. note::
1740 This TurtleScreen method is available as a global function only under the
1741 name ``onscreenclick``. The global function ``onclick`` is another one
1742 derived from the Turtle method ``onclick``.
1743
1744
1745.. function:: ontimer(fun, t=0)
1746
1747 :param fun: a function with no arguments
1748 :param t: a number >= 0
1749
1750 Install a timer that calls *fun* after *t* milliseconds.
1751
R. David Murrayf877feb2009-05-05 02:08:52 +00001752 .. doctest::
1753
1754 >>> running = True
1755 >>> def f():
1756 ... if running:
1757 ... fd(50)
1758 ... lt(60)
1759 ... screen.ontimer(f, 250)
1760 >>> f() ### makes the turtle march around
1761 >>> running = False
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001762
1763
Georg Brandleaa84ef2009-05-05 08:14:33 +00001764.. function:: mainloop()
1765
1766 Starts event loop - calling Tkinter's mainloop function.
1767 Must be the last statement in a turtle graphics program.
1768 Must *not* be used if a script is run from within IDLE in -n mode
1769 (No subprocess) - for interactive use of turtle graphics. ::
1770
1771 >>> screen.mainloop()
1772
1773
1774Input methods
1775-------------
1776
1777.. function:: textinput(title, prompt)
1778
1779 :param title: string
1780 :param prompt: string
1781
1782 Pop up a dialog window for input of a string. Parameter title is
1783 the title of the dialog window, propmt is a text mostly describing
1784 what information to input.
1785 Return the string input. If the dialog is canceled, return None. ::
1786
1787 >>> screen.textinput("NIM", "Name of first player:")
1788
1789
R. David Murray7fcd3de2009-06-25 14:26:19 +00001790.. function:: numinput(title, prompt, default=None, minval=None, maxval=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001791
1792 :param title: string
1793 :param prompt: string
1794 :param default: number (optional)
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001795 :param minval: number (optional)
1796 :param maxval: number (optional)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001797
1798 Pop up a dialog window for input of a number. title is the title of the
1799 dialog window, prompt is a text mostly describing what numerical information
1800 to input. default: default value, minval: minimum value for imput,
1801 maxval: maximum value for input
1802 The number input must be in the range minval .. maxval if these are
1803 given. If not, a hint is issued and the dialog remains open for
1804 correction.
1805 Return the number input. If the dialog is canceled, return None. ::
1806
1807 >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000)
1808
1809
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001810Settings and special methods
1811----------------------------
1812
1813.. function:: mode(mode=None)
1814
1815 :param mode: one of the strings "standard", "logo" or "world"
1816
1817 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1818 is not given, current mode is returned.
1819
1820 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1821 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1822 "world coordinates". **Attention**: in this mode angles appear distorted if
1823 ``x/y`` unit-ratio doesn't equal 1.
1824
1825 ============ ========================= ===================
1826 Mode Initial turtle heading positive angles
1827 ============ ========================= ===================
1828 "standard" to the right (east) counterclockwise
1829 "logo" upward (north) clockwise
1830 ============ ========================= ===================
1831
R. David Murrayf877feb2009-05-05 02:08:52 +00001832 .. doctest::
1833
1834 >>> mode("logo") # resets turtle heading to north
1835 >>> mode()
1836 'logo'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001837
1838
1839.. function:: colormode(cmode=None)
1840
1841 :param cmode: one of the values 1.0 or 255
1842
1843 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1844 values of color triples have to be in the range 0..\ *cmode*.
1845
R. David Murrayf877feb2009-05-05 02:08:52 +00001846 .. doctest::
1847
1848 >>> screen.colormode(1)
1849 >>> turtle.pencolor(240, 160, 80)
1850 Traceback (most recent call last):
1851 ...
1852 TurtleGraphicsError: bad color sequence: (240, 160, 80)
1853 >>> screen.colormode()
1854 1.0
1855 >>> screen.colormode(255)
1856 >>> screen.colormode()
1857 255
1858 >>> turtle.pencolor(240,160,80)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001859
1860
1861.. function:: getcanvas()
1862
1863 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1864 do with a Tkinter Canvas.
1865
R. David Murrayf877feb2009-05-05 02:08:52 +00001866 .. doctest::
1867
1868 >>> cv = screen.getcanvas()
1869 >>> cv
1870 <turtle.ScrolledCanvas instance at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001871
1872
1873.. function:: getshapes()
1874
1875 Return a list of names of all currently available turtle shapes.
1876
R. David Murrayf877feb2009-05-05 02:08:52 +00001877 .. doctest::
1878
1879 >>> screen.getshapes()
1880 ['arrow', 'blank', 'circle', ..., 'turtle']
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001881
1882
1883.. function:: register_shape(name, shape=None)
1884 addshape(name, shape=None)
1885
1886 There are three different ways to call this function:
1887
1888 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
R. David Murrayf877feb2009-05-05 02:08:52 +00001889 corresponding image shape. ::
1890
1891 >>> screen.register_shape("turtle.gif")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001892
1893 .. note::
1894 Image shapes *do not* rotate when turning the turtle, so they do not
1895 display the heading of the turtle!
1896
1897 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1898 coordinates: Install the corresponding polygon shape.
1899
R. David Murrayf877feb2009-05-05 02:08:52 +00001900 .. doctest::
1901
1902 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
1903
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001904 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
1905 object: Install the corresponding compound shape.
1906
1907 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1908 shapes can be used by issuing the command ``shape(shapename)``.
1909
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001910
1911.. function:: turtles()
1912
1913 Return the list of turtles on the screen.
1914
R. David Murrayf877feb2009-05-05 02:08:52 +00001915 .. doctest::
1916
1917 >>> for turtle in screen.turtles():
1918 ... turtle.color("red")
Georg Brandl116aa622007-08-15 14:28:22 +00001919
Georg Brandl116aa622007-08-15 14:28:22 +00001920
1921.. function:: window_height()
1922
R. David Murrayf877feb2009-05-05 02:08:52 +00001923 Return the height of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001924
R. David Murrayf877feb2009-05-05 02:08:52 +00001925 >>> screen.window_height()
1926 480
Georg Brandl116aa622007-08-15 14:28:22 +00001927
Georg Brandl116aa622007-08-15 14:28:22 +00001928
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001929.. function:: window_width()
1930
R. David Murrayf877feb2009-05-05 02:08:52 +00001931 Return the width of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001932
R. David Murrayf877feb2009-05-05 02:08:52 +00001933 >>> screen.window_width()
1934 640
Georg Brandl116aa622007-08-15 14:28:22 +00001935
1936
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001937.. _screenspecific:
Georg Brandl116aa622007-08-15 14:28:22 +00001938
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001939Methods specific to Screen, not inherited from TurtleScreen
1940-----------------------------------------------------------
1941
1942.. function:: bye()
1943
1944 Shut the turtlegraphics window.
Georg Brandl116aa622007-08-15 14:28:22 +00001945
1946
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001947.. function:: exitonclick()
Georg Brandl116aa622007-08-15 14:28:22 +00001948
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001949 Bind bye() method to mouse clicks on the Screen.
Georg Brandl116aa622007-08-15 14:28:22 +00001950
1951
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001952 If the value "using_IDLE" in the configuration dictionary is ``False``
1953 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
1954 (no subprocess) is used, this value should be set to ``True`` in
1955 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1956 client script.
Georg Brandl116aa622007-08-15 14:28:22 +00001957
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001958
1959.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
1960
1961 Set the size and position of the main window. Default values of arguments
Georg Brandl6faee4e2010-09-21 14:48:28 +00001962 are stored in the configuration dictionary and can be changed via a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001963 :file:`turtle.cfg` file.
1964
1965 :param width: if an integer, a size in pixels, if a float, a fraction of the
1966 screen; default is 50% of screen
1967 :param height: if an integer, the height in pixels, if a float, a fraction of
1968 the screen; default is 75% of screen
1969 :param startx: if positive, starting position in pixels from the left
1970 edge of the screen, if negative from the right edge, if None,
1971 center window horizontally
1972 :param startx: if positive, starting position in pixels from the top
1973 edge of the screen, if negative from the bottom edge, if None,
1974 center window vertically
1975
R. David Murrayf877feb2009-05-05 02:08:52 +00001976 .. doctest::
1977
1978 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1979 >>> # sets window to 200x200 pixels, in upper left of screen
1980 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1981 >>> # sets window to 75% of screen by 50% of screen and centers
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001982
1983
1984.. function:: title(titlestring)
1985
1986 :param titlestring: a string that is shown in the titlebar of the turtle
1987 graphics window
1988
1989 Set title of turtle window to *titlestring*.
1990
R. David Murrayf877feb2009-05-05 02:08:52 +00001991 .. doctest::
1992
1993 >>> screen.title("Welcome to the turtle zoo!")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001994
1995
1996The public classes of the module :mod:`turtle`
1997==============================================
1998
1999
2000.. class:: RawTurtle(canvas)
2001 RawPen(canvas)
2002
Ezio Melotti1a263ad2010-03-14 09:51:37 +00002003 :param canvas: a :class:`tkinter.Canvas`, a :class:`ScrolledCanvas` or a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002004 :class:`TurtleScreen`
2005
R. David Murrayf877feb2009-05-05 02:08:52 +00002006 Create a turtle. The turtle has all methods described above as "methods of
2007 Turtle/RawTurtle".
Georg Brandl116aa622007-08-15 14:28:22 +00002008
2009
2010.. class:: Turtle()
2011
R. David Murrayf877feb2009-05-05 02:08:52 +00002012 Subclass of RawTurtle, has the same interface but draws on a default
2013 :class:`Screen` object created automatically when needed for the first time.
Georg Brandl116aa622007-08-15 14:28:22 +00002014
2015
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002016.. class:: TurtleScreen(cv)
Georg Brandl116aa622007-08-15 14:28:22 +00002017
Ezio Melotti1a263ad2010-03-14 09:51:37 +00002018 :param cv: a :class:`tkinter.Canvas`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002019
2020 Provides screen oriented methods like :func:`setbg` etc. that are described
2021 above.
2022
2023.. class:: Screen()
2024
2025 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
2026
Georg Brandl48310cd2009-01-03 21:18:54 +00002027
Benjamin Petersona0dfa822009-11-13 02:25:08 +00002028.. class:: ScrolledCanvas(master)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002029
2030 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
2031 a Tkinter-canvas with scrollbars added
2032
2033 Used by class Screen, which thus automatically provides a ScrolledCanvas as
2034 playground for the turtles.
2035
2036.. class:: Shape(type_, data)
2037
2038 :param type\_: one of the strings "polygon", "image", "compound"
2039
2040 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
2041 specification:
Georg Brandl116aa622007-08-15 14:28:22 +00002042
2043
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002044 =========== ===========
2045 *type_* *data*
2046 =========== ===========
2047 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
2048 "image" an image (in this form only used internally!)
Georg Brandlae2dbe22009-03-13 19:04:40 +00002049 "compound" ``None`` (a compound shape has to be constructed using the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002050 :meth:`addcomponent` method)
2051 =========== ===========
Georg Brandl48310cd2009-01-03 21:18:54 +00002052
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002053 .. method:: addcomponent(poly, fill, outline=None)
Georg Brandl116aa622007-08-15 14:28:22 +00002054
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002055 :param poly: a polygon, i.e. a tuple of pairs of numbers
2056 :param fill: a color the *poly* will be filled with
2057 :param outline: a color for the poly's outline (if given)
Georg Brandl48310cd2009-01-03 21:18:54 +00002058
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002059 Example:
Georg Brandl116aa622007-08-15 14:28:22 +00002060
R. David Murrayf877feb2009-05-05 02:08:52 +00002061 .. doctest::
2062
2063 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
2064 >>> s = Shape("compound")
2065 >>> s.addcomponent(poly, "red", "blue")
2066 >>> # ... add more components and then use register_shape()
Georg Brandl116aa622007-08-15 14:28:22 +00002067
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002068 See :ref:`compoundshapes`.
Georg Brandl116aa622007-08-15 14:28:22 +00002069
2070
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002071.. class:: Vec2D(x, y)
Georg Brandl116aa622007-08-15 14:28:22 +00002072
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002073 A two-dimensional vector class, used as a helper class for implementing
2074 turtle graphics. May be useful for turtle graphics programs too. Derived
2075 from tuple, so a vector is a tuple!
2076
2077 Provides (for *a*, *b* vectors, *k* number):
2078
2079 * ``a + b`` vector addition
2080 * ``a - b`` vector subtraction
2081 * ``a * b`` inner product
2082 * ``k * a`` and ``a * k`` multiplication with scalar
2083 * ``abs(a)`` absolute value of a
2084 * ``a.rotate(angle)`` rotation
2085
2086
2087Help and configuration
2088======================
2089
2090How to use help
2091---------------
2092
2093The public methods of the Screen and Turtle classes are documented extensively
2094via docstrings. So these can be used as online-help via the Python help
2095facilities:
2096
2097- When using IDLE, tooltips show the signatures and first lines of the
2098 docstrings of typed in function-/method calls.
2099
2100- Calling :func:`help` on methods or functions displays the docstrings::
2101
2102 >>> help(Screen.bgcolor)
2103 Help on method bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002104
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002105 bgcolor(self, *args) unbound turtle.Screen method
2106 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002107
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002108 Arguments (if given): a color string or three numbers
2109 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002110
2111
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002112 >>> screen.bgcolor("orange")
2113 >>> screen.bgcolor()
2114 "orange"
2115 >>> screen.bgcolor(0.5,0,0.5)
2116 >>> screen.bgcolor()
2117 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002118
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002119 >>> help(Turtle.penup)
2120 Help on method penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002121
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002122 penup(self) unbound turtle.Turtle method
2123 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002124
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002125 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002126
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002127 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002128
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002129 >>> turtle.penup()
2130
2131- The docstrings of the functions which are derived from methods have a modified
2132 form::
2133
2134 >>> help(bgcolor)
2135 Help on function bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002136
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002137 bgcolor(*args)
2138 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002139
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002140 Arguments (if given): a color string or three numbers
2141 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002142
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002143 Example::
Georg Brandl48310cd2009-01-03 21:18:54 +00002144
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002145 >>> bgcolor("orange")
2146 >>> bgcolor()
2147 "orange"
2148 >>> bgcolor(0.5,0,0.5)
2149 >>> bgcolor()
2150 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002151
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002152 >>> help(penup)
2153 Help on function penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002154
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002155 penup()
2156 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002157
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002158 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002159
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002160 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002161
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002162 Example:
2163 >>> penup()
2164
2165These modified docstrings are created automatically together with the function
2166definitions that are derived from the methods at import time.
2167
2168
2169Translation of docstrings into different languages
2170--------------------------------------------------
2171
2172There is a utility to create a dictionary the keys of which are the method names
2173and the values of which are the docstrings of the public methods of the classes
2174Screen and Turtle.
2175
2176.. function:: write_docstringdict(filename="turtle_docstringdict")
2177
2178 :param filename: a string, used as filename
2179
2180 Create and write docstring-dictionary to a Python script with the given
2181 filename. This function has to be called explicitly (it is not used by the
2182 turtle graphics classes). The docstring dictionary will be written to the
2183 Python script :file:`{filename}.py`. It is intended to serve as a template
2184 for translation of the docstrings into different languages.
2185
2186If you (or your students) want to use :mod:`turtle` with online help in your
2187native language, you have to translate the docstrings and save the resulting
2188file as e.g. :file:`turtle_docstringdict_german.py`.
2189
2190If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2191will be read in at import time and will replace the original English docstrings.
2192
2193At the time of this writing there are docstring dictionaries in German and in
2194Italian. (Requests please to glingl@aon.at.)
2195
2196
2197
2198How to configure Screen and Turtles
2199-----------------------------------
2200
2201The built-in default configuration mimics the appearance and behaviour of the
2202old turtle module in order to retain best possible compatibility with it.
2203
2204If you want to use a different configuration which better reflects the features
2205of this module or which better fits to your needs, e.g. for use in a classroom,
2206you can prepare a configuration file ``turtle.cfg`` which will be read at import
2207time and modify the configuration according to its settings.
2208
2209The built in configuration would correspond to the following turtle.cfg::
2210
2211 width = 0.5
2212 height = 0.75
2213 leftright = None
2214 topbottom = None
2215 canvwidth = 400
2216 canvheight = 300
2217 mode = standard
2218 colormode = 1.0
2219 delay = 10
2220 undobuffersize = 1000
2221 shape = classic
2222 pencolor = black
2223 fillcolor = black
2224 resizemode = noresize
2225 visible = True
2226 language = english
2227 exampleturtle = turtle
2228 examplescreen = screen
2229 title = Python Turtle Graphics
2230 using_IDLE = False
2231
2232Short explanation of selected entries:
2233
2234- The first four lines correspond to the arguments of the :meth:`Screen.setup`
2235 method.
2236- Line 5 and 6 correspond to the arguments of the method
2237 :meth:`Screen.screensize`.
2238- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2239 info try ``help(shape)``.
2240- If you want to use no fillcolor (i.e. make the turtle transparent), you have
2241 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
2242 the cfg-file).
2243- If you want to reflect the turtle its state, you have to use ``resizemode =
2244 auto``.
2245- If you set e.g. ``language = italian`` the docstringdict
2246 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
2247 present on the import path, e.g. in the same directory as :mod:`turtle`.
2248- The entries *exampleturtle* and *examplescreen* define the names of these
2249 objects as they occur in the docstrings. The transformation of
2250 method-docstrings to function-docstrings will delete these names from the
2251 docstrings.
2252- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
2253 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
2254 mainloop.
2255
2256There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2257stored and an additional one in the current working directory. The latter will
2258override the settings of the first one.
2259
2260The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can
2261study it as an example and see its effects when running the demos (preferably
2262not from within the demo-viewer).
2263
2264
2265Demo scripts
2266============
2267
2268There is a set of demo scripts in the turtledemo directory located in the
2269:file:`Demo/turtle` directory in the source distribution.
2270
2271It contains:
2272
Georg Brandlae2dbe22009-03-13 19:04:40 +00002273- a set of 15 demo scripts demonstrating different features of the new module
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002274 :mod:`turtle`
2275- a demo viewer :file:`turtleDemo.py` which can be used to view the sourcecode
2276 of the scripts and run them at the same time. 14 of the examples can be
2277 accessed via the Examples menu; all of them can also be run standalone.
2278- The example :file:`turtledemo_two_canvases.py` demonstrates the simultaneous
2279 use of two canvases with the turtle module. Therefore it only can be run
2280 standalone.
2281- There is a :file:`turtle.cfg` file in this directory, which also serves as an
2282 example for how to write and use such files.
2283
2284The demoscripts are:
2285
2286+----------------+------------------------------+-----------------------+
2287| Name | Description | Features |
2288+----------------+------------------------------+-----------------------+
2289| bytedesign | complex classical | :func:`tracer`, delay,|
2290| | turtlegraphics pattern | :func:`update` |
2291+----------------+------------------------------+-----------------------+
2292| chaos | graphs verhust dynamics, | world coordinates |
2293| | proves that you must not | |
2294| | trust computers' computations| |
2295+----------------+------------------------------+-----------------------+
2296| clock | analog clock showing time | turtles as clock's |
2297| | of your computer | hands, ontimer |
2298+----------------+------------------------------+-----------------------+
2299| colormixer | experiment with r, g, b | :func:`ondrag` |
2300+----------------+------------------------------+-----------------------+
2301| fractalcurves | Hilbert & Koch curves | recursion |
2302+----------------+------------------------------+-----------------------+
2303| lindenmayer | ethnomathematics | L-System |
2304| | (indian kolams) | |
2305+----------------+------------------------------+-----------------------+
2306| minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
2307| | | as Hanoi discs |
2308| | | (shape, shapesize) |
2309+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002310| nim | play the classical nim game | turtles as nimsticks, |
2311| | with three heaps of sticks | event driven (mouse, |
2312| | against the computer. | keyboard) |
2313+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002314| paint | super minimalistic | :func:`onclick` |
2315| | drawing program | |
2316+----------------+------------------------------+-----------------------+
2317| peace | elementary | turtle: appearance |
2318| | | and animation |
2319+----------------+------------------------------+-----------------------+
2320| penrose | aperiodic tiling with | :func:`stamp` |
2321| | kites and darts | |
2322+----------------+------------------------------+-----------------------+
2323| planet_and_moon| simulation of | compound shapes, |
2324| | gravitational system | :class:`Vec2D` |
2325+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002326| round_dance | dancing turtles rotating | compound shapes, clone|
2327| | pairwise in opposite | shapesize, tilt, |
2328| | direction | get_polyshape, update |
2329+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002330| tree | a (graphical) breadth | :func:`clone` |
2331| | first tree (using generators)| |
2332+----------------+------------------------------+-----------------------+
2333| wikipedia | a pattern from the wikipedia | :func:`clone`, |
2334| | article on turtle graphics | :func:`undo` |
2335+----------------+------------------------------+-----------------------+
2336| yingyang | another elementary example | :func:`circle` |
2337+----------------+------------------------------+-----------------------+
2338
2339Have fun!
2340
2341
2342Changes since Python 2.6
2343========================
2344
Georg Brandl48310cd2009-01-03 21:18:54 +00002345- The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and
2346 :meth:`Turtle.window_height` have been eliminated.
2347 Methods with these names and functionality are now available only
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002348 as methods of :class:`Screen`. The functions derived from these remain
Georg Brandl48310cd2009-01-03 21:18:54 +00002349 available. (In fact already in Python 2.6 these methods were merely
2350 duplications of the corresponding
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002351 :class:`TurtleScreen`/:class:`Screen`-methods.)
2352
Georg Brandl48310cd2009-01-03 21:18:54 +00002353- The method :meth:`Turtle.fill` has been eliminated.
2354 The behaviour of :meth:`begin_fill` and :meth:`end_fill`
2355 have changed slightly: now every filling-process must be completed with an
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002356 ``end_fill()`` call.
Georg Brandl48310cd2009-01-03 21:18:54 +00002357
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002358- A method :meth:`Turtle.filling` has been added. It returns a boolean
2359 value: ``True`` if a filling process is under way, ``False`` otherwise.
2360 This behaviour corresponds to a ``fill()`` call without arguments in
Georg Brandl23d11d32008-09-21 07:50:52 +00002361 Python 2.6.
Georg Brandl116aa622007-08-15 14:28:22 +00002362
Georg Brandleaa84ef2009-05-05 08:14:33 +00002363Changes since Python 3.0
2364========================
2365
2366- The methods :meth:`Turtle.shearfactor`, :meth:`Turtle.shapetransform` and
2367 :meth:`Turtle.get_shapepoly` have been added. Thus the full range of
2368 regular linear transforms is now available for transforming turtle shapes.
2369 :meth:`Turtle.tiltangle` has been enhanced in functionality: it now can
2370 be used to get or set the tiltangle. :meth:`Turtle.settiltangle` has been
2371 deprecated.
2372
2373- The method :meth:`Screen.onkeypress` has been added as a complement to
2374 :meth:`Screen.onkey` which in fact binds actions to the keyrelease event.
2375 Accordingly the latter has got an alias: :meth:`Screen.onkeyrelease`.
2376
2377- The method :meth:`Screen.mainloop` has been added. So when working only
2378 with Screen and Turtle objects one must not additonally import
2379 :func:`mainloop` anymore.
2380
2381- Two input methods has been added :meth:`Screen.textinput` and
2382 :meth:`Screen.numinput`. These popup input dialogs and return
2383 strings and numbers respectively.
2384
2385- Two example scripts :file:`tdemo_nim.py` and :file:`tdemo_round_dance.py`
2386 have been added to the Demo directory (source distribution only). As usual
2387 they can be viewed and executed within the demo viewer :file:`turtleDemo.py`.
2388
R. David Murrayf877feb2009-05-05 02:08:52 +00002389
2390.. doctest::
2391 :hide:
2392
2393 >>> for turtle in turtles():
2394 ... turtle.reset()
2395 >>> turtle.penup()
2396 >>> turtle.goto(-200,25)
2397 >>> turtle.pendown()
2398 >>> turtle.write("No one expects the Spanish Inquisition!",
2399 ... font=("Arial", 20, "normal"))
2400 >>> turtle.penup()
2401 >>> turtle.goto(-100,-50)
2402 >>> turtle.pendown()
2403 >>> turtle.write("Our two chief Turtles are...",
2404 ... font=("Arial", 16, "normal"))
2405 >>> turtle.penup()
2406 >>> turtle.goto(-450,-75)
2407 >>> turtle.write(str(turtles()))