blob: b3387b6d88a24a22621d8a25d825895dcb5dd243 [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
38and procedure-oriented ways. Because it uses :mod:`Tkinter` for the underlying
39graphics, it needs a version of python installed with Tk support.
40
41The object-oriented interface uses essentially two+two classes:
42
431. The :class:`TurtleScreen` class defines graphics windows as a playground for
44 the drawing turtles. Its constructor needs a :class:`Tkinter.Canvas` or a
45 :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is
46 used as part of some application.
47
Martin v. Löwis601149b2008-09-29 22:19:08 +000048 The function :func:`Screen` returns a singleton object of a
49 :class:`TurtleScreen` subclass. This function should be used when
50 :mod:`turtle` is used as a standalone tool for doing graphics.
51 As a singleton object, inheriting from its class is not possible.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000052
53 All methods of TurtleScreen/Screen also exist as functions, i.e. as part of
54 the procedure-oriented interface.
55
562. :class:`RawTurtle` (alias: :class:`RawPen`) defines Turtle objects which draw
57 on a :class:`TurtleScreen`. Its constructor needs a Canvas, ScrolledCanvas
58 or TurtleScreen as argument, so the RawTurtle objects know where to draw.
59
60 Derived from RawTurtle is the subclass :class:`Turtle` (alias: :class:`Pen`),
61 which draws on "the" :class:`Screen` - instance which is automatically
62 created, if not already present.
63
64 All methods of RawTurtle/Turtle also exist as functions, i.e. part of the
65 procedure-oriented interface.
66
67The procedural interface provides functions which are derived from the methods
68of the classes :class:`Screen` and :class:`Turtle`. They have the same names as
Georg Brandlae2dbe22009-03-13 19:04:40 +000069the corresponding methods. A screen object is automatically created whenever a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000070function derived from a Screen method is called. An (unnamed) turtle object is
71automatically created whenever any of the functions derived from a Turtle method
72is called.
73
74To use multiple turtles an a screen one has to use the object-oriented interface.
75
76.. note::
77 In the following documentation the argument list for functions is given.
78 Methods, of course, have the additional first argument *self* which is
79 omitted here.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000082Overview over available Turtle and Screen methods
83=================================================
84
85Turtle methods
86--------------
87
88Turtle motion
89 Move and draw
90 | :func:`forward` | :func:`fd`
91 | :func:`backward` | :func:`bk` | :func:`back`
92 | :func:`right` | :func:`rt`
93 | :func:`left` | :func:`lt`
94 | :func:`goto` | :func:`setpos` | :func:`setposition`
95 | :func:`setx`
96 | :func:`sety`
97 | :func:`setheading` | :func:`seth`
98 | :func:`home`
99 | :func:`circle`
100 | :func:`dot`
101 | :func:`stamp`
102 | :func:`clearstamp`
103 | :func:`clearstamps`
104 | :func:`undo`
105 | :func:`speed`
106
107 Tell Turtle's state
108 | :func:`position` | :func:`pos`
109 | :func:`towards`
110 | :func:`xcor`
111 | :func:`ycor`
112 | :func:`heading`
113 | :func:`distance`
114
115 Setting and measurement
116 | :func:`degrees`
117 | :func:`radians`
118
119Pen control
120 Drawing state
121 | :func:`pendown` | :func:`pd` | :func:`down`
122 | :func:`penup` | :func:`pu` | :func:`up`
123 | :func:`pensize` | :func:`width`
124 | :func:`pen`
125 | :func:`isdown`
126
127 Color control
128 | :func:`color`
129 | :func:`pencolor`
130 | :func:`fillcolor`
131
132 Filling
133 | :func:`filling`
134 | :func:`begin_fill`
135 | :func:`end_fill`
136
137 More drawing control
138 | :func:`reset`
139 | :func:`clear`
140 | :func:`write`
141
142Turtle state
143 Visibility
144 | :func:`showturtle` | :func:`st`
145 | :func:`hideturtle` | :func:`ht`
146 | :func:`isvisible`
147
148 Appearance
149 | :func:`shape`
150 | :func:`resizemode`
151 | :func:`shapesize` | :func:`turtlesize`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000152 | :func:`shearfactor`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000153 | :func:`settiltangle`
154 | :func:`tiltangle`
155 | :func:`tilt`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000156 | :func:`shapetransform`
157 | :func:`get_shapepoly`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000158
159Using events
160 | :func:`onclick`
161 | :func:`onrelease`
162 | :func:`ondrag`
163
164Special Turtle methods
165 | :func:`begin_poly`
166 | :func:`end_poly`
167 | :func:`get_poly`
168 | :func:`clone`
169 | :func:`getturtle` | :func:`getpen`
170 | :func:`getscreen`
171 | :func:`setundobuffer`
172 | :func:`undobufferentries`
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000175Methods of TurtleScreen/Screen
176------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000177
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000178Window control
179 | :func:`bgcolor`
180 | :func:`bgpic`
181 | :func:`clear` | :func:`clearscreen`
182 | :func:`reset` | :func:`resetscreen`
183 | :func:`screensize`
184 | :func:`setworldcoordinates`
Georg Brandl116aa622007-08-15 14:28:22 +0000185
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000186Animation control
187 | :func:`delay`
188 | :func:`tracer`
189 | :func:`update`
190
191Using screen events
192 | :func:`listen`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000193 | :func:`onkey` | :func:`onkeyrelease`
194 | :func:`onkeypress`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000195 | :func:`onclick` | :func:`onscreenclick`
196 | :func:`ontimer`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000197 | :func:`mainloop`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000198
199Settings and special methods
200 | :func:`mode`
201 | :func:`colormode`
202 | :func:`getcanvas`
203 | :func:`getshapes`
204 | :func:`register_shape` | :func:`addshape`
205 | :func:`turtles`
206 | :func:`window_height`
207 | :func:`window_width`
208
Georg Brandleaa84ef2009-05-05 08:14:33 +0000209Input methods
210 | :func:`textinput`
211 | :func:`numinput`
212
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000213Methods specific to Screen
214 | :func:`bye`
215 | :func:`exitonclick`
216 | :func:`setup`
217 | :func:`title`
Georg Brandl116aa622007-08-15 14:28:22 +0000218
219
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000220Methods of RawTurtle/Turtle and corresponding functions
221=======================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000222
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000223Most of the examples in this section refer to a Turtle instance called
224``turtle``.
Georg Brandl116aa622007-08-15 14:28:22 +0000225
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000226Turtle motion
227-------------
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229.. function:: forward(distance)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000230 fd(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000231
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000232 :param distance: a number (integer or float)
233
234 Move the turtle forward by the specified *distance*, in the direction the
235 turtle is headed.
236
R. David Murrayf877feb2009-05-05 02:08:52 +0000237 .. doctest::
238
239 >>> turtle.position()
240 (0.00,0.00)
241 >>> turtle.forward(25)
242 >>> turtle.position()
243 (25.00,0.00)
244 >>> turtle.forward(-75)
245 >>> turtle.position()
246 (-50.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000247
248
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000249.. function:: back(distance)
250 bk(distance)
251 backward(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000252
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000253 :param distance: a number
Georg Brandl116aa622007-08-15 14:28:22 +0000254
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000255 Move the turtle backward by *distance*, opposite to the direction the
256 turtle is headed. Do not change the turtle's heading.
Georg Brandl116aa622007-08-15 14:28:22 +0000257
R. David Murrayf877feb2009-05-05 02:08:52 +0000258 .. doctest::
259 :hide:
260
261 >>> turtle.goto(0, 0)
262
263 .. doctest::
264
265 >>> turtle.position()
266 (0.00,0.00)
267 >>> turtle.backward(30)
268 >>> turtle.position()
269 (-30.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271
272.. function:: right(angle)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000273 rt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000275 :param angle: a number (integer or float)
276
277 Turn turtle right by *angle* units. (Units are by default degrees, but
278 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
279 orientation depends on the turtle mode, see :func:`mode`.
280
R. David Murrayf877feb2009-05-05 02:08:52 +0000281 .. doctest::
282 :hide:
283
284 >>> turtle.setheading(22)
285
286 .. doctest::
287
288 >>> turtle.heading()
289 22.0
290 >>> turtle.right(45)
291 >>> turtle.heading()
292 337.0
Georg Brandl116aa622007-08-15 14:28:22 +0000293
294
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000295.. function:: left(angle)
296 lt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000297
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000298 :param angle: a number (integer or float)
Georg Brandl116aa622007-08-15 14:28:22 +0000299
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000300 Turn turtle left by *angle* units. (Units are by default degrees, but
301 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
302 orientation depends on the turtle mode, see :func:`mode`.
Georg Brandl116aa622007-08-15 14:28:22 +0000303
R. David Murrayf877feb2009-05-05 02:08:52 +0000304 .. doctest::
305 :hide:
306
307 >>> turtle.setheading(22)
308
309 .. doctest::
310
311 >>> turtle.heading()
312 22.0
313 >>> turtle.left(45)
314 >>> turtle.heading()
315 67.0
316
Georg Brandl116aa622007-08-15 14:28:22 +0000317
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000318.. function:: goto(x, y=None)
319 setpos(x, y=None)
320 setposition(x, y=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000321
R. David Murrayf877feb2009-05-05 02:08:52 +0000322 :param x: a number or a pair/vector of numbers
323 :param y: a number or ``None``
Georg Brandl116aa622007-08-15 14:28:22 +0000324
R. David Murrayf877feb2009-05-05 02:08:52 +0000325 If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D`
326 (e.g. as returned by :func:`pos`).
Georg Brandl116aa622007-08-15 14:28:22 +0000327
R. David Murrayf877feb2009-05-05 02:08:52 +0000328 Move turtle to an absolute position. If the pen is down, draw line. Do
329 not change the turtle's orientation.
Georg Brandl116aa622007-08-15 14:28:22 +0000330
R. David Murrayf877feb2009-05-05 02:08:52 +0000331 .. doctest::
332 :hide:
333
334 >>> turtle.goto(0, 0)
335
336 .. doctest::
337
338 >>> tp = turtle.pos()
339 >>> tp
340 (0.00,0.00)
341 >>> turtle.setpos(60,30)
342 >>> turtle.pos()
343 (60.00,30.00)
344 >>> turtle.setpos((20,80))
345 >>> turtle.pos()
346 (20.00,80.00)
347 >>> turtle.setpos(tp)
348 >>> turtle.pos()
349 (0.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000350
Georg Brandl116aa622007-08-15 14:28:22 +0000351
352.. function:: setx(x)
353
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000354 :param x: a number (integer or float)
355
356 Set the turtle's first coordinate to *x*, leave second coordinate
357 unchanged.
358
R. David Murrayf877feb2009-05-05 02:08:52 +0000359 .. doctest::
360 :hide:
361
362 >>> turtle.goto(0, 240)
363
364 .. doctest::
365
366 >>> turtle.position()
367 (0.00,240.00)
368 >>> turtle.setx(10)
369 >>> turtle.position()
370 (10.00,240.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000371
Georg Brandl116aa622007-08-15 14:28:22 +0000372
373.. function:: sety(y)
374
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000375 :param y: a number (integer or float)
376
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000377 Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000378
R. David Murrayf877feb2009-05-05 02:08:52 +0000379 .. doctest::
380 :hide:
381
382 >>> turtle.goto(0, 40)
383
384 .. doctest::
385
386 >>> turtle.position()
387 (0.00,40.00)
388 >>> turtle.sety(-10)
389 >>> turtle.position()
390 (0.00,-10.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000391
Georg Brandl116aa622007-08-15 14:28:22 +0000392
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000393.. function:: setheading(to_angle)
394 seth(to_angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000396 :param to_angle: a number (integer or float)
397
398 Set the orientation of the turtle to *to_angle*. Here are some common
399 directions in degrees:
400
401 =================== ====================
402 standard mode logo mode
403 =================== ====================
404 0 - east 0 - north
405 90 - north 90 - east
406 180 - west 180 - south
407 270 - south 270 - west
408 =================== ====================
409
R. David Murrayf877feb2009-05-05 02:08:52 +0000410 .. doctest::
411
412 >>> turtle.setheading(90)
413 >>> turtle.heading()
414 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000415
416
417.. function:: home()
418
419 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
420 its start-orientation (which depends on the mode, see :func:`mode`).
421
R. David Murrayf877feb2009-05-05 02:08:52 +0000422 .. doctest::
423 :hide:
424
425 >>> turtle.setheading(90)
426 >>> turtle.goto(0, -10)
427
428 .. doctest::
429
430 >>> turtle.heading()
431 90.0
432 >>> turtle.position()
433 (0.00,-10.00)
434 >>> turtle.home()
435 >>> turtle.position()
436 (0.00,0.00)
437 >>> turtle.heading()
438 0.0
439
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000440
441.. function:: circle(radius, extent=None, steps=None)
442
443 :param radius: a number
444 :param extent: a number (or ``None``)
445 :param steps: an integer (or ``None``)
446
447 Draw a circle with given *radius*. The center is *radius* units left of
448 the turtle; *extent* -- an angle -- determines which part of the circle
449 is drawn. If *extent* is not given, draw the entire circle. If *extent*
450 is not a full circle, one endpoint of the arc is the current pen
451 position. Draw the arc in counterclockwise direction if *radius* is
452 positive, otherwise in clockwise direction. Finally the direction of the
453 turtle is changed by the amount of *extent*.
454
455 As the circle is approximated by an inscribed regular polygon, *steps*
456 determines the number of steps to use. If not given, it will be
457 calculated automatically. May be used to draw regular polygons.
458
R. David Murrayf877feb2009-05-05 02:08:52 +0000459 .. doctest::
460
461 >>> turtle.home()
462 >>> turtle.position()
463 (0.00,0.00)
464 >>> turtle.heading()
465 0.0
466 >>> turtle.circle(50)
467 >>> turtle.position()
468 (-0.00,0.00)
469 >>> turtle.heading()
470 0.0
471 >>> turtle.circle(120, 180) # draw a semicircle
472 >>> turtle.position()
473 (0.00,240.00)
474 >>> turtle.heading()
475 180.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000476
477
478.. function:: dot(size=None, *color)
479
480 :param size: an integer >= 1 (if given)
481 :param color: a colorstring or a numeric color tuple
482
483 Draw a circular dot with diameter *size*, using *color*. If *size* is
484 not given, the maximum of pensize+4 and 2*pensize is used.
485
R. David Murrayf877feb2009-05-05 02:08:52 +0000486
487 .. doctest::
488
489 >>> turtle.home()
490 >>> turtle.dot()
491 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
492 >>> turtle.position()
493 (100.00,-0.00)
494 >>> turtle.heading()
495 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000496
497
498.. function:: stamp()
499
500 Stamp a copy of the turtle shape onto the canvas at the current turtle
501 position. Return a stamp_id for that stamp, which can be used to delete
502 it by calling ``clearstamp(stamp_id)``.
503
R. David Murrayf877feb2009-05-05 02:08:52 +0000504 .. doctest::
505
506 >>> turtle.color("blue")
507 >>> turtle.stamp()
508 11
509 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000510
511
512.. function:: clearstamp(stampid)
513
514 :param stampid: an integer, must be return value of previous
515 :func:`stamp` call
516
517 Delete stamp with given *stampid*.
518
R. David Murrayf877feb2009-05-05 02:08:52 +0000519 .. doctest::
520
521 >>> turtle.position()
522 (150.00,-0.00)
523 >>> turtle.color("blue")
524 >>> astamp = turtle.stamp()
525 >>> turtle.fd(50)
526 >>> turtle.position()
527 (200.00,-0.00)
528 >>> turtle.clearstamp(astamp)
529 >>> turtle.position()
530 (200.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000531
532
533.. function:: clearstamps(n=None)
534
535 :param n: an integer (or ``None``)
536
537 Delete all or first/last *n* of turtle's stamps. If *n* is None, delete
538 all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete
539 last *n* stamps.
540
R. David Murrayf877feb2009-05-05 02:08:52 +0000541 .. doctest::
542
543 >>> for i in range(8):
544 ... turtle.stamp(); turtle.fd(30)
545 13
546 14
547 15
548 16
549 17
550 18
551 19
552 20
553 >>> turtle.clearstamps(2)
554 >>> turtle.clearstamps(-2)
555 >>> turtle.clearstamps()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000556
557
558.. function:: undo()
559
560 Undo (repeatedly) the last turtle action(s). Number of available
561 undo actions is determined by the size of the undobuffer.
562
R. David Murrayf877feb2009-05-05 02:08:52 +0000563 .. doctest::
564
565 >>> for i in range(4):
566 ... turtle.fd(50); turtle.lt(80)
567 ...
568 >>> for i in range(8):
569 ... turtle.undo()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000570
571
572.. function:: speed(speed=None)
573
574 :param speed: an integer in the range 0..10 or a speedstring (see below)
575
576 Set the turtle's speed to an integer value in the range 0..10. If no
577 argument is given, return current speed.
578
579 If input is a number greater than 10 or smaller than 0.5, speed is set
580 to 0. Speedstrings are mapped to speedvalues as follows:
581
582 * "fastest": 0
583 * "fast": 10
584 * "normal": 6
585 * "slow": 3
586 * "slowest": 1
587
588 Speeds from 1 to 10 enforce increasingly faster animation of line drawing
589 and turtle turning.
590
591 Attention: *speed* = 0 means that *no* animation takes
592 place. forward/back makes turtle jump and likewise left/right make the
593 turtle turn instantly.
594
R. David Murrayf877feb2009-05-05 02:08:52 +0000595 .. doctest::
596
597 >>> turtle.speed()
598 3
599 >>> turtle.speed('normal')
600 >>> turtle.speed()
601 6
602 >>> turtle.speed(9)
603 >>> turtle.speed()
604 9
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000605
606
607Tell Turtle's state
608-------------------
609
610.. function:: position()
611 pos()
612
613 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
614
R. David Murrayf877feb2009-05-05 02:08:52 +0000615 .. doctest::
616
617 >>> turtle.pos()
618 (440.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000619
620
621.. function:: towards(x, y=None)
622
623 :param x: a number or a pair/vector of numbers or a turtle instance
624 :param y: a number if *x* is a number, else ``None``
625
626 Return the angle between the line from turtle position to position specified
627 by (x,y), the vector or the other turtle. This depends on the turtle's start
628 orientation which depends on the mode - "standard"/"world" or "logo").
629
R. David Murrayf877feb2009-05-05 02:08:52 +0000630 .. doctest::
631
632 >>> turtle.goto(10, 10)
633 >>> turtle.towards(0,0)
634 225.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000635
636
637.. function:: xcor()
638
639 Return the turtle's x coordinate.
640
R. David Murrayf877feb2009-05-05 02:08:52 +0000641 .. doctest::
642
643 >>> turtle.home()
644 >>> turtle.left(50)
645 >>> turtle.forward(100)
646 >>> turtle.pos()
647 (64.28,76.60)
648 >>> print turtle.xcor()
649 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)
661 >>> print turtle.pos()
662 (50.00,86.60)
663 >>> print turtle.ycor()
664 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()
884 (0.20000000000000001, 0.80000000000000004, 0.5490196078431373)
885 >>> 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)
1144
1145 :param stretch_wid: positive number
1146 :param stretch_len: positive number
1147 :param outline: positive number
1148
1149 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set
1150 resizemode to "user". If and only if resizemode is set to "user", the turtle
1151 will be displayed stretched according to its stretchfactors: *stretch_wid* is
1152 stretchfactor perpendicular to its orientation, *stretch_len* is
1153 stretchfactor in direction of its orientation, *outline* determines the width
1154 of the shapes's outline.
1155
R. David Murrayf877feb2009-05-05 02:08:52 +00001156 .. doctest::
1157
1158 >>> turtle.shapesize()
1159 (1, 1, 1)
1160 >>> turtle.resizemode("user")
1161 >>> turtle.shapesize(5, 5, 12)
1162 >>> turtle.shapesize()
1163 (5, 5, 12)
1164 >>> turtle.shapesize(outline=8)
1165 >>> turtle.shapesize()
1166 (5, 5, 8)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001167
1168
Georg Brandleaa84ef2009-05-05 08:14:33 +00001169.. function:: shearfactor(self, shear=None):
1170
1171 :param shear: number (optional)
1172
1173 Set or return the current shearfactor. Shear the turtleshape according to
1174 the given shearfactor shear, which is the tangent of the shear angle.
1175 Do *not* change the turtle's heading (direction of movement).
1176 If shear is not given: return the current shearfactor, i. e. the
1177 tangent of the shear angle, by which lines parallel to the
1178 heading of the turtle are sheared.
1179
1180 .. doctest::
1181
1182 >>> turtle.shape("circle")
1183 >>> turtle.shapesize(5,2)
1184 >>> turtle.shearfactor(0.5)
1185 >>> turtle.shearfactor()
1186 >>> 0.5
1187
1188
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001189.. function:: tilt(angle)
1190
1191 :param angle: a number
1192
1193 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
1194 change the turtle's heading (direction of movement).
1195
R. David Murrayf877feb2009-05-05 02:08:52 +00001196 .. doctest::
1197
1198 >>> turtle.reset()
1199 >>> turtle.shape("circle")
1200 >>> turtle.shapesize(5,2)
1201 >>> turtle.tilt(30)
1202 >>> turtle.fd(50)
1203 >>> turtle.tilt(30)
1204 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001205
1206
1207.. function:: settiltangle(angle)
1208
1209 :param angle: a number
1210
1211 Rotate the turtleshape to point in the direction specified by *angle*,
1212 regardless of its current tilt-angle. *Do not* change the turtle's heading
1213 (direction of movement).
1214
R. David Murrayf877feb2009-05-05 02:08:52 +00001215 .. doctest::
1216
1217 >>> turtle.reset()
1218 >>> turtle.shape("circle")
1219 >>> turtle.shapesize(5,2)
1220 >>> turtle.settiltangle(45)
1221 >>> turtle.fd(50)
1222 >>> turtle.settiltangle(-45)
1223 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001224
1225
Georg Brandleaa84ef2009-05-05 08:14:33 +00001226.. function:: tiltangle(angle=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001227
Georg Brandleaa84ef2009-05-05 08:14:33 +00001228 :param angle: a number (optional)
1229
1230 Set or return the current tilt-angle. If angle is given, rotate the
1231 turtleshape to point in the direction specified by angle,
1232 regardless of its current tilt-angle. Do *not* change the turtle's
1233 heading (direction of movement).
1234 If angle is not given: return the current tilt-angle, i. e. the angle
1235 between the orientation of the turtleshape and the heading of the
1236 turtle (its direction of movement).
1237
1238 Deprecated since Python 3.1
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001239
R. David Murrayf877feb2009-05-05 02:08:52 +00001240 .. doctest::
1241
1242 >>> turtle.reset()
1243 >>> turtle.shape("circle")
1244 >>> turtle.shapesize(5,2)
1245 >>> turtle.tilt(45)
1246 >>> turtle.tiltangle()
1247 45.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001248
1249
Georg Brandleaa84ef2009-05-05 08:14:33 +00001250.. function:: shapetransform(t11=None, t12=None, t21=None, t22=None)
1251
1252 :param t11: a number (optional)
1253 :param t12: a number (optional)
1254 :param t21: a number (optional)
1255 :param t12: a number (optional)
1256
1257 Set or return the current transformation matrix of the turtle shape.
1258
1259 If none of the matrix elements are given, return the transformation
1260 matrix as a tuple of 4 elements.
1261 Otherwise set the given elements and transform the turtleshape
1262 according to the matrix consisting of first row t11, t12 and
1263 second row t21, 22. The determinant t11 * t22 - t12 * t21 must not be
1264 zero, otherwise an error is raised.
1265 Modify stretchfactor, shearfactor and tiltangle according to the
1266 given matrix.
1267
1268 .. doctest::
1269
1270 >>> turtle.shape("square")
1271 >>> turtle.shapesize(4,2)
1272 >>> turtle.shearfactor(-0.5)
1273 >>> turtle.shapetransform()
1274 >>> (4.0, -1.0, -0.0, 2.0)
1275
1276
1277.. function:: get_shapepoly():
1278
1279 Return the current shape polygon as tuple of coordinate pairs. This
1280 can be used to define a new shape or components of a compound shape.
1281
1282 .. doctest::
1283
1284 >>> turtle.shape("square")
1285 >>> turtle.shapetransform(4, -1, 0, 2)
1286 >>> turtle.get_shapepoly()
1287 ((50, -20), (30, 20), (-50, 20), (-30, -20))
1288
1289
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001290Using events
1291------------
1292
1293.. function:: onclick(fun, btn=1, add=None)
1294
1295 :param fun: a function with two arguments which will be called with the
1296 coordinates of the clicked point on the canvas
1297 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1298 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1299 added, otherwise it will replace a former binding
1300
1301 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1302 existing bindings are removed. Example for the anonymous turtle, i.e. the
1303 procedural way:
1304
R. David Murrayf877feb2009-05-05 02:08:52 +00001305 .. doctest::
1306
1307 >>> def turn(x, y):
1308 ... left(180)
1309 ...
1310 >>> onclick(turn) # Now clicking into the turtle will turn it.
1311 >>> onclick(None) # event-binding will be removed
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001312
1313
1314.. function:: onrelease(fun, btn=1, add=None)
1315
1316 :param fun: a function with two arguments which will be called with the
1317 coordinates of the clicked point on the canvas
1318 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1319 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1320 added, otherwise it will replace a former binding
1321
1322 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1323 ``None``, existing bindings are removed.
1324
R. David Murrayf877feb2009-05-05 02:08:52 +00001325 .. doctest::
1326
1327 >>> class MyTurtle(Turtle):
1328 ... def glow(self,x,y):
1329 ... self.fillcolor("red")
1330 ... def unglow(self,x,y):
1331 ... self.fillcolor("")
1332 ...
1333 >>> turtle = MyTurtle()
1334 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1335 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001336
1337
1338.. function:: ondrag(fun, btn=1, add=None)
1339
1340 :param fun: a function with two arguments which will be called with the
1341 coordinates of the clicked point on the canvas
1342 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1343 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1344 added, otherwise it will replace a former binding
1345
1346 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1347 existing bindings are removed.
1348
1349 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1350 mouse-click event on that turtle.
1351
R. David Murrayf877feb2009-05-05 02:08:52 +00001352 .. doctest::
1353
1354 >>> turtle.ondrag(turtle.goto)
1355
1356 Subsequently, clicking and dragging the Turtle will move it across
1357 the screen thereby producing handdrawings (if pen is down).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001358
1359
1360Special Turtle methods
1361----------------------
1362
1363.. function:: begin_poly()
1364
1365 Start recording the vertices of a polygon. Current turtle position is first
1366 vertex of polygon.
1367
1368
1369.. function:: end_poly()
1370
1371 Stop recording the vertices of a polygon. Current turtle position is last
1372 vertex of polygon. This will be connected with the first vertex.
1373
1374
1375.. function:: get_poly()
1376
1377 Return the last recorded polygon.
1378
R. David Murrayf877feb2009-05-05 02:08:52 +00001379 .. doctest::
1380
1381 >>> turtle.home()
1382 >>> turtle.begin_poly()
1383 >>> turtle.fd(100)
1384 >>> turtle.left(20)
1385 >>> turtle.fd(30)
1386 >>> turtle.left(60)
1387 >>> turtle.fd(50)
1388 >>> turtle.end_poly()
1389 >>> p = turtle.get_poly()
1390 >>> register_shape("myFavouriteShape", p)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001391
1392
1393.. function:: clone()
1394
1395 Create and return a clone of the turtle with same position, heading and
1396 turtle properties.
1397
R. David Murrayf877feb2009-05-05 02:08:52 +00001398 .. doctest::
1399
1400 >>> mick = Turtle()
1401 >>> joe = mick.clone()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001402
1403
1404.. function:: getturtle()
1405
1406 Return the Turtle object itself. Only reasonable use: as a function to
1407 return the "anonymous turtle":
1408
R. David Murrayf877feb2009-05-05 02:08:52 +00001409 .. doctest::
1410
1411 >>> pet = getturtle()
1412 >>> pet.fd(50)
1413 >>> pet
1414 <turtle.Turtle object at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001415
1416
1417.. function:: getscreen()
1418
1419 Return the :class:`TurtleScreen` object the turtle is drawing on.
1420 TurtleScreen methods can then be called for that object.
1421
R. David Murrayf877feb2009-05-05 02:08:52 +00001422 .. doctest::
1423
1424 >>> ts = turtle.getscreen()
1425 >>> ts
1426 <turtle._Screen object at 0x...>
1427 >>> ts.bgcolor("pink")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001428
1429
1430.. function:: setundobuffer(size)
1431
1432 :param size: an integer or ``None``
1433
1434 Set or disable undobuffer. If *size* is an integer an empty undobuffer of
1435 given size is installed. *size* gives the maximum number of turtle actions
1436 that can be undone by the :func:`undo` method/function. If *size* is
1437 ``None``, the undobuffer is disabled.
1438
R. David Murrayf877feb2009-05-05 02:08:52 +00001439 .. doctest::
1440
1441 >>> turtle.setundobuffer(42)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001442
1443
1444.. function:: undobufferentries()
1445
1446 Return number of entries in the undobuffer.
1447
R. David Murrayf877feb2009-05-05 02:08:52 +00001448 .. doctest::
1449
1450 >>> while undobufferentries():
1451 ... undo()
1452
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001453
1454
1455.. _compoundshapes:
1456
1457Excursus about the use of compound shapes
1458-----------------------------------------
1459
1460To use compound turtle shapes, which consist of several polygons of different
1461color, you must use the helper class :class:`Shape` explicitly as described
1462below:
1463
14641. Create an empty Shape object of type "compound".
14652. Add as many components to this object as desired, using the
1466 :meth:`addcomponent` method.
1467
1468 For example:
1469
R. David Murrayf877feb2009-05-05 02:08:52 +00001470 .. doctest::
1471
1472 >>> s = Shape("compound")
1473 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1474 >>> s.addcomponent(poly1, "red", "blue")
1475 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1476 >>> s.addcomponent(poly2, "blue", "red")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001477
14783. Now add the Shape to the Screen's shapelist and use it:
1479
R. David Murrayf877feb2009-05-05 02:08:52 +00001480 .. doctest::
1481
1482 >>> register_shape("myshape", s)
1483 >>> shape("myshape")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001484
1485
1486.. note::
1487
1488 The :class:`Shape` class is used internally by the :func:`register_shape`
1489 method in different ways. The application programmer has to deal with the
1490 Shape class *only* when using compound shapes like shown above!
1491
1492
1493Methods of TurtleScreen/Screen and corresponding functions
1494==========================================================
1495
1496Most of the examples in this section refer to a TurtleScreen instance called
1497``screen``.
1498
R. David Murrayf877feb2009-05-05 02:08:52 +00001499.. doctest::
1500 :hide:
1501
1502 >>> screen = Screen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001503
1504Window control
1505--------------
1506
1507.. function:: bgcolor(*args)
1508
1509 :param args: a color string or three numbers in the range 0..colormode or a
1510 3-tuple of such numbers
1511
1512 Set or return background color of the TurtleScreen.
1513
R. David Murrayf877feb2009-05-05 02:08:52 +00001514 .. doctest::
1515
1516 >>> screen.bgcolor("orange")
1517 >>> screen.bgcolor()
1518 'orange'
1519 >>> screen.bgcolor("#800080")
1520 >>> screen.bgcolor()
1521 (128, 0, 128)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001522
1523
1524.. function:: bgpic(picname=None)
1525
1526 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
1527
1528 Set background image or return name of current backgroundimage. If *picname*
1529 is a filename, set the corresponding image as background. If *picname* is
1530 ``"nopic"``, delete background image, if present. If *picname* is ``None``,
R. David Murrayf877feb2009-05-05 02:08:52 +00001531 return the filename of the current backgroundimage. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001532
R. David Murrayf877feb2009-05-05 02:08:52 +00001533 >>> screen.bgpic()
1534 'nopic'
1535 >>> screen.bgpic("landscape.gif")
1536 >>> screen.bgpic()
1537 "landscape.gif"
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001538
1539
1540.. function:: clear()
1541 clearscreen()
1542
1543 Delete all drawings and all turtles from the TurtleScreen. Reset the now
1544 empty TurtleScreen to its initial state: white background, no background
1545 image, no event bindings and tracing on.
1546
1547 .. note::
1548 This TurtleScreen method is available as a global function only under the
1549 name ``clearscreen``. The global function ``clear`` is another one
1550 derived from the Turtle method ``clear``.
1551
1552
1553.. function:: reset()
1554 resetscreen()
1555
1556 Reset all Turtles on the Screen to their initial state.
1557
1558 .. note::
1559 This TurtleScreen method is available as a global function only under the
1560 name ``resetscreen``. The global function ``reset`` is another one
1561 derived from the Turtle method ``reset``.
1562
1563
1564.. function:: screensize(canvwidth=None, canvheight=None, bg=None)
1565
Georg Brandlff2ad0e2009-04-27 16:51:45 +00001566 :param canvwidth: positive integer, new width of canvas in pixels
1567 :param canvheight: positive integer, new height of canvas in pixels
1568 :param bg: colorstring or color-tuple, new background color
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001569
1570 If no arguments are given, return current (canvaswidth, canvasheight). Else
1571 resize the canvas the turtles are drawing on. Do not alter the drawing
1572 window. To observe hidden parts of the canvas, use the scrollbars. With this
1573 method, one can make visible those parts of a drawing which were outside the
1574 canvas before.
1575
R. David Murrayf877feb2009-05-05 02:08:52 +00001576 >>> screen.screensize()
1577 (400, 300)
1578 >>> screen.screensize(2000,1500)
1579 >>> screen.screensize()
1580 (2000, 1500)
1581
1582 e.g. to search for an erroneously escaped turtle ;-)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001583
1584
1585.. function:: setworldcoordinates(llx, lly, urx, ury)
1586
1587 :param llx: a number, x-coordinate of lower left corner of canvas
1588 :param lly: a number, y-coordinate of lower left corner of canvas
1589 :param urx: a number, x-coordinate of upper right corner of canvas
1590 :param ury: a number, y-coordinate of upper right corner of canvas
1591
1592 Set up user-defined coordinate system and switch to mode "world" if
1593 necessary. This performs a ``screen.reset()``. If mode "world" is already
1594 active, all drawings are redrawn according to the new coordinates.
1595
1596 **ATTENTION**: in user-defined coordinate systems angles may appear
1597 distorted.
1598
R. David Murrayf877feb2009-05-05 02:08:52 +00001599 .. doctest::
1600
1601 >>> screen.reset()
1602 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1603 >>> for _ in range(72):
1604 ... left(10)
1605 ...
1606 >>> for _ in range(8):
1607 ... left(45); fd(2) # a regular octagon
1608
1609 .. doctest::
1610 :hide:
1611
1612 >>> screen.reset()
1613 >>> for t in turtles():
1614 ... t.reset()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001615
1616
1617Animation control
1618-----------------
1619
1620.. function:: delay(delay=None)
1621
1622 :param delay: positive integer
1623
1624 Set or return the drawing *delay* in milliseconds. (This is approximately
Georg Brandl2ee470f2008-07-16 12:55:28 +00001625 the time interval between two consecutive canvas updates.) The longer the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001626 drawing delay, the slower the animation.
1627
1628 Optional argument:
1629
R. David Murrayf877feb2009-05-05 02:08:52 +00001630 .. doctest::
1631
1632 >>> screen.delay()
1633 10
1634 >>> screen.delay(5)
1635 >>> screen.delay()
1636 5
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001637
1638
1639.. function:: tracer(n=None, delay=None)
1640
1641 :param n: nonnegative integer
1642 :param delay: nonnegative integer
1643
1644 Turn turtle animation on/off and set delay for update drawings. If *n* is
1645 given, only each n-th regular screen update is really performed. (Can be
1646 used to accelerate the drawing of complex graphics.) Second argument sets
1647 delay value (see :func:`delay`).
1648
R. David Murrayf877feb2009-05-05 02:08:52 +00001649 .. doctest::
1650
1651 >>> screen.tracer(8, 25)
1652 >>> dist = 2
1653 >>> for i in range(200):
1654 ... fd(dist)
1655 ... rt(90)
1656 ... dist += 2
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001657
1658
1659.. function:: update()
1660
1661 Perform a TurtleScreen update. To be used when tracer is turned off.
1662
1663See also the RawTurtle/Turtle method :func:`speed`.
1664
1665
1666Using screen events
1667-------------------
1668
1669.. function:: listen(xdummy=None, ydummy=None)
1670
1671 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1672 are provided in order to be able to pass :func:`listen` to the onclick method.
1673
1674
1675.. function:: onkey(fun, key)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001676 onkeyrelease(fun, key)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001677
1678 :param fun: a function with no arguments or ``None``
1679 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1680
1681 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1682 are removed. Remark: in order to be able to register key-events, TurtleScreen
1683 must have the focus. (See method :func:`listen`.)
1684
R. David Murrayf877feb2009-05-05 02:08:52 +00001685 .. doctest::
1686
1687 >>> def f():
1688 ... fd(50)
1689 ... lt(60)
1690 ...
1691 >>> screen.onkey(f, "Up")
1692 >>> screen.listen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001693
1694
Georg Brandleaa84ef2009-05-05 08:14:33 +00001695.. function:: onkeypress(fun, key=None):
1696
1697 :param fun: a function with no arguments or ``None``
1698 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1699
1700 Bind *fun* to key-press event of key if key is given,
1701 or to any key-press-event if no key is given.
1702 Remark: in order to be able to register key-events, TurtleScreen
1703 must have focus. (See method :func:`listen`.)
1704
1705 .. doctest::
1706
1707 >>> def f():
1708 ... fd(50)
1709 ...
1710 >>> screen.onkey(f, "Up")
1711 >>> screen.listen()
1712
1713
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001714.. function:: onclick(fun, btn=1, add=None)
1715 onscreenclick(fun, btn=1, add=None)
1716
1717 :param fun: a function with two arguments which will be called with the
1718 coordinates of the clicked point on the canvas
1719 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1720 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1721 added, otherwise it will replace a former binding
1722
1723 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1724 existing bindings are removed.
1725
1726 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1727 named turtle:
1728
R. David Murrayf877feb2009-05-05 02:08:52 +00001729 .. doctest::
1730
1731 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1732 >>> # make the turtle move to the clicked point.
1733 >>> screen.onclick(None) # remove event binding again
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001734
1735 .. note::
1736 This TurtleScreen method is available as a global function only under the
1737 name ``onscreenclick``. The global function ``onclick`` is another one
1738 derived from the Turtle method ``onclick``.
1739
1740
1741.. function:: ontimer(fun, t=0)
1742
1743 :param fun: a function with no arguments
1744 :param t: a number >= 0
1745
1746 Install a timer that calls *fun* after *t* milliseconds.
1747
R. David Murrayf877feb2009-05-05 02:08:52 +00001748 .. doctest::
1749
1750 >>> running = True
1751 >>> def f():
1752 ... if running:
1753 ... fd(50)
1754 ... lt(60)
1755 ... screen.ontimer(f, 250)
1756 >>> f() ### makes the turtle march around
1757 >>> running = False
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001758
1759
Georg Brandleaa84ef2009-05-05 08:14:33 +00001760.. function:: mainloop()
1761
1762 Starts event loop - calling Tkinter's mainloop function.
1763 Must be the last statement in a turtle graphics program.
1764 Must *not* be used if a script is run from within IDLE in -n mode
1765 (No subprocess) - for interactive use of turtle graphics. ::
1766
1767 >>> screen.mainloop()
1768
1769
1770Input methods
1771-------------
1772
1773.. function:: textinput(title, prompt)
1774
1775 :param title: string
1776 :param prompt: string
1777
1778 Pop up a dialog window for input of a string. Parameter title is
1779 the title of the dialog window, propmt is a text mostly describing
1780 what information to input.
1781 Return the string input. If the dialog is canceled, return None. ::
1782
1783 >>> screen.textinput("NIM", "Name of first player:")
1784
1785
1786.. function:: numinput(self, title, prompt,
1787 default=None, minval=None, maxval=None):
1788
1789 :param title: string
1790 :param prompt: string
1791 :param default: number (optional)
1792 :param prompt: number (optional)
1793 :param prompt: number (optional)
1794
1795 Pop up a dialog window for input of a number. title is the title of the
1796 dialog window, prompt is a text mostly describing what numerical information
1797 to input. default: default value, minval: minimum value for imput,
1798 maxval: maximum value for input
1799 The number input must be in the range minval .. maxval if these are
1800 given. If not, a hint is issued and the dialog remains open for
1801 correction.
1802 Return the number input. If the dialog is canceled, return None. ::
1803
1804 >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000)
1805
1806
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001807Settings and special methods
1808----------------------------
1809
1810.. function:: mode(mode=None)
1811
1812 :param mode: one of the strings "standard", "logo" or "world"
1813
1814 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1815 is not given, current mode is returned.
1816
1817 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1818 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1819 "world coordinates". **Attention**: in this mode angles appear distorted if
1820 ``x/y`` unit-ratio doesn't equal 1.
1821
1822 ============ ========================= ===================
1823 Mode Initial turtle heading positive angles
1824 ============ ========================= ===================
1825 "standard" to the right (east) counterclockwise
1826 "logo" upward (north) clockwise
1827 ============ ========================= ===================
1828
R. David Murrayf877feb2009-05-05 02:08:52 +00001829 .. doctest::
1830
1831 >>> mode("logo") # resets turtle heading to north
1832 >>> mode()
1833 'logo'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001834
1835
1836.. function:: colormode(cmode=None)
1837
1838 :param cmode: one of the values 1.0 or 255
1839
1840 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1841 values of color triples have to be in the range 0..\ *cmode*.
1842
R. David Murrayf877feb2009-05-05 02:08:52 +00001843 .. doctest::
1844
1845 >>> screen.colormode(1)
1846 >>> turtle.pencolor(240, 160, 80)
1847 Traceback (most recent call last):
1848 ...
1849 TurtleGraphicsError: bad color sequence: (240, 160, 80)
1850 >>> screen.colormode()
1851 1.0
1852 >>> screen.colormode(255)
1853 >>> screen.colormode()
1854 255
1855 >>> turtle.pencolor(240,160,80)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001856
1857
1858.. function:: getcanvas()
1859
1860 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1861 do with a Tkinter Canvas.
1862
R. David Murrayf877feb2009-05-05 02:08:52 +00001863 .. doctest::
1864
1865 >>> cv = screen.getcanvas()
1866 >>> cv
1867 <turtle.ScrolledCanvas instance at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001868
1869
1870.. function:: getshapes()
1871
1872 Return a list of names of all currently available turtle shapes.
1873
R. David Murrayf877feb2009-05-05 02:08:52 +00001874 .. doctest::
1875
1876 >>> screen.getshapes()
1877 ['arrow', 'blank', 'circle', ..., 'turtle']
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001878
1879
1880.. function:: register_shape(name, shape=None)
1881 addshape(name, shape=None)
1882
1883 There are three different ways to call this function:
1884
1885 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
R. David Murrayf877feb2009-05-05 02:08:52 +00001886 corresponding image shape. ::
1887
1888 >>> screen.register_shape("turtle.gif")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001889
1890 .. note::
1891 Image shapes *do not* rotate when turning the turtle, so they do not
1892 display the heading of the turtle!
1893
1894 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1895 coordinates: Install the corresponding polygon shape.
1896
R. David Murrayf877feb2009-05-05 02:08:52 +00001897 .. doctest::
1898
1899 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
1900
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001901 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
1902 object: Install the corresponding compound shape.
1903
1904 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1905 shapes can be used by issuing the command ``shape(shapename)``.
1906
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001907
1908.. function:: turtles()
1909
1910 Return the list of turtles on the screen.
1911
R. David Murrayf877feb2009-05-05 02:08:52 +00001912 .. doctest::
1913
1914 >>> for turtle in screen.turtles():
1915 ... turtle.color("red")
Georg Brandl116aa622007-08-15 14:28:22 +00001916
Georg Brandl116aa622007-08-15 14:28:22 +00001917
1918.. function:: window_height()
1919
R. David Murrayf877feb2009-05-05 02:08:52 +00001920 Return the height of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001921
R. David Murrayf877feb2009-05-05 02:08:52 +00001922 >>> screen.window_height()
1923 480
Georg Brandl116aa622007-08-15 14:28:22 +00001924
Georg Brandl116aa622007-08-15 14:28:22 +00001925
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001926.. function:: window_width()
1927
R. David Murrayf877feb2009-05-05 02:08:52 +00001928 Return the width of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001929
R. David Murrayf877feb2009-05-05 02:08:52 +00001930 >>> screen.window_width()
1931 640
Georg Brandl116aa622007-08-15 14:28:22 +00001932
1933
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001934.. _screenspecific:
Georg Brandl116aa622007-08-15 14:28:22 +00001935
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001936Methods specific to Screen, not inherited from TurtleScreen
1937-----------------------------------------------------------
1938
1939.. function:: bye()
1940
1941 Shut the turtlegraphics window.
Georg Brandl116aa622007-08-15 14:28:22 +00001942
1943
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001944.. function:: exitonclick()
Georg Brandl116aa622007-08-15 14:28:22 +00001945
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001946 Bind bye() method to mouse clicks on the Screen.
Georg Brandl116aa622007-08-15 14:28:22 +00001947
1948
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001949 If the value "using_IDLE" in the configuration dictionary is ``False``
1950 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
1951 (no subprocess) is used, this value should be set to ``True`` in
1952 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1953 client script.
Georg Brandl116aa622007-08-15 14:28:22 +00001954
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001955
1956.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
1957
1958 Set the size and position of the main window. Default values of arguments
1959 are stored in the configuration dicionary and can be changed via a
1960 :file:`turtle.cfg` file.
1961
1962 :param width: if an integer, a size in pixels, if a float, a fraction of the
1963 screen; default is 50% of screen
1964 :param height: if an integer, the height in pixels, if a float, a fraction of
1965 the screen; default is 75% of screen
1966 :param startx: if positive, starting position in pixels from the left
1967 edge of the screen, if negative from the right edge, if None,
1968 center window horizontally
1969 :param startx: if positive, starting position in pixels from the top
1970 edge of the screen, if negative from the bottom edge, if None,
1971 center window vertically
1972
R. David Murrayf877feb2009-05-05 02:08:52 +00001973 .. doctest::
1974
1975 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1976 >>> # sets window to 200x200 pixels, in upper left of screen
1977 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1978 >>> # sets window to 75% of screen by 50% of screen and centers
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001979
1980
1981.. function:: title(titlestring)
1982
1983 :param titlestring: a string that is shown in the titlebar of the turtle
1984 graphics window
1985
1986 Set title of turtle window to *titlestring*.
1987
R. David Murrayf877feb2009-05-05 02:08:52 +00001988 .. doctest::
1989
1990 >>> screen.title("Welcome to the turtle zoo!")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001991
1992
1993The public classes of the module :mod:`turtle`
1994==============================================
1995
1996
1997.. class:: RawTurtle(canvas)
1998 RawPen(canvas)
1999
2000 :param canvas: a :class:`Tkinter.Canvas`, a :class:`ScrolledCanvas` or a
2001 :class:`TurtleScreen`
2002
R. David Murrayf877feb2009-05-05 02:08:52 +00002003 Create a turtle. The turtle has all methods described above as "methods of
2004 Turtle/RawTurtle".
Georg Brandl116aa622007-08-15 14:28:22 +00002005
2006
2007.. class:: Turtle()
2008
R. David Murrayf877feb2009-05-05 02:08:52 +00002009 Subclass of RawTurtle, has the same interface but draws on a default
2010 :class:`Screen` object created automatically when needed for the first time.
Georg Brandl116aa622007-08-15 14:28:22 +00002011
2012
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002013.. class:: TurtleScreen(cv)
Georg Brandl116aa622007-08-15 14:28:22 +00002014
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002015 :param cv: a :class:`Tkinter.Canvas`
2016
2017 Provides screen oriented methods like :func:`setbg` etc. that are described
2018 above.
2019
2020.. class:: Screen()
2021
2022 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
2023
Georg Brandl48310cd2009-01-03 21:18:54 +00002024
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002025.. class:: ScrolledCavas(master)
2026
2027 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
2028 a Tkinter-canvas with scrollbars added
2029
2030 Used by class Screen, which thus automatically provides a ScrolledCanvas as
2031 playground for the turtles.
2032
2033.. class:: Shape(type_, data)
2034
2035 :param type\_: one of the strings "polygon", "image", "compound"
2036
2037 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
2038 specification:
Georg Brandl116aa622007-08-15 14:28:22 +00002039
2040
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002041 =========== ===========
2042 *type_* *data*
2043 =========== ===========
2044 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
2045 "image" an image (in this form only used internally!)
Georg Brandlae2dbe22009-03-13 19:04:40 +00002046 "compound" ``None`` (a compound shape has to be constructed using the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002047 :meth:`addcomponent` method)
2048 =========== ===========
Georg Brandl48310cd2009-01-03 21:18:54 +00002049
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002050 .. method:: addcomponent(poly, fill, outline=None)
Georg Brandl116aa622007-08-15 14:28:22 +00002051
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002052 :param poly: a polygon, i.e. a tuple of pairs of numbers
2053 :param fill: a color the *poly* will be filled with
2054 :param outline: a color for the poly's outline (if given)
Georg Brandl48310cd2009-01-03 21:18:54 +00002055
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002056 Example:
Georg Brandl116aa622007-08-15 14:28:22 +00002057
R. David Murrayf877feb2009-05-05 02:08:52 +00002058 .. doctest::
2059
2060 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
2061 >>> s = Shape("compound")
2062 >>> s.addcomponent(poly, "red", "blue")
2063 >>> # ... add more components and then use register_shape()
Georg Brandl116aa622007-08-15 14:28:22 +00002064
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002065 See :ref:`compoundshapes`.
Georg Brandl116aa622007-08-15 14:28:22 +00002066
2067
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002068.. class:: Vec2D(x, y)
Georg Brandl116aa622007-08-15 14:28:22 +00002069
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002070 A two-dimensional vector class, used as a helper class for implementing
2071 turtle graphics. May be useful for turtle graphics programs too. Derived
2072 from tuple, so a vector is a tuple!
2073
2074 Provides (for *a*, *b* vectors, *k* number):
2075
2076 * ``a + b`` vector addition
2077 * ``a - b`` vector subtraction
2078 * ``a * b`` inner product
2079 * ``k * a`` and ``a * k`` multiplication with scalar
2080 * ``abs(a)`` absolute value of a
2081 * ``a.rotate(angle)`` rotation
2082
2083
2084Help and configuration
2085======================
2086
2087How to use help
2088---------------
2089
2090The public methods of the Screen and Turtle classes are documented extensively
2091via docstrings. So these can be used as online-help via the Python help
2092facilities:
2093
2094- When using IDLE, tooltips show the signatures and first lines of the
2095 docstrings of typed in function-/method calls.
2096
2097- Calling :func:`help` on methods or functions displays the docstrings::
2098
2099 >>> help(Screen.bgcolor)
2100 Help on method bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002101
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002102 bgcolor(self, *args) unbound turtle.Screen method
2103 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002104
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002105 Arguments (if given): a color string or three numbers
2106 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002107
2108
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002109 >>> screen.bgcolor("orange")
2110 >>> screen.bgcolor()
2111 "orange"
2112 >>> screen.bgcolor(0.5,0,0.5)
2113 >>> screen.bgcolor()
2114 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002115
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002116 >>> help(Turtle.penup)
2117 Help on method penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002118
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002119 penup(self) unbound turtle.Turtle method
2120 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002121
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002122 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002123
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002124 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002125
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002126 >>> turtle.penup()
2127
2128- The docstrings of the functions which are derived from methods have a modified
2129 form::
2130
2131 >>> help(bgcolor)
2132 Help on function bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002133
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002134 bgcolor(*args)
2135 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002136
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002137 Arguments (if given): a color string or three numbers
2138 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002139
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002140 Example::
Georg Brandl48310cd2009-01-03 21:18:54 +00002141
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002142 >>> bgcolor("orange")
2143 >>> bgcolor()
2144 "orange"
2145 >>> bgcolor(0.5,0,0.5)
2146 >>> bgcolor()
2147 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002148
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002149 >>> help(penup)
2150 Help on function penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002151
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002152 penup()
2153 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002154
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002155 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002156
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002157 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002158
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002159 Example:
2160 >>> penup()
2161
2162These modified docstrings are created automatically together with the function
2163definitions that are derived from the methods at import time.
2164
2165
2166Translation of docstrings into different languages
2167--------------------------------------------------
2168
2169There is a utility to create a dictionary the keys of which are the method names
2170and the values of which are the docstrings of the public methods of the classes
2171Screen and Turtle.
2172
2173.. function:: write_docstringdict(filename="turtle_docstringdict")
2174
2175 :param filename: a string, used as filename
2176
2177 Create and write docstring-dictionary to a Python script with the given
2178 filename. This function has to be called explicitly (it is not used by the
2179 turtle graphics classes). The docstring dictionary will be written to the
2180 Python script :file:`{filename}.py`. It is intended to serve as a template
2181 for translation of the docstrings into different languages.
2182
2183If you (or your students) want to use :mod:`turtle` with online help in your
2184native language, you have to translate the docstrings and save the resulting
2185file as e.g. :file:`turtle_docstringdict_german.py`.
2186
2187If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2188will be read in at import time and will replace the original English docstrings.
2189
2190At the time of this writing there are docstring dictionaries in German and in
2191Italian. (Requests please to glingl@aon.at.)
2192
2193
2194
2195How to configure Screen and Turtles
2196-----------------------------------
2197
2198The built-in default configuration mimics the appearance and behaviour of the
2199old turtle module in order to retain best possible compatibility with it.
2200
2201If you want to use a different configuration which better reflects the features
2202of this module or which better fits to your needs, e.g. for use in a classroom,
2203you can prepare a configuration file ``turtle.cfg`` which will be read at import
2204time and modify the configuration according to its settings.
2205
2206The built in configuration would correspond to the following turtle.cfg::
2207
2208 width = 0.5
2209 height = 0.75
2210 leftright = None
2211 topbottom = None
2212 canvwidth = 400
2213 canvheight = 300
2214 mode = standard
2215 colormode = 1.0
2216 delay = 10
2217 undobuffersize = 1000
2218 shape = classic
2219 pencolor = black
2220 fillcolor = black
2221 resizemode = noresize
2222 visible = True
2223 language = english
2224 exampleturtle = turtle
2225 examplescreen = screen
2226 title = Python Turtle Graphics
2227 using_IDLE = False
2228
2229Short explanation of selected entries:
2230
2231- The first four lines correspond to the arguments of the :meth:`Screen.setup`
2232 method.
2233- Line 5 and 6 correspond to the arguments of the method
2234 :meth:`Screen.screensize`.
2235- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2236 info try ``help(shape)``.
2237- If you want to use no fillcolor (i.e. make the turtle transparent), you have
2238 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
2239 the cfg-file).
2240- If you want to reflect the turtle its state, you have to use ``resizemode =
2241 auto``.
2242- If you set e.g. ``language = italian`` the docstringdict
2243 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
2244 present on the import path, e.g. in the same directory as :mod:`turtle`.
2245- The entries *exampleturtle* and *examplescreen* define the names of these
2246 objects as they occur in the docstrings. The transformation of
2247 method-docstrings to function-docstrings will delete these names from the
2248 docstrings.
2249- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
2250 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
2251 mainloop.
2252
2253There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2254stored and an additional one in the current working directory. The latter will
2255override the settings of the first one.
2256
2257The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can
2258study it as an example and see its effects when running the demos (preferably
2259not from within the demo-viewer).
2260
2261
2262Demo scripts
2263============
2264
2265There is a set of demo scripts in the turtledemo directory located in the
2266:file:`Demo/turtle` directory in the source distribution.
2267
2268It contains:
2269
Georg Brandlae2dbe22009-03-13 19:04:40 +00002270- a set of 15 demo scripts demonstrating different features of the new module
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002271 :mod:`turtle`
2272- a demo viewer :file:`turtleDemo.py` which can be used to view the sourcecode
2273 of the scripts and run them at the same time. 14 of the examples can be
2274 accessed via the Examples menu; all of them can also be run standalone.
2275- The example :file:`turtledemo_two_canvases.py` demonstrates the simultaneous
2276 use of two canvases with the turtle module. Therefore it only can be run
2277 standalone.
2278- There is a :file:`turtle.cfg` file in this directory, which also serves as an
2279 example for how to write and use such files.
2280
2281The demoscripts are:
2282
2283+----------------+------------------------------+-----------------------+
2284| Name | Description | Features |
2285+----------------+------------------------------+-----------------------+
2286| bytedesign | complex classical | :func:`tracer`, delay,|
2287| | turtlegraphics pattern | :func:`update` |
2288+----------------+------------------------------+-----------------------+
2289| chaos | graphs verhust dynamics, | world coordinates |
2290| | proves that you must not | |
2291| | trust computers' computations| |
2292+----------------+------------------------------+-----------------------+
2293| clock | analog clock showing time | turtles as clock's |
2294| | of your computer | hands, ontimer |
2295+----------------+------------------------------+-----------------------+
2296| colormixer | experiment with r, g, b | :func:`ondrag` |
2297+----------------+------------------------------+-----------------------+
2298| fractalcurves | Hilbert & Koch curves | recursion |
2299+----------------+------------------------------+-----------------------+
2300| lindenmayer | ethnomathematics | L-System |
2301| | (indian kolams) | |
2302+----------------+------------------------------+-----------------------+
2303| minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
2304| | | as Hanoi discs |
2305| | | (shape, shapesize) |
2306+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002307| nim | play the classical nim game | turtles as nimsticks, |
2308| | with three heaps of sticks | event driven (mouse, |
2309| | against the computer. | keyboard) |
2310+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002311| paint | super minimalistic | :func:`onclick` |
2312| | drawing program | |
2313+----------------+------------------------------+-----------------------+
2314| peace | elementary | turtle: appearance |
2315| | | and animation |
2316+----------------+------------------------------+-----------------------+
2317| penrose | aperiodic tiling with | :func:`stamp` |
2318| | kites and darts | |
2319+----------------+------------------------------+-----------------------+
2320| planet_and_moon| simulation of | compound shapes, |
2321| | gravitational system | :class:`Vec2D` |
2322+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002323| round_dance | dancing turtles rotating | compound shapes, clone|
2324| | pairwise in opposite | shapesize, tilt, |
2325| | direction | get_polyshape, update |
2326+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002327| tree | a (graphical) breadth | :func:`clone` |
2328| | first tree (using generators)| |
2329+----------------+------------------------------+-----------------------+
2330| wikipedia | a pattern from the wikipedia | :func:`clone`, |
2331| | article on turtle graphics | :func:`undo` |
2332+----------------+------------------------------+-----------------------+
2333| yingyang | another elementary example | :func:`circle` |
2334+----------------+------------------------------+-----------------------+
2335
2336Have fun!
2337
2338
2339Changes since Python 2.6
2340========================
2341
Georg Brandl48310cd2009-01-03 21:18:54 +00002342- The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and
2343 :meth:`Turtle.window_height` have been eliminated.
2344 Methods with these names and functionality are now available only
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002345 as methods of :class:`Screen`. The functions derived from these remain
Georg Brandl48310cd2009-01-03 21:18:54 +00002346 available. (In fact already in Python 2.6 these methods were merely
2347 duplications of the corresponding
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002348 :class:`TurtleScreen`/:class:`Screen`-methods.)
2349
Georg Brandl48310cd2009-01-03 21:18:54 +00002350- The method :meth:`Turtle.fill` has been eliminated.
2351 The behaviour of :meth:`begin_fill` and :meth:`end_fill`
2352 have changed slightly: now every filling-process must be completed with an
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002353 ``end_fill()`` call.
Georg Brandl48310cd2009-01-03 21:18:54 +00002354
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002355- A method :meth:`Turtle.filling` has been added. It returns a boolean
2356 value: ``True`` if a filling process is under way, ``False`` otherwise.
2357 This behaviour corresponds to a ``fill()`` call without arguments in
Georg Brandl23d11d32008-09-21 07:50:52 +00002358 Python 2.6.
Georg Brandl116aa622007-08-15 14:28:22 +00002359
Georg Brandleaa84ef2009-05-05 08:14:33 +00002360Changes since Python 3.0
2361========================
2362
2363- The methods :meth:`Turtle.shearfactor`, :meth:`Turtle.shapetransform` and
2364 :meth:`Turtle.get_shapepoly` have been added. Thus the full range of
2365 regular linear transforms is now available for transforming turtle shapes.
2366 :meth:`Turtle.tiltangle` has been enhanced in functionality: it now can
2367 be used to get or set the tiltangle. :meth:`Turtle.settiltangle` has been
2368 deprecated.
2369
2370- The method :meth:`Screen.onkeypress` has been added as a complement to
2371 :meth:`Screen.onkey` which in fact binds actions to the keyrelease event.
2372 Accordingly the latter has got an alias: :meth:`Screen.onkeyrelease`.
2373
2374- The method :meth:`Screen.mainloop` has been added. So when working only
2375 with Screen and Turtle objects one must not additonally import
2376 :func:`mainloop` anymore.
2377
2378- Two input methods has been added :meth:`Screen.textinput` and
2379 :meth:`Screen.numinput`. These popup input dialogs and return
2380 strings and numbers respectively.
2381
2382- Two example scripts :file:`tdemo_nim.py` and :file:`tdemo_round_dance.py`
2383 have been added to the Demo directory (source distribution only). As usual
2384 they can be viewed and executed within the demo viewer :file:`turtleDemo.py`.
2385
R. David Murrayf877feb2009-05-05 02:08:52 +00002386
2387.. doctest::
2388 :hide:
2389
2390 >>> for turtle in turtles():
2391 ... turtle.reset()
2392 >>> turtle.penup()
2393 >>> turtle.goto(-200,25)
2394 >>> turtle.pendown()
2395 >>> turtle.write("No one expects the Spanish Inquisition!",
2396 ... font=("Arial", 20, "normal"))
2397 >>> turtle.penup()
2398 >>> turtle.goto(-100,-50)
2399 >>> turtle.pendown()
2400 >>> turtle.write("Our two chief Turtles are...",
2401 ... font=("Arial", 16, "normal"))
2402 >>> turtle.penup()
2403 >>> turtle.goto(-450,-75)
2404 >>> turtle.write(str(turtles()))