blob: 9b5c3c0593db2aab05db213d1ce6d7fe7d271f8a [file] [log] [blame]
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00001=================================
2:mod:`turtle` --- Turtle graphics
3=================================
Georg Brandl116aa622007-08-15 14:28:22 +00004
Georg Brandl23d11d32008-09-21 07:50:52 +00005.. module:: turtle
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00006 :synopsis: An educational framework for simple graphics applications
Georg Brandl2ee470f2008-07-16 12:55:28 +00007.. 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
Alexander Belopolsky3cdfb122010-10-29 17:16:49 +0000717
718 Change angle measurement unit to grad (also known as gon,
719 grade, or gradian and equals 1/100-th of the right angle.)
720 >>> turtle.degrees(400.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000721 >>> turtle.heading()
722 100.0
723 >>> turtle.degrees(360)
724 >>> turtle.heading()
725 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000726
727
728.. function:: radians()
729
730 Set the angle measurement units to radians. Equivalent to
731 ``degrees(2*math.pi)``.
732
R. David Murrayf877feb2009-05-05 02:08:52 +0000733 .. doctest::
734
735 >>> turtle.home()
736 >>> turtle.left(90)
737 >>> turtle.heading()
738 90.0
739 >>> turtle.radians()
740 >>> turtle.heading()
741 1.5707963267948966
742
743 .. doctest::
744 :hide:
745
746 >>> turtle.degrees(360)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000747
748
749Pen control
750-----------
751
752Drawing state
753~~~~~~~~~~~~~
754
755.. function:: pendown()
756 pd()
757 down()
758
759 Pull the pen down -- drawing when moving.
760
761
762.. function:: penup()
763 pu()
764 up()
765
766 Pull the pen up -- no drawing when moving.
767
768
769.. function:: pensize(width=None)
770 width(width=None)
771
772 :param width: a positive number
773
774 Set the line thickness to *width* or return it. If resizemode is set to
775 "auto" and turtleshape is a polygon, that polygon is drawn with the same line
776 thickness. If no argument is given, the current pensize is returned.
777
R. David Murrayf877feb2009-05-05 02:08:52 +0000778 .. doctest::
779
780 >>> turtle.pensize()
781 1
782 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000783
784
785.. function:: pen(pen=None, **pendict)
786
787 :param pen: a dictionary with some or all of the below listed keys
788 :param pendict: one or more keyword-arguments with the below listed keys as keywords
789
790 Return or set the pen's attributes in a "pen-dictionary" with the following
791 key/value pairs:
792
793 * "shown": True/False
794 * "pendown": True/False
795 * "pencolor": color-string or color-tuple
796 * "fillcolor": color-string or color-tuple
797 * "pensize": positive number
798 * "speed": number in range 0..10
799 * "resizemode": "auto" or "user" or "noresize"
800 * "stretchfactor": (positive number, positive number)
801 * "outline": positive number
802 * "tilt": number
803
R. David Murrayf877feb2009-05-05 02:08:52 +0000804 This dictionary can be used as argument for a subsequent call to :func:`pen`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000805 to restore the former pen-state. Moreover one or more of these attributes
806 can be provided as keyword-arguments. This can be used to set several pen
807 attributes in one statement.
808
R. David Murrayf877feb2009-05-05 02:08:52 +0000809 .. doctest::
810 :options: +NORMALIZE_WHITESPACE
811
812 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
813 >>> sorted(turtle.pen().items())
814 [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
815 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
816 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
817 >>> penstate=turtle.pen()
818 >>> turtle.color("yellow", "")
819 >>> turtle.penup()
820 >>> sorted(turtle.pen().items())
821 [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow'),
822 ('pendown', False), ('pensize', 10), ('resizemode', 'noresize'),
823 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
824 >>> turtle.pen(penstate, fillcolor="green")
825 >>> sorted(turtle.pen().items())
826 [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red'),
827 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
828 ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000829
830
831.. function:: isdown()
832
833 Return ``True`` if pen is down, ``False`` if it's up.
834
R. David Murrayf877feb2009-05-05 02:08:52 +0000835 .. doctest::
836
837 >>> turtle.penup()
838 >>> turtle.isdown()
839 False
840 >>> turtle.pendown()
841 >>> turtle.isdown()
842 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000843
844
845Color control
846~~~~~~~~~~~~~
847
848.. function:: pencolor(*args)
849
850 Return or set the pencolor.
851
852 Four input formats are allowed:
853
854 ``pencolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000855 Return the current pencolor as color specification string or
856 as a tuple (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000857 color/pencolor/fillcolor call.
858
859 ``pencolor(colorstring)``
860 Set pencolor to *colorstring*, which is a Tk color specification string,
861 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
862
863 ``pencolor((r, g, b))``
864 Set pencolor to the RGB color represented by the tuple of *r*, *g*, and
865 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
866 colormode is either 1.0 or 255 (see :func:`colormode`).
867
868 ``pencolor(r, g, b)``
869 Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of
870 *r*, *g*, and *b* must be in the range 0..colormode.
871
872 If turtleshape is a polygon, the outline of that polygon is drawn with the
873 newly set pencolor.
874
R. David Murrayf877feb2009-05-05 02:08:52 +0000875 .. doctest::
876
877 >>> colormode()
878 1.0
879 >>> turtle.pencolor()
880 'red'
881 >>> turtle.pencolor("brown")
882 >>> turtle.pencolor()
883 'brown'
884 >>> tup = (0.2, 0.8, 0.55)
885 >>> turtle.pencolor(tup)
886 >>> turtle.pencolor()
Mark Dickinson5a55b612009-06-28 20:59:42 +0000887 (0.2, 0.8, 0.5490196078431373)
R. David Murrayf877feb2009-05-05 02:08:52 +0000888 >>> colormode(255)
889 >>> turtle.pencolor()
890 (51, 204, 140)
891 >>> turtle.pencolor('#32c18f')
892 >>> turtle.pencolor()
893 (50, 193, 143)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000894
895
896.. function:: fillcolor(*args)
897
898 Return or set the fillcolor.
899
900 Four input formats are allowed:
901
902 ``fillcolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000903 Return the current fillcolor as color specification string, possibly
904 in tuple format (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000905 color/pencolor/fillcolor call.
906
907 ``fillcolor(colorstring)``
908 Set fillcolor to *colorstring*, which is a Tk color specification string,
909 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
910
911 ``fillcolor((r, g, b))``
912 Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and
913 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
914 colormode is either 1.0 or 255 (see :func:`colormode`).
915
916 ``fillcolor(r, g, b)``
917 Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of
918 *r*, *g*, and *b* must be in the range 0..colormode.
919
920 If turtleshape is a polygon, the interior of that polygon is drawn
921 with the newly set fillcolor.
922
R. David Murrayf877feb2009-05-05 02:08:52 +0000923 .. doctest::
924
925 >>> turtle.fillcolor("violet")
926 >>> turtle.fillcolor()
927 'violet'
928 >>> col = turtle.pencolor()
929 >>> col
930 (50, 193, 143)
931 >>> turtle.fillcolor(col)
932 >>> turtle.fillcolor()
933 (50, 193, 143)
934 >>> turtle.fillcolor('#ffffff')
935 >>> turtle.fillcolor()
936 (255, 255, 255)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000937
938
939.. function:: color(*args)
940
941 Return or set pencolor and fillcolor.
942
943 Several input formats are allowed. They use 0 to 3 arguments as
944 follows:
945
946 ``color()``
947 Return the current pencolor and the current fillcolor as a pair of color
R. David Murrayf877feb2009-05-05 02:08:52 +0000948 specification strings or tuples as returned by :func:`pencolor` and
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000949 :func:`fillcolor`.
950
951 ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)``
952 Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the
953 given value.
954
955 ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))``
956 Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)``
957 and analogously if the other input format is used.
958
959 If turtleshape is a polygon, outline and interior of that polygon is drawn
960 with the newly set colors.
961
R. David Murrayf877feb2009-05-05 02:08:52 +0000962 .. doctest::
963
964 >>> turtle.color("red", "green")
965 >>> turtle.color()
966 ('red', 'green')
967 >>> color("#285078", "#a0c8f0")
968 >>> color()
969 ((40, 80, 120), (160, 200, 240))
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000970
971
972See also: Screen method :func:`colormode`.
973
974
975Filling
976~~~~~~~
977
R. David Murrayf877feb2009-05-05 02:08:52 +0000978.. doctest::
979 :hide:
980
981 >>> turtle.home()
982
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000983.. function:: filling()
984
985 Return fillstate (``True`` if filling, ``False`` else).
986
R. David Murrayf877feb2009-05-05 02:08:52 +0000987 .. doctest::
988
989 >>> turtle.begin_fill()
990 >>> if turtle.filling():
991 ... turtle.pensize(5)
992 ... else:
993 ... turtle.pensize(3)
994
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000995
996
997.. function:: begin_fill()
998
999 To be called just before drawing a shape to be filled.
1000
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001001
1002.. function:: end_fill()
1003
1004 Fill the shape drawn after the last call to :func:`begin_fill`.
1005
R. David Murrayf877feb2009-05-05 02:08:52 +00001006 .. doctest::
1007
1008 >>> turtle.color("black", "red")
1009 >>> turtle.begin_fill()
1010 >>> turtle.circle(80)
1011 >>> turtle.end_fill()
1012
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001013
1014More drawing control
1015~~~~~~~~~~~~~~~~~~~~
1016
1017.. function:: reset()
1018
1019 Delete the turtle's drawings from the screen, re-center the turtle and set
1020 variables to the default values.
1021
R. David Murrayf877feb2009-05-05 02:08:52 +00001022 .. doctest::
1023
1024 >>> turtle.goto(0,-22)
1025 >>> turtle.left(100)
1026 >>> turtle.position()
1027 (0.00,-22.00)
1028 >>> turtle.heading()
1029 100.0
1030 >>> turtle.reset()
1031 >>> turtle.position()
1032 (0.00,0.00)
1033 >>> turtle.heading()
1034 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001035
1036
1037.. function:: clear()
1038
1039 Delete the turtle's drawings from the screen. Do not move turtle. State and
1040 position of the turtle as well as drawings of other turtles are not affected.
1041
1042
1043.. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal"))
1044
1045 :param arg: object to be written to the TurtleScreen
1046 :param move: True/False
1047 :param align: one of the strings "left", "center" or right"
1048 :param font: a triple (fontname, fontsize, fonttype)
1049
1050 Write text - the string representation of *arg* - at the current turtle
1051 position according to *align* ("left", "center" or right") and with the given
1052 font. If *move* is True, the pen is moved to the bottom-right corner of the
1053 text. By default, *move* is False.
1054
1055 >>> turtle.write("Home = ", True, align="center")
1056 >>> turtle.write((0,0), True)
1057
1058
1059Turtle state
1060------------
1061
1062Visibility
1063~~~~~~~~~~
1064
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001065.. function:: hideturtle()
1066 ht()
1067
1068 Make the turtle invisible. It's a good idea to do this while you're in the
1069 middle of doing some complex drawing, because hiding the turtle speeds up the
1070 drawing observably.
1071
R. David Murrayf877feb2009-05-05 02:08:52 +00001072 .. doctest::
1073
1074 >>> turtle.hideturtle()
1075
1076
1077.. function:: showturtle()
1078 st()
1079
1080 Make the turtle visible.
1081
1082 .. doctest::
1083
1084 >>> turtle.showturtle()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001085
1086
1087.. function:: isvisible()
1088
1089 Return True if the Turtle is shown, False if it's hidden.
1090
1091 >>> turtle.hideturtle()
R. David Murrayf877feb2009-05-05 02:08:52 +00001092 >>> turtle.isvisible()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001093 False
R. David Murrayf877feb2009-05-05 02:08:52 +00001094 >>> turtle.showturtle()
1095 >>> turtle.isvisible()
1096 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001097
1098
1099Appearance
1100~~~~~~~~~~
1101
1102.. function:: shape(name=None)
1103
1104 :param name: a string which is a valid shapename
1105
1106 Set turtle shape to shape with given *name* or, if name is not given, return
1107 name of current shape. Shape with *name* must exist in the TurtleScreen's
1108 shape dictionary. Initially there are the following polygon shapes: "arrow",
1109 "turtle", "circle", "square", "triangle", "classic". To learn about how to
1110 deal with shapes see Screen method :func:`register_shape`.
1111
R. David Murrayf877feb2009-05-05 02:08:52 +00001112 .. doctest::
1113
1114 >>> turtle.shape()
1115 'classic'
1116 >>> turtle.shape("turtle")
1117 >>> turtle.shape()
1118 'turtle'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001119
1120
1121.. function:: resizemode(rmode=None)
1122
1123 :param rmode: one of the strings "auto", "user", "noresize"
1124
1125 Set resizemode to one of the values: "auto", "user", "noresize". If *rmode*
1126 is not given, return current resizemode. Different resizemodes have the
1127 following effects:
1128
1129 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
1130 - "user": adapts the appearance of the turtle according to the values of
1131 stretchfactor and outlinewidth (outline), which are set by
1132 :func:`shapesize`.
1133 - "noresize": no adaption of the turtle's appearance takes place.
1134
1135 resizemode("user") is called by :func:`shapesize` when used with arguments.
1136
R. David Murrayf877feb2009-05-05 02:08:52 +00001137 .. doctest::
1138
1139 >>> turtle.resizemode()
1140 'noresize'
1141 >>> turtle.resizemode("auto")
1142 >>> turtle.resizemode()
1143 'auto'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001144
1145
1146.. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None)
R. David Murray7fcd3de2009-06-25 14:26:19 +00001147 turtlesize(stretch_wid=None, stretch_len=None, outline=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001148
1149 :param stretch_wid: positive number
1150 :param stretch_len: positive number
1151 :param outline: positive number
1152
1153 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set
1154 resizemode to "user". If and only if resizemode is set to "user", the turtle
1155 will be displayed stretched according to its stretchfactors: *stretch_wid* is
1156 stretchfactor perpendicular to its orientation, *stretch_len* is
1157 stretchfactor in direction of its orientation, *outline* determines the width
1158 of the shapes's outline.
1159
R. David Murrayf877feb2009-05-05 02:08:52 +00001160 .. doctest::
1161
1162 >>> turtle.shapesize()
1163 (1, 1, 1)
1164 >>> turtle.resizemode("user")
1165 >>> turtle.shapesize(5, 5, 12)
1166 >>> turtle.shapesize()
1167 (5, 5, 12)
1168 >>> turtle.shapesize(outline=8)
1169 >>> turtle.shapesize()
1170 (5, 5, 8)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001171
1172
R. David Murray7fcd3de2009-06-25 14:26:19 +00001173.. function:: shearfactor(shear=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001174
1175 :param shear: number (optional)
1176
1177 Set or return the current shearfactor. Shear the turtleshape according to
1178 the given shearfactor shear, which is the tangent of the shear angle.
1179 Do *not* change the turtle's heading (direction of movement).
1180 If shear is not given: return the current shearfactor, i. e. the
1181 tangent of the shear angle, by which lines parallel to the
1182 heading of the turtle are sheared.
1183
1184 .. doctest::
1185
1186 >>> turtle.shape("circle")
1187 >>> turtle.shapesize(5,2)
1188 >>> turtle.shearfactor(0.5)
1189 >>> turtle.shearfactor()
1190 >>> 0.5
1191
1192
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001193.. function:: tilt(angle)
1194
1195 :param angle: a number
1196
1197 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
1198 change the turtle's heading (direction of movement).
1199
R. David Murrayf877feb2009-05-05 02:08:52 +00001200 .. doctest::
1201
1202 >>> turtle.reset()
1203 >>> turtle.shape("circle")
1204 >>> turtle.shapesize(5,2)
1205 >>> turtle.tilt(30)
1206 >>> turtle.fd(50)
1207 >>> turtle.tilt(30)
1208 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001209
1210
1211.. function:: settiltangle(angle)
1212
1213 :param angle: a number
1214
1215 Rotate the turtleshape to point in the direction specified by *angle*,
1216 regardless of its current tilt-angle. *Do not* change the turtle's heading
1217 (direction of movement).
1218
R. David Murrayf877feb2009-05-05 02:08:52 +00001219 .. doctest::
1220
1221 >>> turtle.reset()
1222 >>> turtle.shape("circle")
1223 >>> turtle.shapesize(5,2)
1224 >>> turtle.settiltangle(45)
1225 >>> turtle.fd(50)
1226 >>> turtle.settiltangle(-45)
1227 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001228
Ezio Melotti4e511282010-02-14 03:11:06 +00001229 .. deprecated:: 3.1
1230
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001231
Georg Brandleaa84ef2009-05-05 08:14:33 +00001232.. function:: tiltangle(angle=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001233
Georg Brandleaa84ef2009-05-05 08:14:33 +00001234 :param angle: a number (optional)
1235
1236 Set or return the current tilt-angle. If angle is given, rotate the
1237 turtleshape to point in the direction specified by angle,
1238 regardless of its current tilt-angle. Do *not* change the turtle's
1239 heading (direction of movement).
1240 If angle is not given: return the current tilt-angle, i. e. the angle
1241 between the orientation of the turtleshape and the heading of the
1242 turtle (its direction of movement).
1243
R. David Murrayf877feb2009-05-05 02:08:52 +00001244 .. doctest::
1245
1246 >>> turtle.reset()
1247 >>> turtle.shape("circle")
1248 >>> turtle.shapesize(5,2)
1249 >>> turtle.tilt(45)
1250 >>> turtle.tiltangle()
1251 45.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001252
1253
Georg Brandleaa84ef2009-05-05 08:14:33 +00001254.. function:: shapetransform(t11=None, t12=None, t21=None, t22=None)
1255
1256 :param t11: a number (optional)
1257 :param t12: a number (optional)
1258 :param t21: a number (optional)
1259 :param t12: a number (optional)
1260
1261 Set or return the current transformation matrix of the turtle shape.
1262
1263 If none of the matrix elements are given, return the transformation
1264 matrix as a tuple of 4 elements.
1265 Otherwise set the given elements and transform the turtleshape
1266 according to the matrix consisting of first row t11, t12 and
1267 second row t21, 22. The determinant t11 * t22 - t12 * t21 must not be
1268 zero, otherwise an error is raised.
1269 Modify stretchfactor, shearfactor and tiltangle according to the
1270 given matrix.
1271
1272 .. doctest::
1273
1274 >>> turtle.shape("square")
1275 >>> turtle.shapesize(4,2)
1276 >>> turtle.shearfactor(-0.5)
1277 >>> turtle.shapetransform()
1278 >>> (4.0, -1.0, -0.0, 2.0)
1279
1280
R. David Murray7fcd3de2009-06-25 14:26:19 +00001281.. function:: get_shapepoly()
Georg Brandleaa84ef2009-05-05 08:14:33 +00001282
1283 Return the current shape polygon as tuple of coordinate pairs. This
1284 can be used to define a new shape or components of a compound shape.
1285
1286 .. doctest::
1287
1288 >>> turtle.shape("square")
1289 >>> turtle.shapetransform(4, -1, 0, 2)
1290 >>> turtle.get_shapepoly()
1291 ((50, -20), (30, 20), (-50, 20), (-30, -20))
1292
1293
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001294Using events
1295------------
1296
1297.. function:: onclick(fun, btn=1, add=None)
1298
1299 :param fun: a function with two arguments which will be called with the
1300 coordinates of the clicked point on the canvas
1301 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1302 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1303 added, otherwise it will replace a former binding
1304
1305 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1306 existing bindings are removed. Example for the anonymous turtle, i.e. the
1307 procedural way:
1308
R. David Murrayf877feb2009-05-05 02:08:52 +00001309 .. doctest::
1310
1311 >>> def turn(x, y):
1312 ... left(180)
1313 ...
1314 >>> onclick(turn) # Now clicking into the turtle will turn it.
1315 >>> onclick(None) # event-binding will be removed
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001316
1317
1318.. function:: onrelease(fun, btn=1, add=None)
1319
1320 :param fun: a function with two arguments which will be called with the
1321 coordinates of the clicked point on the canvas
1322 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1323 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1324 added, otherwise it will replace a former binding
1325
1326 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1327 ``None``, existing bindings are removed.
1328
R. David Murrayf877feb2009-05-05 02:08:52 +00001329 .. doctest::
1330
1331 >>> class MyTurtle(Turtle):
1332 ... def glow(self,x,y):
1333 ... self.fillcolor("red")
1334 ... def unglow(self,x,y):
1335 ... self.fillcolor("")
1336 ...
1337 >>> turtle = MyTurtle()
1338 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1339 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001340
1341
1342.. function:: ondrag(fun, btn=1, add=None)
1343
1344 :param fun: a function with two arguments which will be called with the
1345 coordinates of the clicked point on the canvas
1346 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1347 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1348 added, otherwise it will replace a former binding
1349
1350 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1351 existing bindings are removed.
1352
1353 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1354 mouse-click event on that turtle.
1355
R. David Murrayf877feb2009-05-05 02:08:52 +00001356 .. doctest::
1357
1358 >>> turtle.ondrag(turtle.goto)
1359
1360 Subsequently, clicking and dragging the Turtle will move it across
1361 the screen thereby producing handdrawings (if pen is down).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001362
1363
1364Special Turtle methods
1365----------------------
1366
1367.. function:: begin_poly()
1368
1369 Start recording the vertices of a polygon. Current turtle position is first
1370 vertex of polygon.
1371
1372
1373.. function:: end_poly()
1374
1375 Stop recording the vertices of a polygon. Current turtle position is last
1376 vertex of polygon. This will be connected with the first vertex.
1377
1378
1379.. function:: get_poly()
1380
1381 Return the last recorded polygon.
1382
R. David Murrayf877feb2009-05-05 02:08:52 +00001383 .. doctest::
1384
1385 >>> turtle.home()
1386 >>> turtle.begin_poly()
1387 >>> turtle.fd(100)
1388 >>> turtle.left(20)
1389 >>> turtle.fd(30)
1390 >>> turtle.left(60)
1391 >>> turtle.fd(50)
1392 >>> turtle.end_poly()
1393 >>> p = turtle.get_poly()
1394 >>> register_shape("myFavouriteShape", p)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001395
1396
1397.. function:: clone()
1398
1399 Create and return a clone of the turtle with same position, heading and
1400 turtle properties.
1401
R. David Murrayf877feb2009-05-05 02:08:52 +00001402 .. doctest::
1403
1404 >>> mick = Turtle()
1405 >>> joe = mick.clone()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001406
1407
1408.. function:: getturtle()
R. David Murray7fcd3de2009-06-25 14:26:19 +00001409 getpen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001410
1411 Return the Turtle object itself. Only reasonable use: as a function to
1412 return the "anonymous turtle":
1413
R. David Murrayf877feb2009-05-05 02:08:52 +00001414 .. doctest::
1415
1416 >>> pet = getturtle()
1417 >>> pet.fd(50)
1418 >>> pet
1419 <turtle.Turtle object at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001420
1421
1422.. function:: getscreen()
1423
1424 Return the :class:`TurtleScreen` object the turtle is drawing on.
1425 TurtleScreen methods can then be called for that object.
1426
R. David Murrayf877feb2009-05-05 02:08:52 +00001427 .. doctest::
1428
1429 >>> ts = turtle.getscreen()
1430 >>> ts
1431 <turtle._Screen object at 0x...>
1432 >>> ts.bgcolor("pink")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001433
1434
1435.. function:: setundobuffer(size)
1436
1437 :param size: an integer or ``None``
1438
1439 Set or disable undobuffer. If *size* is an integer an empty undobuffer of
1440 given size is installed. *size* gives the maximum number of turtle actions
1441 that can be undone by the :func:`undo` method/function. If *size* is
1442 ``None``, the undobuffer is disabled.
1443
R. David Murrayf877feb2009-05-05 02:08:52 +00001444 .. doctest::
1445
1446 >>> turtle.setundobuffer(42)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001447
1448
1449.. function:: undobufferentries()
1450
1451 Return number of entries in the undobuffer.
1452
R. David Murrayf877feb2009-05-05 02:08:52 +00001453 .. doctest::
1454
1455 >>> while undobufferentries():
1456 ... undo()
1457
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001458
1459
1460.. _compoundshapes:
1461
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001462Compound shapes
Alexander Belopolsky41f56f02010-10-21 18:15:39 +00001463---------------
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001464
1465To use compound turtle shapes, which consist of several polygons of different
1466color, you must use the helper class :class:`Shape` explicitly as described
1467below:
1468
14691. Create an empty Shape object of type "compound".
14702. Add as many components to this object as desired, using the
1471 :meth:`addcomponent` method.
1472
1473 For example:
1474
R. David Murrayf877feb2009-05-05 02:08:52 +00001475 .. doctest::
1476
1477 >>> s = Shape("compound")
1478 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1479 >>> s.addcomponent(poly1, "red", "blue")
1480 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1481 >>> s.addcomponent(poly2, "blue", "red")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001482
14833. Now add the Shape to the Screen's shapelist and use it:
1484
R. David Murrayf877feb2009-05-05 02:08:52 +00001485 .. doctest::
1486
1487 >>> register_shape("myshape", s)
1488 >>> shape("myshape")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001489
1490
1491.. note::
1492
1493 The :class:`Shape` class is used internally by the :func:`register_shape`
1494 method in different ways. The application programmer has to deal with the
1495 Shape class *only* when using compound shapes like shown above!
1496
1497
1498Methods of TurtleScreen/Screen and corresponding functions
1499==========================================================
1500
1501Most of the examples in this section refer to a TurtleScreen instance called
1502``screen``.
1503
R. David Murrayf877feb2009-05-05 02:08:52 +00001504.. doctest::
1505 :hide:
1506
1507 >>> screen = Screen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001508
1509Window control
1510--------------
1511
1512.. function:: bgcolor(*args)
1513
1514 :param args: a color string or three numbers in the range 0..colormode or a
1515 3-tuple of such numbers
1516
Alexander Belopolsky3cdfb122010-10-29 17:16:49 +00001517
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001518 Set or return background color of the TurtleScreen.
1519
R. David Murrayf877feb2009-05-05 02:08:52 +00001520 .. doctest::
1521
1522 >>> screen.bgcolor("orange")
1523 >>> screen.bgcolor()
1524 'orange'
1525 >>> screen.bgcolor("#800080")
1526 >>> screen.bgcolor()
1527 (128, 0, 128)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001528
1529
1530.. function:: bgpic(picname=None)
1531
1532 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
1533
1534 Set background image or return name of current backgroundimage. If *picname*
1535 is a filename, set the corresponding image as background. If *picname* is
1536 ``"nopic"``, delete background image, if present. If *picname* is ``None``,
R. David Murrayf877feb2009-05-05 02:08:52 +00001537 return the filename of the current backgroundimage. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001538
R. David Murrayf877feb2009-05-05 02:08:52 +00001539 >>> screen.bgpic()
1540 'nopic'
1541 >>> screen.bgpic("landscape.gif")
1542 >>> screen.bgpic()
1543 "landscape.gif"
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001544
1545
1546.. function:: clear()
1547 clearscreen()
1548
1549 Delete all drawings and all turtles from the TurtleScreen. Reset the now
1550 empty TurtleScreen to its initial state: white background, no background
1551 image, no event bindings and tracing on.
1552
1553 .. note::
1554 This TurtleScreen method is available as a global function only under the
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001555 name ``clearscreen``. The global function ``clear`` is a different one
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001556 derived from the Turtle method ``clear``.
1557
1558
1559.. function:: reset()
1560 resetscreen()
1561
1562 Reset all Turtles on the Screen to their initial state.
1563
1564 .. note::
1565 This TurtleScreen method is available as a global function only under the
1566 name ``resetscreen``. The global function ``reset`` is another one
1567 derived from the Turtle method ``reset``.
1568
1569
1570.. function:: screensize(canvwidth=None, canvheight=None, bg=None)
1571
Georg Brandlff2ad0e2009-04-27 16:51:45 +00001572 :param canvwidth: positive integer, new width of canvas in pixels
1573 :param canvheight: positive integer, new height of canvas in pixels
1574 :param bg: colorstring or color-tuple, new background color
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001575
1576 If no arguments are given, return current (canvaswidth, canvasheight). Else
1577 resize the canvas the turtles are drawing on. Do not alter the drawing
1578 window. To observe hidden parts of the canvas, use the scrollbars. With this
1579 method, one can make visible those parts of a drawing which were outside the
1580 canvas before.
1581
R. David Murrayf877feb2009-05-05 02:08:52 +00001582 >>> screen.screensize()
1583 (400, 300)
1584 >>> screen.screensize(2000,1500)
1585 >>> screen.screensize()
1586 (2000, 1500)
1587
1588 e.g. to search for an erroneously escaped turtle ;-)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001589
1590
1591.. function:: setworldcoordinates(llx, lly, urx, ury)
1592
1593 :param llx: a number, x-coordinate of lower left corner of canvas
1594 :param lly: a number, y-coordinate of lower left corner of canvas
1595 :param urx: a number, x-coordinate of upper right corner of canvas
1596 :param ury: a number, y-coordinate of upper right corner of canvas
1597
1598 Set up user-defined coordinate system and switch to mode "world" if
1599 necessary. This performs a ``screen.reset()``. If mode "world" is already
1600 active, all drawings are redrawn according to the new coordinates.
1601
1602 **ATTENTION**: in user-defined coordinate systems angles may appear
1603 distorted.
1604
R. David Murrayf877feb2009-05-05 02:08:52 +00001605 .. doctest::
1606
1607 >>> screen.reset()
1608 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1609 >>> for _ in range(72):
1610 ... left(10)
1611 ...
1612 >>> for _ in range(8):
1613 ... left(45); fd(2) # a regular octagon
1614
1615 .. doctest::
1616 :hide:
1617
1618 >>> screen.reset()
1619 >>> for t in turtles():
1620 ... t.reset()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001621
1622
1623Animation control
1624-----------------
1625
1626.. function:: delay(delay=None)
1627
1628 :param delay: positive integer
1629
1630 Set or return the drawing *delay* in milliseconds. (This is approximately
Georg Brandl2ee470f2008-07-16 12:55:28 +00001631 the time interval between two consecutive canvas updates.) The longer the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001632 drawing delay, the slower the animation.
1633
1634 Optional argument:
1635
R. David Murrayf877feb2009-05-05 02:08:52 +00001636 .. doctest::
1637
1638 >>> screen.delay()
1639 10
1640 >>> screen.delay(5)
1641 >>> screen.delay()
1642 5
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001643
1644
1645.. function:: tracer(n=None, delay=None)
1646
1647 :param n: nonnegative integer
1648 :param delay: nonnegative integer
1649
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001650 Turn turtle animation on/off and set delay for update drawings. If
1651 *n* is given, only each n-th regular screen update is really
1652 performed. (Can be used to accelerate the drawing of complex
1653 graphics.) When called without arguments, returns the currently
1654 stored value of n. Second argument sets delay value (see
1655 :func:`delay`).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001656
R. David Murrayf877feb2009-05-05 02:08:52 +00001657 .. doctest::
1658
1659 >>> screen.tracer(8, 25)
1660 >>> dist = 2
1661 >>> for i in range(200):
1662 ... fd(dist)
1663 ... rt(90)
1664 ... dist += 2
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001665
1666
1667.. function:: update()
1668
1669 Perform a TurtleScreen update. To be used when tracer is turned off.
1670
1671See also the RawTurtle/Turtle method :func:`speed`.
1672
1673
1674Using screen events
1675-------------------
1676
1677.. function:: listen(xdummy=None, ydummy=None)
1678
1679 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1680 are provided in order to be able to pass :func:`listen` to the onclick method.
1681
1682
1683.. function:: onkey(fun, key)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001684 onkeyrelease(fun, key)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001685
1686 :param fun: a function with no arguments or ``None``
1687 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1688
1689 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1690 are removed. Remark: in order to be able to register key-events, TurtleScreen
1691 must have the focus. (See method :func:`listen`.)
1692
R. David Murrayf877feb2009-05-05 02:08:52 +00001693 .. doctest::
1694
1695 >>> def f():
1696 ... fd(50)
1697 ... lt(60)
1698 ...
1699 >>> screen.onkey(f, "Up")
1700 >>> screen.listen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001701
1702
R. David Murray7fcd3de2009-06-25 14:26:19 +00001703.. function:: onkeypress(fun, key=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001704
1705 :param fun: a function with no arguments or ``None``
1706 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1707
1708 Bind *fun* to key-press event of key if key is given,
1709 or to any key-press-event if no key is given.
1710 Remark: in order to be able to register key-events, TurtleScreen
1711 must have focus. (See method :func:`listen`.)
1712
1713 .. doctest::
1714
1715 >>> def f():
1716 ... fd(50)
1717 ...
1718 >>> screen.onkey(f, "Up")
1719 >>> screen.listen()
1720
1721
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001722.. function:: onclick(fun, btn=1, add=None)
1723 onscreenclick(fun, btn=1, add=None)
1724
1725 :param fun: a function with two arguments which will be called with the
1726 coordinates of the clicked point on the canvas
1727 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1728 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1729 added, otherwise it will replace a former binding
1730
1731 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1732 existing bindings are removed.
1733
1734 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1735 named turtle:
1736
R. David Murrayf877feb2009-05-05 02:08:52 +00001737 .. doctest::
1738
1739 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1740 >>> # make the turtle move to the clicked point.
1741 >>> screen.onclick(None) # remove event binding again
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001742
1743 .. note::
1744 This TurtleScreen method is available as a global function only under the
1745 name ``onscreenclick``. The global function ``onclick`` is another one
1746 derived from the Turtle method ``onclick``.
1747
1748
1749.. function:: ontimer(fun, t=0)
1750
1751 :param fun: a function with no arguments
1752 :param t: a number >= 0
1753
1754 Install a timer that calls *fun* after *t* milliseconds.
1755
R. David Murrayf877feb2009-05-05 02:08:52 +00001756 .. doctest::
1757
1758 >>> running = True
1759 >>> def f():
1760 ... if running:
1761 ... fd(50)
1762 ... lt(60)
1763 ... screen.ontimer(f, 250)
1764 >>> f() ### makes the turtle march around
1765 >>> running = False
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001766
1767
Georg Brandleaa84ef2009-05-05 08:14:33 +00001768.. function:: mainloop()
1769
1770 Starts event loop - calling Tkinter's mainloop function.
1771 Must be the last statement in a turtle graphics program.
1772 Must *not* be used if a script is run from within IDLE in -n mode
1773 (No subprocess) - for interactive use of turtle graphics. ::
1774
1775 >>> screen.mainloop()
1776
1777
1778Input methods
1779-------------
1780
1781.. function:: textinput(title, prompt)
1782
1783 :param title: string
1784 :param prompt: string
1785
1786 Pop up a dialog window for input of a string. Parameter title is
1787 the title of the dialog window, propmt is a text mostly describing
1788 what information to input.
1789 Return the string input. If the dialog is canceled, return None. ::
1790
1791 >>> screen.textinput("NIM", "Name of first player:")
1792
1793
R. David Murray7fcd3de2009-06-25 14:26:19 +00001794.. function:: numinput(title, prompt, default=None, minval=None, maxval=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001795
1796 :param title: string
1797 :param prompt: string
1798 :param default: number (optional)
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001799 :param minval: number (optional)
1800 :param maxval: number (optional)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001801
1802 Pop up a dialog window for input of a number. title is the title of the
1803 dialog window, prompt is a text mostly describing what numerical information
1804 to input. default: default value, minval: minimum value for imput,
1805 maxval: maximum value for input
1806 The number input must be in the range minval .. maxval if these are
1807 given. If not, a hint is issued and the dialog remains open for
1808 correction.
1809 Return the number input. If the dialog is canceled, return None. ::
1810
1811 >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000)
1812
1813
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001814Settings and special methods
1815----------------------------
1816
1817.. function:: mode(mode=None)
1818
1819 :param mode: one of the strings "standard", "logo" or "world"
1820
1821 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1822 is not given, current mode is returned.
1823
1824 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1825 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1826 "world coordinates". **Attention**: in this mode angles appear distorted if
1827 ``x/y`` unit-ratio doesn't equal 1.
1828
1829 ============ ========================= ===================
1830 Mode Initial turtle heading positive angles
1831 ============ ========================= ===================
1832 "standard" to the right (east) counterclockwise
1833 "logo" upward (north) clockwise
1834 ============ ========================= ===================
1835
R. David Murrayf877feb2009-05-05 02:08:52 +00001836 .. doctest::
1837
1838 >>> mode("logo") # resets turtle heading to north
1839 >>> mode()
1840 'logo'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001841
1842
1843.. function:: colormode(cmode=None)
1844
1845 :param cmode: one of the values 1.0 or 255
1846
1847 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1848 values of color triples have to be in the range 0..\ *cmode*.
1849
R. David Murrayf877feb2009-05-05 02:08:52 +00001850 .. doctest::
1851
1852 >>> screen.colormode(1)
1853 >>> turtle.pencolor(240, 160, 80)
1854 Traceback (most recent call last):
1855 ...
1856 TurtleGraphicsError: bad color sequence: (240, 160, 80)
1857 >>> screen.colormode()
1858 1.0
1859 >>> screen.colormode(255)
1860 >>> screen.colormode()
1861 255
1862 >>> turtle.pencolor(240,160,80)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001863
1864
1865.. function:: getcanvas()
1866
1867 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1868 do with a Tkinter Canvas.
1869
R. David Murrayf877feb2009-05-05 02:08:52 +00001870 .. doctest::
1871
1872 >>> cv = screen.getcanvas()
1873 >>> cv
1874 <turtle.ScrolledCanvas instance at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001875
1876
1877.. function:: getshapes()
1878
1879 Return a list of names of all currently available turtle shapes.
1880
R. David Murrayf877feb2009-05-05 02:08:52 +00001881 .. doctest::
1882
1883 >>> screen.getshapes()
1884 ['arrow', 'blank', 'circle', ..., 'turtle']
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001885
1886
1887.. function:: register_shape(name, shape=None)
1888 addshape(name, shape=None)
1889
1890 There are three different ways to call this function:
1891
1892 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
R. David Murrayf877feb2009-05-05 02:08:52 +00001893 corresponding image shape. ::
1894
1895 >>> screen.register_shape("turtle.gif")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001896
1897 .. note::
1898 Image shapes *do not* rotate when turning the turtle, so they do not
1899 display the heading of the turtle!
1900
1901 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1902 coordinates: Install the corresponding polygon shape.
1903
R. David Murrayf877feb2009-05-05 02:08:52 +00001904 .. doctest::
1905
1906 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
1907
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001908 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
1909 object: Install the corresponding compound shape.
1910
1911 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1912 shapes can be used by issuing the command ``shape(shapename)``.
1913
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001914
1915.. function:: turtles()
1916
1917 Return the list of turtles on the screen.
1918
R. David Murrayf877feb2009-05-05 02:08:52 +00001919 .. doctest::
1920
1921 >>> for turtle in screen.turtles():
1922 ... turtle.color("red")
Georg Brandl116aa622007-08-15 14:28:22 +00001923
Georg Brandl116aa622007-08-15 14:28:22 +00001924
1925.. function:: window_height()
1926
R. David Murrayf877feb2009-05-05 02:08:52 +00001927 Return the height of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001928
R. David Murrayf877feb2009-05-05 02:08:52 +00001929 >>> screen.window_height()
1930 480
Georg Brandl116aa622007-08-15 14:28:22 +00001931
Georg Brandl116aa622007-08-15 14:28:22 +00001932
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001933.. function:: window_width()
1934
R. David Murrayf877feb2009-05-05 02:08:52 +00001935 Return the width of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001936
R. David Murrayf877feb2009-05-05 02:08:52 +00001937 >>> screen.window_width()
1938 640
Georg Brandl116aa622007-08-15 14:28:22 +00001939
1940
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001941.. _screenspecific:
Georg Brandl116aa622007-08-15 14:28:22 +00001942
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001943Methods specific to Screen, not inherited from TurtleScreen
1944-----------------------------------------------------------
1945
1946.. function:: bye()
1947
1948 Shut the turtlegraphics window.
Georg Brandl116aa622007-08-15 14:28:22 +00001949
1950
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001951.. function:: exitonclick()
Georg Brandl116aa622007-08-15 14:28:22 +00001952
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001953 Bind bye() method to mouse clicks on the Screen.
Georg Brandl116aa622007-08-15 14:28:22 +00001954
1955
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001956 If the value "using_IDLE" in the configuration dictionary is ``False``
1957 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
1958 (no subprocess) is used, this value should be set to ``True`` in
1959 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1960 client script.
Georg Brandl116aa622007-08-15 14:28:22 +00001961
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001962
1963.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
1964
1965 Set the size and position of the main window. Default values of arguments
Georg Brandl6faee4e2010-09-21 14:48:28 +00001966 are stored in the configuration dictionary and can be changed via a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001967 :file:`turtle.cfg` file.
1968
1969 :param width: if an integer, a size in pixels, if a float, a fraction of the
1970 screen; default is 50% of screen
1971 :param height: if an integer, the height in pixels, if a float, a fraction of
1972 the screen; default is 75% of screen
1973 :param startx: if positive, starting position in pixels from the left
1974 edge of the screen, if negative from the right edge, if None,
1975 center window horizontally
1976 :param startx: if positive, starting position in pixels from the top
1977 edge of the screen, if negative from the bottom edge, if None,
1978 center window vertically
1979
R. David Murrayf877feb2009-05-05 02:08:52 +00001980 .. doctest::
1981
1982 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1983 >>> # sets window to 200x200 pixels, in upper left of screen
1984 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1985 >>> # sets window to 75% of screen by 50% of screen and centers
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001986
1987
1988.. function:: title(titlestring)
1989
1990 :param titlestring: a string that is shown in the titlebar of the turtle
1991 graphics window
1992
1993 Set title of turtle window to *titlestring*.
1994
R. David Murrayf877feb2009-05-05 02:08:52 +00001995 .. doctest::
1996
1997 >>> screen.title("Welcome to the turtle zoo!")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001998
1999
2000The public classes of the module :mod:`turtle`
2001==============================================
2002
2003
2004.. class:: RawTurtle(canvas)
2005 RawPen(canvas)
2006
Ezio Melotti1a263ad2010-03-14 09:51:37 +00002007 :param canvas: a :class:`tkinter.Canvas`, a :class:`ScrolledCanvas` or a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002008 :class:`TurtleScreen`
2009
R. David Murrayf877feb2009-05-05 02:08:52 +00002010 Create a turtle. The turtle has all methods described above as "methods of
2011 Turtle/RawTurtle".
Georg Brandl116aa622007-08-15 14:28:22 +00002012
2013
2014.. class:: Turtle()
2015
R. David Murrayf877feb2009-05-05 02:08:52 +00002016 Subclass of RawTurtle, has the same interface but draws on a default
2017 :class:`Screen` object created automatically when needed for the first time.
Georg Brandl116aa622007-08-15 14:28:22 +00002018
2019
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002020.. class:: TurtleScreen(cv)
Georg Brandl116aa622007-08-15 14:28:22 +00002021
Ezio Melotti1a263ad2010-03-14 09:51:37 +00002022 :param cv: a :class:`tkinter.Canvas`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002023
2024 Provides screen oriented methods like :func:`setbg` etc. that are described
2025 above.
2026
2027.. class:: Screen()
2028
2029 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
2030
Georg Brandl48310cd2009-01-03 21:18:54 +00002031
Benjamin Petersona0dfa822009-11-13 02:25:08 +00002032.. class:: ScrolledCanvas(master)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002033
2034 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
2035 a Tkinter-canvas with scrollbars added
2036
2037 Used by class Screen, which thus automatically provides a ScrolledCanvas as
2038 playground for the turtles.
2039
2040.. class:: Shape(type_, data)
2041
2042 :param type\_: one of the strings "polygon", "image", "compound"
2043
2044 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
2045 specification:
Georg Brandl116aa622007-08-15 14:28:22 +00002046
2047
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002048 =========== ===========
2049 *type_* *data*
2050 =========== ===========
2051 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
2052 "image" an image (in this form only used internally!)
Georg Brandlae2dbe22009-03-13 19:04:40 +00002053 "compound" ``None`` (a compound shape has to be constructed using the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002054 :meth:`addcomponent` method)
2055 =========== ===========
Georg Brandl48310cd2009-01-03 21:18:54 +00002056
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002057 .. method:: addcomponent(poly, fill, outline=None)
Georg Brandl116aa622007-08-15 14:28:22 +00002058
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002059 :param poly: a polygon, i.e. a tuple of pairs of numbers
2060 :param fill: a color the *poly* will be filled with
2061 :param outline: a color for the poly's outline (if given)
Georg Brandl48310cd2009-01-03 21:18:54 +00002062
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002063 Example:
Georg Brandl116aa622007-08-15 14:28:22 +00002064
R. David Murrayf877feb2009-05-05 02:08:52 +00002065 .. doctest::
2066
2067 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
2068 >>> s = Shape("compound")
2069 >>> s.addcomponent(poly, "red", "blue")
2070 >>> # ... add more components and then use register_shape()
Georg Brandl116aa622007-08-15 14:28:22 +00002071
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002072 See :ref:`compoundshapes`.
Georg Brandl116aa622007-08-15 14:28:22 +00002073
2074
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002075.. class:: Vec2D(x, y)
Georg Brandl116aa622007-08-15 14:28:22 +00002076
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002077 A two-dimensional vector class, used as a helper class for implementing
2078 turtle graphics. May be useful for turtle graphics programs too. Derived
2079 from tuple, so a vector is a tuple!
2080
2081 Provides (for *a*, *b* vectors, *k* number):
2082
2083 * ``a + b`` vector addition
2084 * ``a - b`` vector subtraction
2085 * ``a * b`` inner product
2086 * ``k * a`` and ``a * k`` multiplication with scalar
2087 * ``abs(a)`` absolute value of a
2088 * ``a.rotate(angle)`` rotation
2089
2090
2091Help and configuration
2092======================
2093
2094How to use help
2095---------------
2096
2097The public methods of the Screen and Turtle classes are documented extensively
2098via docstrings. So these can be used as online-help via the Python help
2099facilities:
2100
2101- When using IDLE, tooltips show the signatures and first lines of the
2102 docstrings of typed in function-/method calls.
2103
2104- Calling :func:`help` on methods or functions displays the docstrings::
2105
2106 >>> help(Screen.bgcolor)
2107 Help on method bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002108
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002109 bgcolor(self, *args) unbound turtle.Screen method
2110 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002111
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002112 Arguments (if given): a color string or three numbers
2113 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002114
2115
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002116 >>> screen.bgcolor("orange")
2117 >>> screen.bgcolor()
2118 "orange"
2119 >>> screen.bgcolor(0.5,0,0.5)
2120 >>> screen.bgcolor()
2121 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002122
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002123 >>> help(Turtle.penup)
2124 Help on method penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002125
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002126 penup(self) unbound turtle.Turtle method
2127 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002128
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002129 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002130
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002131 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002132
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002133 >>> turtle.penup()
2134
2135- The docstrings of the functions which are derived from methods have a modified
2136 form::
2137
2138 >>> help(bgcolor)
2139 Help on function bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002140
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002141 bgcolor(*args)
2142 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002143
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002144 Arguments (if given): a color string or three numbers
2145 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002146
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002147 Example::
Georg Brandl48310cd2009-01-03 21:18:54 +00002148
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002149 >>> bgcolor("orange")
2150 >>> bgcolor()
2151 "orange"
2152 >>> bgcolor(0.5,0,0.5)
2153 >>> bgcolor()
2154 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002155
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002156 >>> help(penup)
2157 Help on function penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002158
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002159 penup()
2160 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002161
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002162 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002163
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002164 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002165
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002166 Example:
2167 >>> penup()
2168
2169These modified docstrings are created automatically together with the function
2170definitions that are derived from the methods at import time.
2171
2172
2173Translation of docstrings into different languages
2174--------------------------------------------------
2175
2176There is a utility to create a dictionary the keys of which are the method names
2177and the values of which are the docstrings of the public methods of the classes
2178Screen and Turtle.
2179
2180.. function:: write_docstringdict(filename="turtle_docstringdict")
2181
2182 :param filename: a string, used as filename
2183
2184 Create and write docstring-dictionary to a Python script with the given
2185 filename. This function has to be called explicitly (it is not used by the
2186 turtle graphics classes). The docstring dictionary will be written to the
2187 Python script :file:`{filename}.py`. It is intended to serve as a template
2188 for translation of the docstrings into different languages.
2189
2190If you (or your students) want to use :mod:`turtle` with online help in your
2191native language, you have to translate the docstrings and save the resulting
2192file as e.g. :file:`turtle_docstringdict_german.py`.
2193
2194If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2195will be read in at import time and will replace the original English docstrings.
2196
2197At the time of this writing there are docstring dictionaries in German and in
2198Italian. (Requests please to glingl@aon.at.)
2199
2200
2201
2202How to configure Screen and Turtles
2203-----------------------------------
2204
2205The built-in default configuration mimics the appearance and behaviour of the
2206old turtle module in order to retain best possible compatibility with it.
2207
2208If you want to use a different configuration which better reflects the features
2209of this module or which better fits to your needs, e.g. for use in a classroom,
2210you can prepare a configuration file ``turtle.cfg`` which will be read at import
2211time and modify the configuration according to its settings.
2212
2213The built in configuration would correspond to the following turtle.cfg::
2214
2215 width = 0.5
2216 height = 0.75
2217 leftright = None
2218 topbottom = None
2219 canvwidth = 400
2220 canvheight = 300
2221 mode = standard
2222 colormode = 1.0
2223 delay = 10
2224 undobuffersize = 1000
2225 shape = classic
2226 pencolor = black
2227 fillcolor = black
2228 resizemode = noresize
2229 visible = True
2230 language = english
2231 exampleturtle = turtle
2232 examplescreen = screen
2233 title = Python Turtle Graphics
2234 using_IDLE = False
2235
2236Short explanation of selected entries:
2237
2238- The first four lines correspond to the arguments of the :meth:`Screen.setup`
2239 method.
2240- Line 5 and 6 correspond to the arguments of the method
2241 :meth:`Screen.screensize`.
2242- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2243 info try ``help(shape)``.
2244- If you want to use no fillcolor (i.e. make the turtle transparent), you have
2245 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
2246 the cfg-file).
2247- If you want to reflect the turtle its state, you have to use ``resizemode =
2248 auto``.
2249- If you set e.g. ``language = italian`` the docstringdict
2250 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
2251 present on the import path, e.g. in the same directory as :mod:`turtle`.
2252- The entries *exampleturtle* and *examplescreen* define the names of these
2253 objects as they occur in the docstrings. The transformation of
2254 method-docstrings to function-docstrings will delete these names from the
2255 docstrings.
2256- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
2257 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
2258 mainloop.
2259
2260There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2261stored and an additional one in the current working directory. The latter will
2262override the settings of the first one.
2263
2264The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can
2265study it as an example and see its effects when running the demos (preferably
2266not from within the demo-viewer).
2267
2268
2269Demo scripts
2270============
2271
2272There is a set of demo scripts in the turtledemo directory located in the
2273:file:`Demo/turtle` directory in the source distribution.
2274
2275It contains:
2276
Georg Brandlae2dbe22009-03-13 19:04:40 +00002277- a set of 15 demo scripts demonstrating different features of the new module
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002278 :mod:`turtle`
2279- a demo viewer :file:`turtleDemo.py` which can be used to view the sourcecode
2280 of the scripts and run them at the same time. 14 of the examples can be
2281 accessed via the Examples menu; all of them can also be run standalone.
2282- The example :file:`turtledemo_two_canvases.py` demonstrates the simultaneous
2283 use of two canvases with the turtle module. Therefore it only can be run
2284 standalone.
2285- There is a :file:`turtle.cfg` file in this directory, which also serves as an
2286 example for how to write and use such files.
2287
2288The demoscripts are:
2289
2290+----------------+------------------------------+-----------------------+
2291| Name | Description | Features |
2292+----------------+------------------------------+-----------------------+
2293| bytedesign | complex classical | :func:`tracer`, delay,|
2294| | turtlegraphics pattern | :func:`update` |
2295+----------------+------------------------------+-----------------------+
2296| chaos | graphs verhust dynamics, | world coordinates |
2297| | proves that you must not | |
2298| | trust computers' computations| |
2299+----------------+------------------------------+-----------------------+
2300| clock | analog clock showing time | turtles as clock's |
2301| | of your computer | hands, ontimer |
2302+----------------+------------------------------+-----------------------+
2303| colormixer | experiment with r, g, b | :func:`ondrag` |
2304+----------------+------------------------------+-----------------------+
2305| fractalcurves | Hilbert & Koch curves | recursion |
2306+----------------+------------------------------+-----------------------+
2307| lindenmayer | ethnomathematics | L-System |
2308| | (indian kolams) | |
2309+----------------+------------------------------+-----------------------+
2310| minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
2311| | | as Hanoi discs |
2312| | | (shape, shapesize) |
2313+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002314| nim | play the classical nim game | turtles as nimsticks, |
2315| | with three heaps of sticks | event driven (mouse, |
2316| | against the computer. | keyboard) |
2317+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002318| paint | super minimalistic | :func:`onclick` |
2319| | drawing program | |
2320+----------------+------------------------------+-----------------------+
2321| peace | elementary | turtle: appearance |
2322| | | and animation |
2323+----------------+------------------------------+-----------------------+
2324| penrose | aperiodic tiling with | :func:`stamp` |
2325| | kites and darts | |
2326+----------------+------------------------------+-----------------------+
2327| planet_and_moon| simulation of | compound shapes, |
2328| | gravitational system | :class:`Vec2D` |
2329+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002330| round_dance | dancing turtles rotating | compound shapes, clone|
2331| | pairwise in opposite | shapesize, tilt, |
Alexander Belopolskyc08f5442010-10-21 22:29:36 +00002332| | direction | get_shapepoly, update |
Georg Brandleaa84ef2009-05-05 08:14:33 +00002333+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002334| tree | a (graphical) breadth | :func:`clone` |
2335| | first tree (using generators)| |
2336+----------------+------------------------------+-----------------------+
2337| wikipedia | a pattern from the wikipedia | :func:`clone`, |
2338| | article on turtle graphics | :func:`undo` |
2339+----------------+------------------------------+-----------------------+
2340| yingyang | another elementary example | :func:`circle` |
2341+----------------+------------------------------+-----------------------+
2342
2343Have fun!
2344
2345
2346Changes since Python 2.6
2347========================
2348
Georg Brandl48310cd2009-01-03 21:18:54 +00002349- The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and
2350 :meth:`Turtle.window_height` have been eliminated.
2351 Methods with these names and functionality are now available only
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002352 as methods of :class:`Screen`. The functions derived from these remain
Georg Brandl48310cd2009-01-03 21:18:54 +00002353 available. (In fact already in Python 2.6 these methods were merely
2354 duplications of the corresponding
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002355 :class:`TurtleScreen`/:class:`Screen`-methods.)
2356
Georg Brandl48310cd2009-01-03 21:18:54 +00002357- The method :meth:`Turtle.fill` has been eliminated.
2358 The behaviour of :meth:`begin_fill` and :meth:`end_fill`
2359 have changed slightly: now every filling-process must be completed with an
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002360 ``end_fill()`` call.
Georg Brandl48310cd2009-01-03 21:18:54 +00002361
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002362- A method :meth:`Turtle.filling` has been added. It returns a boolean
2363 value: ``True`` if a filling process is under way, ``False`` otherwise.
2364 This behaviour corresponds to a ``fill()`` call without arguments in
Georg Brandl23d11d32008-09-21 07:50:52 +00002365 Python 2.6.
Georg Brandl116aa622007-08-15 14:28:22 +00002366
Georg Brandleaa84ef2009-05-05 08:14:33 +00002367Changes since Python 3.0
2368========================
2369
2370- The methods :meth:`Turtle.shearfactor`, :meth:`Turtle.shapetransform` and
2371 :meth:`Turtle.get_shapepoly` have been added. Thus the full range of
2372 regular linear transforms is now available for transforming turtle shapes.
2373 :meth:`Turtle.tiltangle` has been enhanced in functionality: it now can
2374 be used to get or set the tiltangle. :meth:`Turtle.settiltangle` has been
2375 deprecated.
2376
2377- The method :meth:`Screen.onkeypress` has been added as a complement to
2378 :meth:`Screen.onkey` which in fact binds actions to the keyrelease event.
2379 Accordingly the latter has got an alias: :meth:`Screen.onkeyrelease`.
2380
2381- The method :meth:`Screen.mainloop` has been added. So when working only
2382 with Screen and Turtle objects one must not additonally import
2383 :func:`mainloop` anymore.
2384
2385- Two input methods has been added :meth:`Screen.textinput` and
2386 :meth:`Screen.numinput`. These popup input dialogs and return
2387 strings and numbers respectively.
2388
2389- Two example scripts :file:`tdemo_nim.py` and :file:`tdemo_round_dance.py`
2390 have been added to the Demo directory (source distribution only). As usual
2391 they can be viewed and executed within the demo viewer :file:`turtleDemo.py`.
2392
R. David Murrayf877feb2009-05-05 02:08:52 +00002393
2394.. doctest::
2395 :hide:
2396
2397 >>> for turtle in turtles():
2398 ... turtle.reset()
2399 >>> turtle.penup()
2400 >>> turtle.goto(-200,25)
2401 >>> turtle.pendown()
2402 >>> turtle.write("No one expects the Spanish Inquisition!",
2403 ... font=("Arial", 20, "normal"))
2404 >>> turtle.penup()
2405 >>> turtle.goto(-100,-50)
2406 >>> turtle.pendown()
2407 >>> turtle.write("Our two chief Turtles are...",
2408 ... font=("Arial", 16, "normal"))
2409 >>> turtle.penup()
2410 >>> turtle.goto(-450,-75)
2411 >>> turtle.write(str(turtles()))