blob: d3487537df99a9546eeaf39fde6b382519ff9985 [file] [log] [blame]
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00001=================================
2:mod:`turtle` --- Turtle graphics
3=================================
Georg Brandl116aa622007-08-15 14:28:22 +00004
Georg Brandl23d11d32008-09-21 07:50:52 +00005.. module:: turtle
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00006 :synopsis: An educational framework for simple graphics applications
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Georg Brandl2ee470f2008-07-16 12:55:28 +00008.. sectionauthor:: Gregor Lingl <gregor.lingl@aon.at>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/turtle.py`
11
R. David Murrayf877feb2009-05-05 02:08:52 +000012.. testsetup:: default
13
14 from turtle import *
15 turtle = Turtle()
16
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040017--------------
18
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000019Introduction
20============
21
22Turtle graphics is a popular way for introducing programming to kids. It was
Stéphane Wirtel66501052019-06-01 13:41:33 +020023part of the original Logo programming language developed by Wally Feurzeig,
24Seymour Papert and Cynthia Solomon in 1967.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000025
Sandro Tosi2a389e42011-08-07 17:12:19 +020026Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000027command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
28direction it is facing, drawing a line as it moves. Give it the command
Sandro Tosi2a389e42011-08-07 17:12:19 +020029``turtle.right(25)``, and it rotates in-place 25 degrees clockwise.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000030
Alexander Belopolsky14fb7992010-11-09 18:40:03 +000031.. sidebar:: Turtle star
32
33 Turtle can draw intricate shapes using programs that repeat simple
34 moves.
35
36 .. image:: turtle-star.*
37 :align: center
38
39 .. literalinclude:: ../includes/turtle-star.py
40
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000041By combining together these and similar commands, intricate shapes and pictures
42can easily be drawn.
43
44The :mod:`turtle` module is an extended reimplementation of the same-named
45module from the Python standard distribution up to version Python 2.5.
46
47It tries to keep the merits of the old turtle module and to be (nearly) 100%
48compatible with it. This means in the first place to enable the learning
49programmer to use all the commands, classes and methods interactively when using
50the module from within IDLE run with the ``-n`` switch.
51
52The turtle module provides turtle graphics primitives, in both object-oriented
Ezio Melotti1a263ad2010-03-14 09:51:37 +000053and procedure-oriented ways. Because it uses :mod:`tkinter` for the underlying
Ezio Melotti0639d5a2009-12-19 23:26:38 +000054graphics, it needs a version of Python installed with Tk support.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000055
56The object-oriented interface uses essentially two+two classes:
57
581. The :class:`TurtleScreen` class defines graphics windows as a playground for
Ezio Melotti1a263ad2010-03-14 09:51:37 +000059 the drawing turtles. Its constructor needs a :class:`tkinter.Canvas` or a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000060 :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is
61 used as part of some application.
62
Martin v. Löwis601149b2008-09-29 22:19:08 +000063 The function :func:`Screen` returns a singleton object of a
64 :class:`TurtleScreen` subclass. This function should be used when
65 :mod:`turtle` is used as a standalone tool for doing graphics.
66 As a singleton object, inheriting from its class is not possible.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000067
68 All methods of TurtleScreen/Screen also exist as functions, i.e. as part of
69 the procedure-oriented interface.
70
712. :class:`RawTurtle` (alias: :class:`RawPen`) defines Turtle objects which draw
72 on a :class:`TurtleScreen`. Its constructor needs a Canvas, ScrolledCanvas
73 or TurtleScreen as argument, so the RawTurtle objects know where to draw.
74
75 Derived from RawTurtle is the subclass :class:`Turtle` (alias: :class:`Pen`),
Alexander Belopolsky435d3062010-10-19 21:07:52 +000076 which draws on "the" :class:`Screen` instance which is automatically
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000077 created, if not already present.
78
79 All methods of RawTurtle/Turtle also exist as functions, i.e. part of the
80 procedure-oriented interface.
81
82The procedural interface provides functions which are derived from the methods
83of the classes :class:`Screen` and :class:`Turtle`. They have the same names as
Georg Brandlae2dbe22009-03-13 19:04:40 +000084the corresponding methods. A screen object is automatically created whenever a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000085function derived from a Screen method is called. An (unnamed) turtle object is
86automatically created whenever any of the functions derived from a Turtle method
87is called.
88
Alexander Belopolsky435d3062010-10-19 21:07:52 +000089To use multiple turtles on a screen one has to use the object-oriented interface.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000090
91.. note::
92 In the following documentation the argument list for functions is given.
93 Methods, of course, have the additional first argument *self* which is
94 omitted here.
Georg Brandl116aa622007-08-15 14:28:22 +000095
96
Alexander Belopolsky435d3062010-10-19 21:07:52 +000097Overview of available Turtle and Screen methods
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000098=================================================
99
100Turtle methods
101--------------
102
103Turtle motion
104 Move and draw
105 | :func:`forward` | :func:`fd`
106 | :func:`backward` | :func:`bk` | :func:`back`
107 | :func:`right` | :func:`rt`
108 | :func:`left` | :func:`lt`
109 | :func:`goto` | :func:`setpos` | :func:`setposition`
110 | :func:`setx`
111 | :func:`sety`
112 | :func:`setheading` | :func:`seth`
113 | :func:`home`
114 | :func:`circle`
115 | :func:`dot`
116 | :func:`stamp`
117 | :func:`clearstamp`
118 | :func:`clearstamps`
119 | :func:`undo`
120 | :func:`speed`
121
122 Tell Turtle's state
123 | :func:`position` | :func:`pos`
124 | :func:`towards`
125 | :func:`xcor`
126 | :func:`ycor`
127 | :func:`heading`
128 | :func:`distance`
129
130 Setting and measurement
131 | :func:`degrees`
132 | :func:`radians`
133
134Pen control
135 Drawing state
136 | :func:`pendown` | :func:`pd` | :func:`down`
137 | :func:`penup` | :func:`pu` | :func:`up`
138 | :func:`pensize` | :func:`width`
139 | :func:`pen`
140 | :func:`isdown`
141
142 Color control
143 | :func:`color`
144 | :func:`pencolor`
145 | :func:`fillcolor`
146
147 Filling
148 | :func:`filling`
149 | :func:`begin_fill`
150 | :func:`end_fill`
151
152 More drawing control
153 | :func:`reset`
154 | :func:`clear`
155 | :func:`write`
156
157Turtle state
158 Visibility
159 | :func:`showturtle` | :func:`st`
160 | :func:`hideturtle` | :func:`ht`
161 | :func:`isvisible`
162
163 Appearance
164 | :func:`shape`
165 | :func:`resizemode`
166 | :func:`shapesize` | :func:`turtlesize`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000167 | :func:`shearfactor`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000168 | :func:`settiltangle`
169 | :func:`tiltangle`
170 | :func:`tilt`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000171 | :func:`shapetransform`
172 | :func:`get_shapepoly`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000173
174Using events
175 | :func:`onclick`
176 | :func:`onrelease`
177 | :func:`ondrag`
178
179Special Turtle methods
180 | :func:`begin_poly`
181 | :func:`end_poly`
182 | :func:`get_poly`
183 | :func:`clone`
184 | :func:`getturtle` | :func:`getpen`
185 | :func:`getscreen`
186 | :func:`setundobuffer`
187 | :func:`undobufferentries`
Georg Brandl116aa622007-08-15 14:28:22 +0000188
189
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000190Methods of TurtleScreen/Screen
191------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000192
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000193Window control
194 | :func:`bgcolor`
195 | :func:`bgpic`
196 | :func:`clear` | :func:`clearscreen`
197 | :func:`reset` | :func:`resetscreen`
198 | :func:`screensize`
199 | :func:`setworldcoordinates`
Georg Brandl116aa622007-08-15 14:28:22 +0000200
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000201Animation control
202 | :func:`delay`
203 | :func:`tracer`
204 | :func:`update`
205
206Using screen events
207 | :func:`listen`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000208 | :func:`onkey` | :func:`onkeyrelease`
209 | :func:`onkeypress`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000210 | :func:`onclick` | :func:`onscreenclick`
211 | :func:`ontimer`
Sandro Tosie3484552011-10-31 10:12:43 +0100212 | :func:`mainloop` | :func:`done`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000213
214Settings and special methods
215 | :func:`mode`
216 | :func:`colormode`
217 | :func:`getcanvas`
218 | :func:`getshapes`
219 | :func:`register_shape` | :func:`addshape`
220 | :func:`turtles`
221 | :func:`window_height`
222 | :func:`window_width`
223
Georg Brandleaa84ef2009-05-05 08:14:33 +0000224Input methods
225 | :func:`textinput`
226 | :func:`numinput`
227
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000228Methods specific to Screen
229 | :func:`bye`
230 | :func:`exitonclick`
231 | :func:`setup`
232 | :func:`title`
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000235Methods of RawTurtle/Turtle and corresponding functions
236=======================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000237
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000238Most of the examples in this section refer to a Turtle instance called
239``turtle``.
Georg Brandl116aa622007-08-15 14:28:22 +0000240
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000241Turtle motion
242-------------
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244.. function:: forward(distance)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000245 fd(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000246
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000247 :param distance: a number (integer or float)
248
249 Move the turtle forward by the specified *distance*, in the direction the
250 turtle is headed.
251
R. David Murrayf877feb2009-05-05 02:08:52 +0000252 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200253 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000254
255 >>> turtle.position()
256 (0.00,0.00)
257 >>> turtle.forward(25)
258 >>> turtle.position()
259 (25.00,0.00)
260 >>> turtle.forward(-75)
261 >>> turtle.position()
262 (-50.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000265.. function:: back(distance)
266 bk(distance)
267 backward(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000268
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000269 :param distance: a number
Georg Brandl116aa622007-08-15 14:28:22 +0000270
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000271 Move the turtle backward by *distance*, opposite to the direction the
272 turtle is headed. Do not change the turtle's heading.
Georg Brandl116aa622007-08-15 14:28:22 +0000273
R. David Murrayf877feb2009-05-05 02:08:52 +0000274 .. doctest::
275 :hide:
276
277 >>> turtle.goto(0, 0)
278
279 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200280 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000281
282 >>> turtle.position()
283 (0.00,0.00)
284 >>> turtle.backward(30)
285 >>> turtle.position()
286 (-30.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000287
288
289.. function:: right(angle)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000290 rt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000291
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000292 :param angle: a number (integer or float)
293
294 Turn turtle right by *angle* units. (Units are by default degrees, but
295 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
296 orientation depends on the turtle mode, see :func:`mode`.
297
R. David Murrayf877feb2009-05-05 02:08:52 +0000298 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200299 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000300 :hide:
301
302 >>> turtle.setheading(22)
303
304 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200305 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000306
307 >>> turtle.heading()
308 22.0
309 >>> turtle.right(45)
310 >>> turtle.heading()
311 337.0
Georg Brandl116aa622007-08-15 14:28:22 +0000312
313
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000314.. function:: left(angle)
315 lt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000316
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000317 :param angle: a number (integer or float)
Georg Brandl116aa622007-08-15 14:28:22 +0000318
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000319 Turn turtle left by *angle* units. (Units are by default degrees, but
320 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
321 orientation depends on the turtle mode, see :func:`mode`.
Georg Brandl116aa622007-08-15 14:28:22 +0000322
R. David Murrayf877feb2009-05-05 02:08:52 +0000323 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200324 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000325 :hide:
326
327 >>> turtle.setheading(22)
328
329 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200330 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000331
332 >>> turtle.heading()
333 22.0
334 >>> turtle.left(45)
335 >>> turtle.heading()
336 67.0
337
Georg Brandl116aa622007-08-15 14:28:22 +0000338
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000339.. function:: goto(x, y=None)
340 setpos(x, y=None)
341 setposition(x, y=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000342
R. David Murrayf877feb2009-05-05 02:08:52 +0000343 :param x: a number or a pair/vector of numbers
344 :param y: a number or ``None``
Georg Brandl116aa622007-08-15 14:28:22 +0000345
R. David Murrayf877feb2009-05-05 02:08:52 +0000346 If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D`
347 (e.g. as returned by :func:`pos`).
Georg Brandl116aa622007-08-15 14:28:22 +0000348
R. David Murrayf877feb2009-05-05 02:08:52 +0000349 Move turtle to an absolute position. If the pen is down, draw line. Do
350 not change the turtle's orientation.
Georg Brandl116aa622007-08-15 14:28:22 +0000351
R. David Murrayf877feb2009-05-05 02:08:52 +0000352 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200353 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000354 :hide:
355
356 >>> turtle.goto(0, 0)
357
358 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200359 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000360
361 >>> tp = turtle.pos()
362 >>> tp
363 (0.00,0.00)
364 >>> turtle.setpos(60,30)
365 >>> turtle.pos()
366 (60.00,30.00)
367 >>> turtle.setpos((20,80))
368 >>> turtle.pos()
369 (20.00,80.00)
370 >>> turtle.setpos(tp)
371 >>> turtle.pos()
372 (0.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000373
Georg Brandl116aa622007-08-15 14:28:22 +0000374
375.. function:: setx(x)
376
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000377 :param x: a number (integer or float)
378
379 Set the turtle's first coordinate to *x*, leave second coordinate
380 unchanged.
381
R. David Murrayf877feb2009-05-05 02:08:52 +0000382 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200383 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000384 :hide:
385
386 >>> turtle.goto(0, 240)
387
388 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200389 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000390
391 >>> turtle.position()
392 (0.00,240.00)
393 >>> turtle.setx(10)
394 >>> turtle.position()
395 (10.00,240.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000396
Georg Brandl116aa622007-08-15 14:28:22 +0000397
398.. function:: sety(y)
399
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000400 :param y: a number (integer or float)
401
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000402 Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000403
R. David Murrayf877feb2009-05-05 02:08:52 +0000404 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200405 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000406 :hide:
407
408 >>> turtle.goto(0, 40)
409
410 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200411 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000412
413 >>> turtle.position()
414 (0.00,40.00)
415 >>> turtle.sety(-10)
416 >>> turtle.position()
417 (0.00,-10.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000418
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000420.. function:: setheading(to_angle)
421 seth(to_angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000423 :param to_angle: a number (integer or float)
424
425 Set the orientation of the turtle to *to_angle*. Here are some common
426 directions in degrees:
427
428 =================== ====================
429 standard mode logo mode
430 =================== ====================
431 0 - east 0 - north
432 90 - north 90 - east
433 180 - west 180 - south
434 270 - south 270 - west
435 =================== ====================
436
R. David Murrayf877feb2009-05-05 02:08:52 +0000437 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200438 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000439
440 >>> turtle.setheading(90)
441 >>> turtle.heading()
442 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000443
444
445.. function:: home()
446
447 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
448 its start-orientation (which depends on the mode, see :func:`mode`).
449
R. David Murrayf877feb2009-05-05 02:08:52 +0000450 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200451 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000452 :hide:
453
454 >>> turtle.setheading(90)
455 >>> turtle.goto(0, -10)
456
457 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200458 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000459
460 >>> turtle.heading()
461 90.0
462 >>> turtle.position()
463 (0.00,-10.00)
464 >>> turtle.home()
465 >>> turtle.position()
466 (0.00,0.00)
467 >>> turtle.heading()
468 0.0
469
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000470
471.. function:: circle(radius, extent=None, steps=None)
472
473 :param radius: a number
474 :param extent: a number (or ``None``)
475 :param steps: an integer (or ``None``)
476
477 Draw a circle with given *radius*. The center is *radius* units left of
478 the turtle; *extent* -- an angle -- determines which part of the circle
479 is drawn. If *extent* is not given, draw the entire circle. If *extent*
480 is not a full circle, one endpoint of the arc is the current pen
481 position. Draw the arc in counterclockwise direction if *radius* is
482 positive, otherwise in clockwise direction. Finally the direction of the
483 turtle is changed by the amount of *extent*.
484
485 As the circle is approximated by an inscribed regular polygon, *steps*
486 determines the number of steps to use. If not given, it will be
487 calculated automatically. May be used to draw regular polygons.
488
R. David Murrayf877feb2009-05-05 02:08:52 +0000489 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200490 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000491
492 >>> turtle.home()
493 >>> turtle.position()
494 (0.00,0.00)
495 >>> turtle.heading()
496 0.0
497 >>> turtle.circle(50)
498 >>> turtle.position()
499 (-0.00,0.00)
500 >>> turtle.heading()
501 0.0
502 >>> turtle.circle(120, 180) # draw a semicircle
503 >>> turtle.position()
504 (0.00,240.00)
505 >>> turtle.heading()
506 180.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000507
508
509.. function:: dot(size=None, *color)
510
511 :param size: an integer >= 1 (if given)
512 :param color: a colorstring or a numeric color tuple
513
514 Draw a circular dot with diameter *size*, using *color*. If *size* is
515 not given, the maximum of pensize+4 and 2*pensize is used.
516
R. David Murrayf877feb2009-05-05 02:08:52 +0000517
518 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200519 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000520
521 >>> turtle.home()
522 >>> turtle.dot()
523 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
524 >>> turtle.position()
525 (100.00,-0.00)
526 >>> turtle.heading()
527 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000528
529
530.. function:: stamp()
531
532 Stamp a copy of the turtle shape onto the canvas at the current turtle
533 position. Return a stamp_id for that stamp, which can be used to delete
534 it by calling ``clearstamp(stamp_id)``.
535
R. David Murrayf877feb2009-05-05 02:08:52 +0000536 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200537 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000538
539 >>> turtle.color("blue")
540 >>> turtle.stamp()
541 11
542 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000543
544
545.. function:: clearstamp(stampid)
546
547 :param stampid: an integer, must be return value of previous
548 :func:`stamp` call
549
550 Delete stamp with given *stampid*.
551
R. David Murrayf877feb2009-05-05 02:08:52 +0000552 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200553 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000554
555 >>> turtle.position()
556 (150.00,-0.00)
557 >>> turtle.color("blue")
558 >>> astamp = turtle.stamp()
559 >>> turtle.fd(50)
560 >>> turtle.position()
561 (200.00,-0.00)
562 >>> turtle.clearstamp(astamp)
563 >>> turtle.position()
564 (200.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000565
566
567.. function:: clearstamps(n=None)
568
569 :param n: an integer (or ``None``)
570
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300571 Delete all or first/last *n* of turtle's stamps. If *n* is ``None``, delete
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000572 all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete
573 last *n* stamps.
574
R. David Murrayf877feb2009-05-05 02:08:52 +0000575 .. doctest::
576
577 >>> for i in range(8):
578 ... turtle.stamp(); turtle.fd(30)
579 13
580 14
581 15
582 16
583 17
584 18
585 19
586 20
587 >>> turtle.clearstamps(2)
588 >>> turtle.clearstamps(-2)
589 >>> turtle.clearstamps()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000590
591
592.. function:: undo()
593
594 Undo (repeatedly) the last turtle action(s). Number of available
595 undo actions is determined by the size of the undobuffer.
596
R. David Murrayf877feb2009-05-05 02:08:52 +0000597 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200598 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000599
600 >>> for i in range(4):
601 ... turtle.fd(50); turtle.lt(80)
602 ...
603 >>> for i in range(8):
604 ... turtle.undo()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000605
606
607.. function:: speed(speed=None)
608
609 :param speed: an integer in the range 0..10 or a speedstring (see below)
610
611 Set the turtle's speed to an integer value in the range 0..10. If no
612 argument is given, return current speed.
613
614 If input is a number greater than 10 or smaller than 0.5, speed is set
615 to 0. Speedstrings are mapped to speedvalues as follows:
616
617 * "fastest": 0
618 * "fast": 10
619 * "normal": 6
620 * "slow": 3
621 * "slowest": 1
622
623 Speeds from 1 to 10 enforce increasingly faster animation of line drawing
624 and turtle turning.
625
626 Attention: *speed* = 0 means that *no* animation takes
627 place. forward/back makes turtle jump and likewise left/right make the
628 turtle turn instantly.
629
R. David Murrayf877feb2009-05-05 02:08:52 +0000630 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200631 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000632
633 >>> turtle.speed()
634 3
635 >>> turtle.speed('normal')
636 >>> turtle.speed()
637 6
638 >>> turtle.speed(9)
639 >>> turtle.speed()
640 9
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000641
642
643Tell Turtle's state
644-------------------
645
646.. function:: position()
647 pos()
648
649 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
650
R. David Murrayf877feb2009-05-05 02:08:52 +0000651 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200652 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000653
654 >>> turtle.pos()
655 (440.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000656
657
658.. function:: towards(x, y=None)
659
660 :param x: a number or a pair/vector of numbers or a turtle instance
661 :param y: a number if *x* is a number, else ``None``
662
663 Return the angle between the line from turtle position to position specified
664 by (x,y), the vector or the other turtle. This depends on the turtle's start
665 orientation which depends on the mode - "standard"/"world" or "logo").
666
R. David Murrayf877feb2009-05-05 02:08:52 +0000667 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200668 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000669
670 >>> turtle.goto(10, 10)
671 >>> turtle.towards(0,0)
672 225.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000673
674
675.. function:: xcor()
676
677 Return the turtle's x coordinate.
678
R. David Murrayf877feb2009-05-05 02:08:52 +0000679 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200680 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000681
682 >>> turtle.home()
683 >>> turtle.left(50)
684 >>> turtle.forward(100)
685 >>> turtle.pos()
686 (64.28,76.60)
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000687 >>> print(round(turtle.xcor(), 5))
688 64.27876
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000689
690
691.. function:: ycor()
692
693 Return the turtle's y coordinate.
694
R. David Murrayf877feb2009-05-05 02:08:52 +0000695 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200696 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000697
698 >>> turtle.home()
699 >>> turtle.left(60)
700 >>> turtle.forward(100)
Ezio Melotti985e24d2009-09-13 07:54:02 +0000701 >>> print(turtle.pos())
R. David Murrayf877feb2009-05-05 02:08:52 +0000702 (50.00,86.60)
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000703 >>> print(round(turtle.ycor(), 5))
704 86.60254
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000705
706
707.. function:: heading()
708
709 Return the turtle's current heading (value depends on the turtle mode, see
710 :func:`mode`).
711
R. David Murrayf877feb2009-05-05 02:08:52 +0000712 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200713 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000714
715 >>> turtle.home()
716 >>> turtle.left(67)
717 >>> turtle.heading()
718 67.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000719
720
721.. function:: distance(x, y=None)
722
723 :param x: a number or a pair/vector of numbers or a turtle instance
724 :param y: a number if *x* is a number, else ``None``
725
726 Return the distance from the turtle to (x,y), the given vector, or the given
727 other turtle, in turtle step units.
728
R. David Murrayf877feb2009-05-05 02:08:52 +0000729 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200730 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000731
732 >>> turtle.home()
733 >>> turtle.distance(30,40)
734 50.0
735 >>> turtle.distance((30,40))
736 50.0
737 >>> joe = Turtle()
738 >>> joe.forward(77)
739 >>> turtle.distance(joe)
740 77.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000741
742
743Settings for measurement
744------------------------
745
746.. function:: degrees(fullcircle=360.0)
747
748 :param fullcircle: a number
749
750 Set angle measurement units, i.e. set number of "degrees" for a full circle.
751 Default value is 360 degrees.
752
R. David Murrayf877feb2009-05-05 02:08:52 +0000753 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200754 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000755
756 >>> turtle.home()
757 >>> turtle.left(90)
758 >>> turtle.heading()
759 90.0
Alexander Belopolsky3cdfb122010-10-29 17:16:49 +0000760
761 Change angle measurement unit to grad (also known as gon,
762 grade, or gradian and equals 1/100-th of the right angle.)
763 >>> turtle.degrees(400.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000764 >>> turtle.heading()
765 100.0
766 >>> turtle.degrees(360)
767 >>> turtle.heading()
768 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000769
770
771.. function:: radians()
772
773 Set the angle measurement units to radians. Equivalent to
774 ``degrees(2*math.pi)``.
775
R. David Murrayf877feb2009-05-05 02:08:52 +0000776 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200777 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000778
779 >>> turtle.home()
780 >>> turtle.left(90)
781 >>> turtle.heading()
782 90.0
783 >>> turtle.radians()
784 >>> turtle.heading()
785 1.5707963267948966
786
787 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200788 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000789 :hide:
790
791 >>> turtle.degrees(360)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000792
793
794Pen control
795-----------
796
797Drawing state
798~~~~~~~~~~~~~
799
800.. function:: pendown()
801 pd()
802 down()
803
804 Pull the pen down -- drawing when moving.
805
806
807.. function:: penup()
808 pu()
809 up()
810
811 Pull the pen up -- no drawing when moving.
812
813
814.. function:: pensize(width=None)
815 width(width=None)
816
817 :param width: a positive number
818
819 Set the line thickness to *width* or return it. If resizemode is set to
820 "auto" and turtleshape is a polygon, that polygon is drawn with the same line
821 thickness. If no argument is given, the current pensize is returned.
822
R. David Murrayf877feb2009-05-05 02:08:52 +0000823 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200824 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000825
826 >>> turtle.pensize()
827 1
828 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000829
830
831.. function:: pen(pen=None, **pendict)
832
833 :param pen: a dictionary with some or all of the below listed keys
834 :param pendict: one or more keyword-arguments with the below listed keys as keywords
835
836 Return or set the pen's attributes in a "pen-dictionary" with the following
837 key/value pairs:
838
839 * "shown": True/False
840 * "pendown": True/False
841 * "pencolor": color-string or color-tuple
842 * "fillcolor": color-string or color-tuple
843 * "pensize": positive number
844 * "speed": number in range 0..10
845 * "resizemode": "auto" or "user" or "noresize"
846 * "stretchfactor": (positive number, positive number)
847 * "outline": positive number
848 * "tilt": number
849
R. David Murrayf877feb2009-05-05 02:08:52 +0000850 This dictionary can be used as argument for a subsequent call to :func:`pen`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000851 to restore the former pen-state. Moreover one or more of these attributes
852 can be provided as keyword-arguments. This can be used to set several pen
853 attributes in one statement.
854
R. David Murrayf877feb2009-05-05 02:08:52 +0000855 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200856 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000857 :options: +NORMALIZE_WHITESPACE
858
859 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
860 >>> sorted(turtle.pen().items())
861 [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
862 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000863 ('shearfactor', 0.0), ('shown', True), ('speed', 9),
864 ('stretchfactor', (1.0, 1.0)), ('tilt', 0.0)]
R. David Murrayf877feb2009-05-05 02:08:52 +0000865 >>> penstate=turtle.pen()
866 >>> turtle.color("yellow", "")
867 >>> turtle.penup()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000868 >>> sorted(turtle.pen().items())[:3]
869 [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow')]
R. David Murrayf877feb2009-05-05 02:08:52 +0000870 >>> turtle.pen(penstate, fillcolor="green")
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000871 >>> sorted(turtle.pen().items())[:3]
872 [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red')]
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000873
874.. function:: isdown()
875
876 Return ``True`` if pen is down, ``False`` if it's up.
877
R. David Murrayf877feb2009-05-05 02:08:52 +0000878 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200879 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000880
881 >>> turtle.penup()
882 >>> turtle.isdown()
883 False
884 >>> turtle.pendown()
885 >>> turtle.isdown()
886 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000887
888
889Color control
890~~~~~~~~~~~~~
891
892.. function:: pencolor(*args)
893
894 Return or set the pencolor.
895
896 Four input formats are allowed:
897
898 ``pencolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000899 Return the current pencolor as color specification string or
900 as a tuple (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000901 color/pencolor/fillcolor call.
902
903 ``pencolor(colorstring)``
904 Set pencolor to *colorstring*, which is a Tk color specification string,
905 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
906
907 ``pencolor((r, g, b))``
908 Set pencolor to the RGB color represented by the tuple of *r*, *g*, and
909 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
910 colormode is either 1.0 or 255 (see :func:`colormode`).
911
912 ``pencolor(r, g, b)``
913 Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of
914 *r*, *g*, and *b* must be in the range 0..colormode.
915
916 If turtleshape is a polygon, the outline of that polygon is drawn with the
917 newly set pencolor.
918
R. David Murrayf877feb2009-05-05 02:08:52 +0000919 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200920 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000921
922 >>> colormode()
923 1.0
924 >>> turtle.pencolor()
925 'red'
926 >>> turtle.pencolor("brown")
927 >>> turtle.pencolor()
928 'brown'
929 >>> tup = (0.2, 0.8, 0.55)
930 >>> turtle.pencolor(tup)
931 >>> turtle.pencolor()
Mark Dickinson5a55b612009-06-28 20:59:42 +0000932 (0.2, 0.8, 0.5490196078431373)
R. David Murrayf877feb2009-05-05 02:08:52 +0000933 >>> colormode(255)
934 >>> turtle.pencolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000935 (51.0, 204.0, 140.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000936 >>> turtle.pencolor('#32c18f')
937 >>> turtle.pencolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000938 (50.0, 193.0, 143.0)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000939
940
941.. function:: fillcolor(*args)
942
943 Return or set the fillcolor.
944
945 Four input formats are allowed:
946
947 ``fillcolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000948 Return the current fillcolor as color specification string, possibly
949 in tuple format (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000950 color/pencolor/fillcolor call.
951
952 ``fillcolor(colorstring)``
953 Set fillcolor to *colorstring*, which is a Tk color specification string,
954 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
955
956 ``fillcolor((r, g, b))``
957 Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and
958 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
959 colormode is either 1.0 or 255 (see :func:`colormode`).
960
961 ``fillcolor(r, g, b)``
962 Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of
963 *r*, *g*, and *b* must be in the range 0..colormode.
964
965 If turtleshape is a polygon, the interior of that polygon is drawn
966 with the newly set fillcolor.
967
R. David Murrayf877feb2009-05-05 02:08:52 +0000968 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +0200969 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +0000970
971 >>> turtle.fillcolor("violet")
972 >>> turtle.fillcolor()
973 'violet'
Marco Buttu7b2491a2017-04-13 16:17:59 +0200974 >>> turtle.pencolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000975 (50.0, 193.0, 143.0)
Marco Buttu7b2491a2017-04-13 16:17:59 +0200976 >>> turtle.fillcolor((50, 193, 143)) # Integers, not floats
R. David Murrayf877feb2009-05-05 02:08:52 +0000977 >>> turtle.fillcolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000978 (50.0, 193.0, 143.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000979 >>> turtle.fillcolor('#ffffff')
980 >>> turtle.fillcolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000981 (255.0, 255.0, 255.0)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000982
983
984.. function:: color(*args)
985
986 Return or set pencolor and fillcolor.
987
988 Several input formats are allowed. They use 0 to 3 arguments as
989 follows:
990
991 ``color()``
992 Return the current pencolor and the current fillcolor as a pair of color
R. David Murrayf877feb2009-05-05 02:08:52 +0000993 specification strings or tuples as returned by :func:`pencolor` and
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000994 :func:`fillcolor`.
995
996 ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)``
997 Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the
998 given value.
999
1000 ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))``
1001 Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)``
1002 and analogously if the other input format is used.
1003
1004 If turtleshape is a polygon, outline and interior of that polygon is drawn
1005 with the newly set colors.
1006
R. David Murrayf877feb2009-05-05 02:08:52 +00001007 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001008 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001009
1010 >>> turtle.color("red", "green")
1011 >>> turtle.color()
1012 ('red', 'green')
1013 >>> color("#285078", "#a0c8f0")
1014 >>> color()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001015 ((40.0, 80.0, 120.0), (160.0, 200.0, 240.0))
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001016
1017
1018See also: Screen method :func:`colormode`.
1019
1020
1021Filling
1022~~~~~~~
1023
R. David Murrayf877feb2009-05-05 02:08:52 +00001024.. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001025 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001026 :hide:
1027
1028 >>> turtle.home()
1029
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001030.. function:: filling()
1031
1032 Return fillstate (``True`` if filling, ``False`` else).
1033
R. David Murrayf877feb2009-05-05 02:08:52 +00001034 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001035 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001036
1037 >>> turtle.begin_fill()
1038 >>> if turtle.filling():
1039 ... turtle.pensize(5)
1040 ... else:
1041 ... turtle.pensize(3)
1042
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001043
1044
1045.. function:: begin_fill()
1046
1047 To be called just before drawing a shape to be filled.
1048
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001049
1050.. function:: end_fill()
1051
1052 Fill the shape drawn after the last call to :func:`begin_fill`.
1053
Terry Jan Reedy2824c452020-01-27 18:41:18 -05001054 Whether or not overlap regions for self-intersecting polygons
1055 or multiple shapes are filled depends on the operating system graphics,
1056 type of overlap, and number of overlaps. For example, the Turtle star
1057 above may be either all yellow or have some white regions.
1058
R. David Murrayf877feb2009-05-05 02:08:52 +00001059 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001060 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001061
1062 >>> turtle.color("black", "red")
1063 >>> turtle.begin_fill()
1064 >>> turtle.circle(80)
1065 >>> turtle.end_fill()
1066
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001067
1068More drawing control
1069~~~~~~~~~~~~~~~~~~~~
1070
1071.. function:: reset()
Victor Stinner8f881902020-08-19 19:25:22 +02001072 :noindex:
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001073
1074 Delete the turtle's drawings from the screen, re-center the turtle and set
1075 variables to the default values.
1076
R. David Murrayf877feb2009-05-05 02:08:52 +00001077 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001078 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001079
1080 >>> turtle.goto(0,-22)
1081 >>> turtle.left(100)
1082 >>> turtle.position()
1083 (0.00,-22.00)
1084 >>> turtle.heading()
1085 100.0
1086 >>> turtle.reset()
1087 >>> turtle.position()
1088 (0.00,0.00)
1089 >>> turtle.heading()
1090 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001091
1092
1093.. function:: clear()
Victor Stinner8f881902020-08-19 19:25:22 +02001094 :noindex:
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001095
1096 Delete the turtle's drawings from the screen. Do not move turtle. State and
1097 position of the turtle as well as drawings of other turtles are not affected.
1098
1099
1100.. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal"))
1101
1102 :param arg: object to be written to the TurtleScreen
1103 :param move: True/False
1104 :param align: one of the strings "left", "center" or right"
1105 :param font: a triple (fontname, fontsize, fonttype)
1106
1107 Write text - the string representation of *arg* - at the current turtle
1108 position according to *align* ("left", "center" or right") and with the given
Serhiy Storchakafbc1c262013-11-29 12:17:13 +02001109 font. If *move* is true, the pen is moved to the bottom-right corner of the
1110 text. By default, *move* is ``False``.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001111
1112 >>> turtle.write("Home = ", True, align="center")
1113 >>> turtle.write((0,0), True)
1114
1115
1116Turtle state
1117------------
1118
1119Visibility
1120~~~~~~~~~~
1121
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001122.. function:: hideturtle()
1123 ht()
1124
1125 Make the turtle invisible. It's a good idea to do this while you're in the
1126 middle of doing some complex drawing, because hiding the turtle speeds up the
1127 drawing observably.
1128
R. David Murrayf877feb2009-05-05 02:08:52 +00001129 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001130 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001131
1132 >>> turtle.hideturtle()
1133
1134
1135.. function:: showturtle()
1136 st()
1137
1138 Make the turtle visible.
1139
1140 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001141 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001142
1143 >>> turtle.showturtle()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001144
1145
1146.. function:: isvisible()
1147
Serhiy Storchakafbc1c262013-11-29 12:17:13 +02001148 Return ``True`` if the Turtle is shown, ``False`` if it's hidden.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001149
1150 >>> turtle.hideturtle()
R. David Murrayf877feb2009-05-05 02:08:52 +00001151 >>> turtle.isvisible()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001152 False
R. David Murrayf877feb2009-05-05 02:08:52 +00001153 >>> turtle.showturtle()
1154 >>> turtle.isvisible()
1155 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001156
1157
1158Appearance
1159~~~~~~~~~~
1160
1161.. function:: shape(name=None)
1162
1163 :param name: a string which is a valid shapename
1164
1165 Set turtle shape to shape with given *name* or, if name is not given, return
1166 name of current shape. Shape with *name* must exist in the TurtleScreen's
1167 shape dictionary. Initially there are the following polygon shapes: "arrow",
1168 "turtle", "circle", "square", "triangle", "classic". To learn about how to
1169 deal with shapes see Screen method :func:`register_shape`.
1170
R. David Murrayf877feb2009-05-05 02:08:52 +00001171 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001172 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001173
1174 >>> turtle.shape()
1175 'classic'
1176 >>> turtle.shape("turtle")
1177 >>> turtle.shape()
1178 'turtle'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001179
1180
1181.. function:: resizemode(rmode=None)
1182
1183 :param rmode: one of the strings "auto", "user", "noresize"
1184
1185 Set resizemode to one of the values: "auto", "user", "noresize". If *rmode*
1186 is not given, return current resizemode. Different resizemodes have the
1187 following effects:
1188
1189 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
1190 - "user": adapts the appearance of the turtle according to the values of
1191 stretchfactor and outlinewidth (outline), which are set by
1192 :func:`shapesize`.
1193 - "noresize": no adaption of the turtle's appearance takes place.
1194
1195 resizemode("user") is called by :func:`shapesize` when used with arguments.
1196
R. David Murrayf877feb2009-05-05 02:08:52 +00001197 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001198 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001199
1200 >>> turtle.resizemode()
1201 'noresize'
1202 >>> turtle.resizemode("auto")
1203 >>> turtle.resizemode()
1204 'auto'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001205
1206
1207.. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None)
R. David Murray7fcd3de2009-06-25 14:26:19 +00001208 turtlesize(stretch_wid=None, stretch_len=None, outline=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001209
1210 :param stretch_wid: positive number
1211 :param stretch_len: positive number
1212 :param outline: positive number
1213
1214 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set
1215 resizemode to "user". If and only if resizemode is set to "user", the turtle
1216 will be displayed stretched according to its stretchfactors: *stretch_wid* is
1217 stretchfactor perpendicular to its orientation, *stretch_len* is
1218 stretchfactor in direction of its orientation, *outline* determines the width
1219 of the shapes's outline.
1220
R. David Murrayf877feb2009-05-05 02:08:52 +00001221 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001222 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001223
1224 >>> turtle.shapesize()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001225 (1.0, 1.0, 1)
R. David Murrayf877feb2009-05-05 02:08:52 +00001226 >>> turtle.resizemode("user")
1227 >>> turtle.shapesize(5, 5, 12)
1228 >>> turtle.shapesize()
1229 (5, 5, 12)
1230 >>> turtle.shapesize(outline=8)
1231 >>> turtle.shapesize()
1232 (5, 5, 8)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001233
1234
R. David Murray7fcd3de2009-06-25 14:26:19 +00001235.. function:: shearfactor(shear=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001236
1237 :param shear: number (optional)
1238
1239 Set or return the current shearfactor. Shear the turtleshape according to
1240 the given shearfactor shear, which is the tangent of the shear angle.
1241 Do *not* change the turtle's heading (direction of movement).
1242 If shear is not given: return the current shearfactor, i. e. the
1243 tangent of the shear angle, by which lines parallel to the
1244 heading of the turtle are sheared.
1245
1246 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001247 :skipif: _tkinter is None
Georg Brandleaa84ef2009-05-05 08:14:33 +00001248
1249 >>> turtle.shape("circle")
1250 >>> turtle.shapesize(5,2)
1251 >>> turtle.shearfactor(0.5)
1252 >>> turtle.shearfactor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001253 0.5
Georg Brandleaa84ef2009-05-05 08:14:33 +00001254
1255
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001256.. function:: tilt(angle)
1257
1258 :param angle: a number
1259
1260 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
1261 change the turtle's heading (direction of movement).
1262
R. David Murrayf877feb2009-05-05 02:08:52 +00001263 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001264 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001265
1266 >>> turtle.reset()
1267 >>> turtle.shape("circle")
1268 >>> turtle.shapesize(5,2)
1269 >>> turtle.tilt(30)
1270 >>> turtle.fd(50)
1271 >>> turtle.tilt(30)
1272 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001273
1274
1275.. function:: settiltangle(angle)
1276
1277 :param angle: a number
1278
1279 Rotate the turtleshape to point in the direction specified by *angle*,
1280 regardless of its current tilt-angle. *Do not* change the turtle's heading
1281 (direction of movement).
1282
R. David Murrayf877feb2009-05-05 02:08:52 +00001283 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001284 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001285
1286 >>> turtle.reset()
1287 >>> turtle.shape("circle")
1288 >>> turtle.shapesize(5,2)
1289 >>> turtle.settiltangle(45)
1290 >>> turtle.fd(50)
1291 >>> turtle.settiltangle(-45)
1292 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001293
Ezio Melotti4e511282010-02-14 03:11:06 +00001294 .. deprecated:: 3.1
1295
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001296
Georg Brandleaa84ef2009-05-05 08:14:33 +00001297.. function:: tiltangle(angle=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001298
Georg Brandleaa84ef2009-05-05 08:14:33 +00001299 :param angle: a number (optional)
1300
1301 Set or return the current tilt-angle. If angle is given, rotate the
1302 turtleshape to point in the direction specified by angle,
1303 regardless of its current tilt-angle. Do *not* change the turtle's
1304 heading (direction of movement).
1305 If angle is not given: return the current tilt-angle, i. e. the angle
1306 between the orientation of the turtleshape and the heading of the
1307 turtle (its direction of movement).
1308
R. David Murrayf877feb2009-05-05 02:08:52 +00001309 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001310 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001311
1312 >>> turtle.reset()
1313 >>> turtle.shape("circle")
1314 >>> turtle.shapesize(5,2)
1315 >>> turtle.tilt(45)
1316 >>> turtle.tiltangle()
1317 45.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001318
1319
Georg Brandleaa84ef2009-05-05 08:14:33 +00001320.. function:: shapetransform(t11=None, t12=None, t21=None, t22=None)
1321
1322 :param t11: a number (optional)
1323 :param t12: a number (optional)
1324 :param t21: a number (optional)
1325 :param t12: a number (optional)
1326
1327 Set or return the current transformation matrix of the turtle shape.
1328
1329 If none of the matrix elements are given, return the transformation
1330 matrix as a tuple of 4 elements.
1331 Otherwise set the given elements and transform the turtleshape
1332 according to the matrix consisting of first row t11, t12 and
1333 second row t21, 22. The determinant t11 * t22 - t12 * t21 must not be
1334 zero, otherwise an error is raised.
1335 Modify stretchfactor, shearfactor and tiltangle according to the
1336 given matrix.
1337
1338 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001339 :skipif: _tkinter is None
Georg Brandleaa84ef2009-05-05 08:14:33 +00001340
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001341 >>> turtle = Turtle()
Georg Brandleaa84ef2009-05-05 08:14:33 +00001342 >>> turtle.shape("square")
1343 >>> turtle.shapesize(4,2)
1344 >>> turtle.shearfactor(-0.5)
1345 >>> turtle.shapetransform()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001346 (4.0, -1.0, -0.0, 2.0)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001347
1348
R. David Murray7fcd3de2009-06-25 14:26:19 +00001349.. function:: get_shapepoly()
Georg Brandleaa84ef2009-05-05 08:14:33 +00001350
1351 Return the current shape polygon as tuple of coordinate pairs. This
1352 can be used to define a new shape or components of a compound shape.
1353
1354 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001355 :skipif: _tkinter is None
Georg Brandleaa84ef2009-05-05 08:14:33 +00001356
1357 >>> turtle.shape("square")
1358 >>> turtle.shapetransform(4, -1, 0, 2)
1359 >>> turtle.get_shapepoly()
1360 ((50, -20), (30, 20), (-50, 20), (-30, -20))
1361
1362
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001363Using events
1364------------
1365
1366.. function:: onclick(fun, btn=1, add=None)
Victor Stinner8f881902020-08-19 19:25:22 +02001367 :noindex:
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001368
1369 :param fun: a function with two arguments which will be called with the
1370 coordinates of the clicked point on the canvas
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)4edeaea2018-11-16 18:58:51 +05301371 :param btn: number of the mouse-button, defaults to 1 (left mouse button)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001372 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1373 added, otherwise it will replace a former binding
1374
1375 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1376 existing bindings are removed. Example for the anonymous turtle, i.e. the
1377 procedural way:
1378
R. David Murrayf877feb2009-05-05 02:08:52 +00001379 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001380 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001381
1382 >>> def turn(x, y):
1383 ... left(180)
1384 ...
1385 >>> onclick(turn) # Now clicking into the turtle will turn it.
1386 >>> onclick(None) # event-binding will be removed
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001387
1388
1389.. function:: onrelease(fun, btn=1, add=None)
1390
1391 :param fun: a function with two arguments which will be called with the
1392 coordinates of the clicked point on the canvas
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)4edeaea2018-11-16 18:58:51 +05301393 :param btn: number of the mouse-button, defaults to 1 (left mouse button)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001394 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1395 added, otherwise it will replace a former binding
1396
1397 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1398 ``None``, existing bindings are removed.
1399
R. David Murrayf877feb2009-05-05 02:08:52 +00001400 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001401 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001402
1403 >>> class MyTurtle(Turtle):
1404 ... def glow(self,x,y):
1405 ... self.fillcolor("red")
1406 ... def unglow(self,x,y):
1407 ... self.fillcolor("")
1408 ...
1409 >>> turtle = MyTurtle()
1410 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1411 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001412
1413
1414.. function:: ondrag(fun, btn=1, add=None)
1415
1416 :param fun: a function with two arguments which will be called with the
1417 coordinates of the clicked point on the canvas
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)4edeaea2018-11-16 18:58:51 +05301418 :param btn: number of the mouse-button, defaults to 1 (left mouse button)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001419 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1420 added, otherwise it will replace a former binding
1421
1422 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1423 existing bindings are removed.
1424
1425 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1426 mouse-click event on that turtle.
1427
R. David Murrayf877feb2009-05-05 02:08:52 +00001428 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001429 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001430
1431 >>> turtle.ondrag(turtle.goto)
1432
1433 Subsequently, clicking and dragging the Turtle will move it across
1434 the screen thereby producing handdrawings (if pen is down).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001435
1436
1437Special Turtle methods
1438----------------------
1439
1440.. function:: begin_poly()
1441
1442 Start recording the vertices of a polygon. Current turtle position is first
1443 vertex of polygon.
1444
1445
1446.. function:: end_poly()
1447
1448 Stop recording the vertices of a polygon. Current turtle position is last
1449 vertex of polygon. This will be connected with the first vertex.
1450
1451
1452.. function:: get_poly()
1453
1454 Return the last recorded polygon.
1455
R. David Murrayf877feb2009-05-05 02:08:52 +00001456 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001457 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001458
1459 >>> turtle.home()
1460 >>> turtle.begin_poly()
1461 >>> turtle.fd(100)
1462 >>> turtle.left(20)
1463 >>> turtle.fd(30)
1464 >>> turtle.left(60)
1465 >>> turtle.fd(50)
1466 >>> turtle.end_poly()
1467 >>> p = turtle.get_poly()
1468 >>> register_shape("myFavouriteShape", p)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001469
1470
1471.. function:: clone()
1472
1473 Create and return a clone of the turtle with same position, heading and
1474 turtle properties.
1475
R. David Murrayf877feb2009-05-05 02:08:52 +00001476 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001477 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001478
1479 >>> mick = Turtle()
1480 >>> joe = mick.clone()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001481
1482
1483.. function:: getturtle()
R. David Murray7fcd3de2009-06-25 14:26:19 +00001484 getpen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001485
1486 Return the Turtle object itself. Only reasonable use: as a function to
1487 return the "anonymous turtle":
1488
R. David Murrayf877feb2009-05-05 02:08:52 +00001489 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001490 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001491
1492 >>> pet = getturtle()
1493 >>> pet.fd(50)
1494 >>> pet
1495 <turtle.Turtle object at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001496
1497
1498.. function:: getscreen()
1499
1500 Return the :class:`TurtleScreen` object the turtle is drawing on.
1501 TurtleScreen methods can then be called for that object.
1502
R. David Murrayf877feb2009-05-05 02:08:52 +00001503 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001504 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001505
1506 >>> ts = turtle.getscreen()
1507 >>> ts
1508 <turtle._Screen object at 0x...>
1509 >>> ts.bgcolor("pink")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001510
1511
1512.. function:: setundobuffer(size)
1513
1514 :param size: an integer or ``None``
1515
1516 Set or disable undobuffer. If *size* is an integer an empty undobuffer of
1517 given size is installed. *size* gives the maximum number of turtle actions
1518 that can be undone by the :func:`undo` method/function. If *size* is
1519 ``None``, the undobuffer is disabled.
1520
R. David Murrayf877feb2009-05-05 02:08:52 +00001521 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001522 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001523
1524 >>> turtle.setundobuffer(42)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001525
1526
1527.. function:: undobufferentries()
1528
1529 Return number of entries in the undobuffer.
1530
R. David Murrayf877feb2009-05-05 02:08:52 +00001531 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001532 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001533
1534 >>> while undobufferentries():
1535 ... undo()
1536
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001537
1538
1539.. _compoundshapes:
1540
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001541Compound shapes
Alexander Belopolsky41f56f02010-10-21 18:15:39 +00001542---------------
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001543
1544To use compound turtle shapes, which consist of several polygons of different
1545color, you must use the helper class :class:`Shape` explicitly as described
1546below:
1547
15481. Create an empty Shape object of type "compound".
15492. Add as many components to this object as desired, using the
1550 :meth:`addcomponent` method.
1551
1552 For example:
1553
R. David Murrayf877feb2009-05-05 02:08:52 +00001554 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001555 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001556
1557 >>> s = Shape("compound")
1558 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1559 >>> s.addcomponent(poly1, "red", "blue")
1560 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1561 >>> s.addcomponent(poly2, "blue", "red")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001562
15633. Now add the Shape to the Screen's shapelist and use it:
1564
R. David Murrayf877feb2009-05-05 02:08:52 +00001565 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001566 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001567
1568 >>> register_shape("myshape", s)
1569 >>> shape("myshape")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001570
1571
1572.. note::
1573
1574 The :class:`Shape` class is used internally by the :func:`register_shape`
1575 method in different ways. The application programmer has to deal with the
1576 Shape class *only* when using compound shapes like shown above!
1577
1578
1579Methods of TurtleScreen/Screen and corresponding functions
1580==========================================================
1581
1582Most of the examples in this section refer to a TurtleScreen instance called
1583``screen``.
1584
R. David Murrayf877feb2009-05-05 02:08:52 +00001585.. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001586 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001587 :hide:
1588
1589 >>> screen = Screen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001590
1591Window control
1592--------------
1593
1594.. function:: bgcolor(*args)
1595
1596 :param args: a color string or three numbers in the range 0..colormode or a
1597 3-tuple of such numbers
1598
Alexander Belopolsky3cdfb122010-10-29 17:16:49 +00001599
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001600 Set or return background color of the TurtleScreen.
1601
R. David Murrayf877feb2009-05-05 02:08:52 +00001602 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001603 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001604
1605 >>> screen.bgcolor("orange")
1606 >>> screen.bgcolor()
1607 'orange'
1608 >>> screen.bgcolor("#800080")
1609 >>> screen.bgcolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001610 (128.0, 0.0, 128.0)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001611
1612
1613.. function:: bgpic(picname=None)
1614
1615 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
1616
1617 Set background image or return name of current backgroundimage. If *picname*
1618 is a filename, set the corresponding image as background. If *picname* is
1619 ``"nopic"``, delete background image, if present. If *picname* is ``None``,
R. David Murrayf877feb2009-05-05 02:08:52 +00001620 return the filename of the current backgroundimage. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001621
R. David Murrayf877feb2009-05-05 02:08:52 +00001622 >>> screen.bgpic()
1623 'nopic'
1624 >>> screen.bgpic("landscape.gif")
1625 >>> screen.bgpic()
1626 "landscape.gif"
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001627
1628
1629.. function:: clear()
1630 clearscreen()
1631
1632 Delete all drawings and all turtles from the TurtleScreen. Reset the now
1633 empty TurtleScreen to its initial state: white background, no background
1634 image, no event bindings and tracing on.
1635
1636 .. note::
1637 This TurtleScreen method is available as a global function only under the
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001638 name ``clearscreen``. The global function ``clear`` is a different one
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001639 derived from the Turtle method ``clear``.
1640
1641
1642.. function:: reset()
1643 resetscreen()
1644
1645 Reset all Turtles on the Screen to their initial state.
1646
1647 .. note::
1648 This TurtleScreen method is available as a global function only under the
1649 name ``resetscreen``. The global function ``reset`` is another one
1650 derived from the Turtle method ``reset``.
1651
1652
1653.. function:: screensize(canvwidth=None, canvheight=None, bg=None)
1654
Georg Brandlff2ad0e2009-04-27 16:51:45 +00001655 :param canvwidth: positive integer, new width of canvas in pixels
1656 :param canvheight: positive integer, new height of canvas in pixels
1657 :param bg: colorstring or color-tuple, new background color
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001658
1659 If no arguments are given, return current (canvaswidth, canvasheight). Else
1660 resize the canvas the turtles are drawing on. Do not alter the drawing
1661 window. To observe hidden parts of the canvas, use the scrollbars. With this
1662 method, one can make visible those parts of a drawing which were outside the
1663 canvas before.
1664
R. David Murrayf877feb2009-05-05 02:08:52 +00001665 >>> screen.screensize()
1666 (400, 300)
1667 >>> screen.screensize(2000,1500)
1668 >>> screen.screensize()
1669 (2000, 1500)
1670
1671 e.g. to search for an erroneously escaped turtle ;-)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001672
1673
1674.. function:: setworldcoordinates(llx, lly, urx, ury)
1675
1676 :param llx: a number, x-coordinate of lower left corner of canvas
1677 :param lly: a number, y-coordinate of lower left corner of canvas
1678 :param urx: a number, x-coordinate of upper right corner of canvas
1679 :param ury: a number, y-coordinate of upper right corner of canvas
1680
1681 Set up user-defined coordinate system and switch to mode "world" if
1682 necessary. This performs a ``screen.reset()``. If mode "world" is already
1683 active, all drawings are redrawn according to the new coordinates.
1684
1685 **ATTENTION**: in user-defined coordinate systems angles may appear
1686 distorted.
1687
R. David Murrayf877feb2009-05-05 02:08:52 +00001688 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001689 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001690
1691 >>> screen.reset()
1692 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1693 >>> for _ in range(72):
1694 ... left(10)
1695 ...
1696 >>> for _ in range(8):
1697 ... left(45); fd(2) # a regular octagon
1698
1699 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001700 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001701 :hide:
1702
1703 >>> screen.reset()
1704 >>> for t in turtles():
1705 ... t.reset()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001706
1707
1708Animation control
1709-----------------
1710
1711.. function:: delay(delay=None)
1712
1713 :param delay: positive integer
1714
1715 Set or return the drawing *delay* in milliseconds. (This is approximately
Georg Brandl2ee470f2008-07-16 12:55:28 +00001716 the time interval between two consecutive canvas updates.) The longer the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001717 drawing delay, the slower the animation.
1718
1719 Optional argument:
1720
R. David Murrayf877feb2009-05-05 02:08:52 +00001721 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001722 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001723
1724 >>> screen.delay()
1725 10
1726 >>> screen.delay(5)
1727 >>> screen.delay()
1728 5
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001729
1730
1731.. function:: tracer(n=None, delay=None)
1732
1733 :param n: nonnegative integer
1734 :param delay: nonnegative integer
1735
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001736 Turn turtle animation on/off and set delay for update drawings. If
1737 *n* is given, only each n-th regular screen update is really
1738 performed. (Can be used to accelerate the drawing of complex
1739 graphics.) When called without arguments, returns the currently
1740 stored value of n. Second argument sets delay value (see
1741 :func:`delay`).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001742
R. David Murrayf877feb2009-05-05 02:08:52 +00001743 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001744 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001745
1746 >>> screen.tracer(8, 25)
1747 >>> dist = 2
1748 >>> for i in range(200):
1749 ... fd(dist)
1750 ... rt(90)
1751 ... dist += 2
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001752
1753
1754.. function:: update()
1755
1756 Perform a TurtleScreen update. To be used when tracer is turned off.
1757
1758See also the RawTurtle/Turtle method :func:`speed`.
1759
1760
1761Using screen events
1762-------------------
1763
1764.. function:: listen(xdummy=None, ydummy=None)
1765
1766 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1767 are provided in order to be able to pass :func:`listen` to the onclick method.
1768
1769
1770.. function:: onkey(fun, key)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001771 onkeyrelease(fun, key)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001772
1773 :param fun: a function with no arguments or ``None``
1774 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1775
1776 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1777 are removed. Remark: in order to be able to register key-events, TurtleScreen
1778 must have the focus. (See method :func:`listen`.)
1779
R. David Murrayf877feb2009-05-05 02:08:52 +00001780 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001781 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001782
1783 >>> def f():
1784 ... fd(50)
1785 ... lt(60)
1786 ...
1787 >>> screen.onkey(f, "Up")
1788 >>> screen.listen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001789
1790
R. David Murray7fcd3de2009-06-25 14:26:19 +00001791.. function:: onkeypress(fun, key=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001792
1793 :param fun: a function with no arguments or ``None``
1794 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1795
1796 Bind *fun* to key-press event of key if key is given,
1797 or to any key-press-event if no key is given.
1798 Remark: in order to be able to register key-events, TurtleScreen
1799 must have focus. (See method :func:`listen`.)
1800
1801 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001802 :skipif: _tkinter is None
Georg Brandleaa84ef2009-05-05 08:14:33 +00001803
1804 >>> def f():
1805 ... fd(50)
1806 ...
1807 >>> screen.onkey(f, "Up")
1808 >>> screen.listen()
1809
1810
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001811.. function:: onclick(fun, btn=1, add=None)
1812 onscreenclick(fun, btn=1, add=None)
1813
1814 :param fun: a function with two arguments which will be called with the
1815 coordinates of the clicked point on the canvas
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)4edeaea2018-11-16 18:58:51 +05301816 :param btn: number of the mouse-button, defaults to 1 (left mouse button)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001817 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1818 added, otherwise it will replace a former binding
1819
1820 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1821 existing bindings are removed.
1822
1823 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1824 named turtle:
1825
R. David Murrayf877feb2009-05-05 02:08:52 +00001826 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001827 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001828
1829 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1830 >>> # make the turtle move to the clicked point.
1831 >>> screen.onclick(None) # remove event binding again
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001832
1833 .. note::
1834 This TurtleScreen method is available as a global function only under the
1835 name ``onscreenclick``. The global function ``onclick`` is another one
1836 derived from the Turtle method ``onclick``.
1837
1838
1839.. function:: ontimer(fun, t=0)
1840
1841 :param fun: a function with no arguments
1842 :param t: a number >= 0
1843
1844 Install a timer that calls *fun* after *t* milliseconds.
1845
R. David Murrayf877feb2009-05-05 02:08:52 +00001846 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001847 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001848
1849 >>> running = True
1850 >>> def f():
1851 ... if running:
1852 ... fd(50)
1853 ... lt(60)
1854 ... screen.ontimer(f, 250)
1855 >>> f() ### makes the turtle march around
1856 >>> running = False
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001857
1858
Georg Brandleaa84ef2009-05-05 08:14:33 +00001859.. function:: mainloop()
Sandro Tosie3484552011-10-31 10:12:43 +01001860 done()
Georg Brandleaa84ef2009-05-05 08:14:33 +00001861
1862 Starts event loop - calling Tkinter's mainloop function.
1863 Must be the last statement in a turtle graphics program.
1864 Must *not* be used if a script is run from within IDLE in -n mode
1865 (No subprocess) - for interactive use of turtle graphics. ::
1866
1867 >>> screen.mainloop()
1868
1869
1870Input methods
1871-------------
1872
1873.. function:: textinput(title, prompt)
1874
1875 :param title: string
1876 :param prompt: string
1877
1878 Pop up a dialog window for input of a string. Parameter title is
delirious-lettuce3378b202017-05-19 14:37:57 -06001879 the title of the dialog window, prompt is a text mostly describing
Georg Brandleaa84ef2009-05-05 08:14:33 +00001880 what information to input.
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03001881 Return the string input. If the dialog is canceled, return ``None``. ::
Georg Brandleaa84ef2009-05-05 08:14:33 +00001882
1883 >>> screen.textinput("NIM", "Name of first player:")
1884
1885
R. David Murray7fcd3de2009-06-25 14:26:19 +00001886.. function:: numinput(title, prompt, default=None, minval=None, maxval=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001887
1888 :param title: string
1889 :param prompt: string
1890 :param default: number (optional)
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001891 :param minval: number (optional)
1892 :param maxval: number (optional)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001893
1894 Pop up a dialog window for input of a number. title is the title of the
1895 dialog window, prompt is a text mostly describing what numerical information
Donald Stufft8b852f12014-05-20 12:58:38 -04001896 to input. default: default value, minval: minimum value for input,
Georg Brandleaa84ef2009-05-05 08:14:33 +00001897 maxval: maximum value for input
1898 The number input must be in the range minval .. maxval if these are
1899 given. If not, a hint is issued and the dialog remains open for
1900 correction.
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03001901 Return the number input. If the dialog is canceled, return ``None``. ::
Georg Brandleaa84ef2009-05-05 08:14:33 +00001902
1903 >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000)
1904
1905
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001906Settings and special methods
1907----------------------------
1908
1909.. function:: mode(mode=None)
1910
1911 :param mode: one of the strings "standard", "logo" or "world"
1912
1913 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1914 is not given, current mode is returned.
1915
1916 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1917 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1918 "world coordinates". **Attention**: in this mode angles appear distorted if
1919 ``x/y`` unit-ratio doesn't equal 1.
1920
1921 ============ ========================= ===================
1922 Mode Initial turtle heading positive angles
1923 ============ ========================= ===================
1924 "standard" to the right (east) counterclockwise
1925 "logo" upward (north) clockwise
1926 ============ ========================= ===================
1927
R. David Murrayf877feb2009-05-05 02:08:52 +00001928 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001929 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001930
1931 >>> mode("logo") # resets turtle heading to north
1932 >>> mode()
1933 'logo'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001934
1935
1936.. function:: colormode(cmode=None)
1937
1938 :param cmode: one of the values 1.0 or 255
1939
1940 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1941 values of color triples have to be in the range 0..\ *cmode*.
1942
R. David Murrayf877feb2009-05-05 02:08:52 +00001943 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001944 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001945
1946 >>> screen.colormode(1)
1947 >>> turtle.pencolor(240, 160, 80)
1948 Traceback (most recent call last):
1949 ...
1950 TurtleGraphicsError: bad color sequence: (240, 160, 80)
1951 >>> screen.colormode()
1952 1.0
1953 >>> screen.colormode(255)
1954 >>> screen.colormode()
1955 255
1956 >>> turtle.pencolor(240,160,80)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001957
1958
1959.. function:: getcanvas()
1960
1961 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1962 do with a Tkinter Canvas.
1963
R. David Murrayf877feb2009-05-05 02:08:52 +00001964 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001965 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001966
1967 >>> cv = screen.getcanvas()
1968 >>> cv
Serhiy Storchakabcc17462014-04-04 15:45:02 +03001969 <turtle.ScrolledCanvas object ...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001970
1971
1972.. function:: getshapes()
1973
1974 Return a list of names of all currently available turtle shapes.
1975
R. David Murrayf877feb2009-05-05 02:08:52 +00001976 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02001977 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00001978
1979 >>> screen.getshapes()
1980 ['arrow', 'blank', 'circle', ..., 'turtle']
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001981
1982
1983.. function:: register_shape(name, shape=None)
1984 addshape(name, shape=None)
1985
1986 There are three different ways to call this function:
1987
1988 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
R. David Murrayf877feb2009-05-05 02:08:52 +00001989 corresponding image shape. ::
1990
1991 >>> screen.register_shape("turtle.gif")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001992
1993 .. note::
1994 Image shapes *do not* rotate when turning the turtle, so they do not
1995 display the heading of the turtle!
1996
1997 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1998 coordinates: Install the corresponding polygon shape.
1999
R. David Murrayf877feb2009-05-05 02:08:52 +00002000 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02002001 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00002002
2003 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
2004
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002005 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
2006 object: Install the corresponding compound shape.
2007
2008 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
2009 shapes can be used by issuing the command ``shape(shapename)``.
2010
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002011
2012.. function:: turtles()
2013
2014 Return the list of turtles on the screen.
2015
R. David Murrayf877feb2009-05-05 02:08:52 +00002016 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02002017 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00002018
2019 >>> for turtle in screen.turtles():
2020 ... turtle.color("red")
Georg Brandl116aa622007-08-15 14:28:22 +00002021
Georg Brandl116aa622007-08-15 14:28:22 +00002022
2023.. function:: window_height()
2024
R. David Murrayf877feb2009-05-05 02:08:52 +00002025 Return the height of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002026
R. David Murrayf877feb2009-05-05 02:08:52 +00002027 >>> screen.window_height()
2028 480
Georg Brandl116aa622007-08-15 14:28:22 +00002029
Georg Brandl116aa622007-08-15 14:28:22 +00002030
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002031.. function:: window_width()
2032
R. David Murrayf877feb2009-05-05 02:08:52 +00002033 Return the width of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002034
R. David Murrayf877feb2009-05-05 02:08:52 +00002035 >>> screen.window_width()
2036 640
Georg Brandl116aa622007-08-15 14:28:22 +00002037
2038
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002039.. _screenspecific:
Georg Brandl116aa622007-08-15 14:28:22 +00002040
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002041Methods specific to Screen, not inherited from TurtleScreen
2042-----------------------------------------------------------
2043
2044.. function:: bye()
2045
2046 Shut the turtlegraphics window.
Georg Brandl116aa622007-08-15 14:28:22 +00002047
2048
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002049.. function:: exitonclick()
Georg Brandl116aa622007-08-15 14:28:22 +00002050
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002051 Bind bye() method to mouse clicks on the Screen.
Georg Brandl116aa622007-08-15 14:28:22 +00002052
2053
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002054 If the value "using_IDLE" in the configuration dictionary is ``False``
2055 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
2056 (no subprocess) is used, this value should be set to ``True`` in
2057 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
2058 client script.
Georg Brandl116aa622007-08-15 14:28:22 +00002059
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002060
2061.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
2062
2063 Set the size and position of the main window. Default values of arguments
Georg Brandl6faee4e2010-09-21 14:48:28 +00002064 are stored in the configuration dictionary and can be changed via a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002065 :file:`turtle.cfg` file.
2066
2067 :param width: if an integer, a size in pixels, if a float, a fraction of the
2068 screen; default is 50% of screen
2069 :param height: if an integer, the height in pixels, if a float, a fraction of
2070 the screen; default is 75% of screen
2071 :param startx: if positive, starting position in pixels from the left
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03002072 edge of the screen, if negative from the right edge, if ``None``,
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002073 center window horizontally
Zachary Ware8faecbf2014-07-16 14:48:48 -05002074 :param starty: if positive, starting position in pixels from the top
Serhiy Storchakaecf41da2016-10-19 16:29:26 +03002075 edge of the screen, if negative from the bottom edge, if ``None``,
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002076 center window vertically
2077
R. David Murrayf877feb2009-05-05 02:08:52 +00002078 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02002079 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00002080
2081 >>> screen.setup (width=200, height=200, startx=0, starty=0)
2082 >>> # sets window to 200x200 pixels, in upper left of screen
2083 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
2084 >>> # sets window to 75% of screen by 50% of screen and centers
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002085
2086
2087.. function:: title(titlestring)
2088
2089 :param titlestring: a string that is shown in the titlebar of the turtle
2090 graphics window
2091
2092 Set title of turtle window to *titlestring*.
2093
R. David Murrayf877feb2009-05-05 02:08:52 +00002094 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02002095 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00002096
2097 >>> screen.title("Welcome to the turtle zoo!")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002098
2099
Alexander Belopolsky65095992010-11-01 15:45:34 +00002100Public classes
2101==============
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002102
2103
2104.. class:: RawTurtle(canvas)
2105 RawPen(canvas)
2106
Ezio Melotti1a263ad2010-03-14 09:51:37 +00002107 :param canvas: a :class:`tkinter.Canvas`, a :class:`ScrolledCanvas` or a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002108 :class:`TurtleScreen`
2109
R. David Murrayf877feb2009-05-05 02:08:52 +00002110 Create a turtle. The turtle has all methods described above as "methods of
2111 Turtle/RawTurtle".
Georg Brandl116aa622007-08-15 14:28:22 +00002112
2113
2114.. class:: Turtle()
2115
R. David Murrayf877feb2009-05-05 02:08:52 +00002116 Subclass of RawTurtle, has the same interface but draws on a default
2117 :class:`Screen` object created automatically when needed for the first time.
Georg Brandl116aa622007-08-15 14:28:22 +00002118
2119
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002120.. class:: TurtleScreen(cv)
Georg Brandl116aa622007-08-15 14:28:22 +00002121
Ezio Melotti1a263ad2010-03-14 09:51:37 +00002122 :param cv: a :class:`tkinter.Canvas`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002123
2124 Provides screen oriented methods like :func:`setbg` etc. that are described
2125 above.
2126
2127.. class:: Screen()
2128
2129 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
2130
Georg Brandl48310cd2009-01-03 21:18:54 +00002131
Benjamin Petersona0dfa822009-11-13 02:25:08 +00002132.. class:: ScrolledCanvas(master)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002133
2134 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
2135 a Tkinter-canvas with scrollbars added
2136
2137 Used by class Screen, which thus automatically provides a ScrolledCanvas as
2138 playground for the turtles.
2139
2140.. class:: Shape(type_, data)
2141
2142 :param type\_: one of the strings "polygon", "image", "compound"
2143
2144 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
2145 specification:
Georg Brandl116aa622007-08-15 14:28:22 +00002146
2147
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002148 =========== ===========
2149 *type_* *data*
2150 =========== ===========
2151 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
2152 "image" an image (in this form only used internally!)
Georg Brandlae2dbe22009-03-13 19:04:40 +00002153 "compound" ``None`` (a compound shape has to be constructed using the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002154 :meth:`addcomponent` method)
2155 =========== ===========
Georg Brandl48310cd2009-01-03 21:18:54 +00002156
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002157 .. method:: addcomponent(poly, fill, outline=None)
Georg Brandl116aa622007-08-15 14:28:22 +00002158
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002159 :param poly: a polygon, i.e. a tuple of pairs of numbers
2160 :param fill: a color the *poly* will be filled with
2161 :param outline: a color for the poly's outline (if given)
Georg Brandl48310cd2009-01-03 21:18:54 +00002162
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002163 Example:
Georg Brandl116aa622007-08-15 14:28:22 +00002164
R. David Murrayf877feb2009-05-05 02:08:52 +00002165 .. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02002166 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00002167
2168 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
2169 >>> s = Shape("compound")
2170 >>> s.addcomponent(poly, "red", "blue")
2171 >>> # ... add more components and then use register_shape()
Georg Brandl116aa622007-08-15 14:28:22 +00002172
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002173 See :ref:`compoundshapes`.
Georg Brandl116aa622007-08-15 14:28:22 +00002174
2175
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002176.. class:: Vec2D(x, y)
Georg Brandl116aa622007-08-15 14:28:22 +00002177
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002178 A two-dimensional vector class, used as a helper class for implementing
2179 turtle graphics. May be useful for turtle graphics programs too. Derived
2180 from tuple, so a vector is a tuple!
2181
2182 Provides (for *a*, *b* vectors, *k* number):
2183
2184 * ``a + b`` vector addition
2185 * ``a - b`` vector subtraction
2186 * ``a * b`` inner product
2187 * ``k * a`` and ``a * k`` multiplication with scalar
2188 * ``abs(a)`` absolute value of a
2189 * ``a.rotate(angle)`` rotation
2190
2191
2192Help and configuration
2193======================
2194
2195How to use help
2196---------------
2197
2198The public methods of the Screen and Turtle classes are documented extensively
2199via docstrings. So these can be used as online-help via the Python help
2200facilities:
2201
2202- When using IDLE, tooltips show the signatures and first lines of the
2203 docstrings of typed in function-/method calls.
2204
2205- Calling :func:`help` on methods or functions displays the docstrings::
2206
2207 >>> help(Screen.bgcolor)
2208 Help on method bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002209
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002210 bgcolor(self, *args) unbound turtle.Screen method
2211 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002212
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002213 Arguments (if given): a color string or three numbers
2214 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002215
2216
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002217 >>> screen.bgcolor("orange")
2218 >>> screen.bgcolor()
2219 "orange"
2220 >>> screen.bgcolor(0.5,0,0.5)
2221 >>> screen.bgcolor()
2222 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002223
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002224 >>> help(Turtle.penup)
2225 Help on method penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002226
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002227 penup(self) unbound turtle.Turtle method
2228 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002229
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002230 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002231
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002232 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002233
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002234 >>> turtle.penup()
2235
2236- The docstrings of the functions which are derived from methods have a modified
2237 form::
2238
2239 >>> help(bgcolor)
2240 Help on function bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002241
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002242 bgcolor(*args)
2243 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002244
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002245 Arguments (if given): a color string or three numbers
2246 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002247
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002248 Example::
Georg Brandl48310cd2009-01-03 21:18:54 +00002249
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002250 >>> bgcolor("orange")
2251 >>> bgcolor()
2252 "orange"
2253 >>> bgcolor(0.5,0,0.5)
2254 >>> bgcolor()
2255 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002256
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002257 >>> help(penup)
2258 Help on function penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002259
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002260 penup()
2261 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002262
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002263 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002264
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002265 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002266
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002267 Example:
2268 >>> penup()
2269
2270These modified docstrings are created automatically together with the function
2271definitions that are derived from the methods at import time.
2272
2273
2274Translation of docstrings into different languages
2275--------------------------------------------------
2276
2277There is a utility to create a dictionary the keys of which are the method names
2278and the values of which are the docstrings of the public methods of the classes
2279Screen and Turtle.
2280
2281.. function:: write_docstringdict(filename="turtle_docstringdict")
2282
2283 :param filename: a string, used as filename
2284
2285 Create and write docstring-dictionary to a Python script with the given
2286 filename. This function has to be called explicitly (it is not used by the
2287 turtle graphics classes). The docstring dictionary will be written to the
2288 Python script :file:`{filename}.py`. It is intended to serve as a template
2289 for translation of the docstrings into different languages.
2290
2291If you (or your students) want to use :mod:`turtle` with online help in your
2292native language, you have to translate the docstrings and save the resulting
2293file as e.g. :file:`turtle_docstringdict_german.py`.
2294
2295If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2296will be read in at import time and will replace the original English docstrings.
2297
2298At the time of this writing there are docstring dictionaries in German and in
2299Italian. (Requests please to glingl@aon.at.)
2300
2301
2302
2303How to configure Screen and Turtles
2304-----------------------------------
2305
2306The built-in default configuration mimics the appearance and behaviour of the
2307old turtle module in order to retain best possible compatibility with it.
2308
2309If you want to use a different configuration which better reflects the features
2310of this module or which better fits to your needs, e.g. for use in a classroom,
2311you can prepare a configuration file ``turtle.cfg`` which will be read at import
2312time and modify the configuration according to its settings.
2313
2314The built in configuration would correspond to the following turtle.cfg::
2315
2316 width = 0.5
2317 height = 0.75
2318 leftright = None
2319 topbottom = None
2320 canvwidth = 400
2321 canvheight = 300
2322 mode = standard
2323 colormode = 1.0
2324 delay = 10
2325 undobuffersize = 1000
2326 shape = classic
2327 pencolor = black
2328 fillcolor = black
2329 resizemode = noresize
2330 visible = True
2331 language = english
2332 exampleturtle = turtle
2333 examplescreen = screen
2334 title = Python Turtle Graphics
2335 using_IDLE = False
2336
2337Short explanation of selected entries:
2338
2339- The first four lines correspond to the arguments of the :meth:`Screen.setup`
2340 method.
2341- Line 5 and 6 correspond to the arguments of the method
2342 :meth:`Screen.screensize`.
2343- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2344 info try ``help(shape)``.
2345- If you want to use no fillcolor (i.e. make the turtle transparent), you have
2346 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
2347 the cfg-file).
2348- If you want to reflect the turtle its state, you have to use ``resizemode =
2349 auto``.
2350- If you set e.g. ``language = italian`` the docstringdict
2351 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
2352 present on the import path, e.g. in the same directory as :mod:`turtle`.
2353- The entries *exampleturtle* and *examplescreen* define the names of these
2354 objects as they occur in the docstrings. The transformation of
2355 method-docstrings to function-docstrings will delete these names from the
2356 docstrings.
2357- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
2358 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
2359 mainloop.
2360
2361There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2362stored and an additional one in the current working directory. The latter will
2363override the settings of the first one.
2364
Georg Brandl59b44722010-12-30 22:12:40 +00002365The :file:`Lib/turtledemo` directory contains a :file:`turtle.cfg` file. You can
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002366study it as an example and see its effects when running the demos (preferably
2367not from within the demo-viewer).
2368
2369
Terry Jan Reedy6e978d22014-10-02 00:16:31 -04002370:mod:`turtledemo` --- Demo scripts
2371==================================
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002372
Terry Jan Reedy6e978d22014-10-02 00:16:31 -04002373.. module:: turtledemo
2374 :synopsis: A viewer for example turtle scripts
2375
2376The :mod:`turtledemo` package includes a set of demo scripts. These
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002377scripts can be run and viewed using the supplied demo viewer as follows::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002378
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002379 python -m turtledemo
2380
Alexander Belopolskye1f849c2010-11-09 03:13:43 +00002381Alternatively, you can run the demo scripts individually. For example, ::
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002382
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002383 python -m turtledemo.bytedesign
2384
2385The :mod:`turtledemo` package directory contains:
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002386
Terry Jan Reedy6e978d22014-10-02 00:16:31 -04002387- A demo viewer :file:`__main__.py` which can be used to view the sourcecode
2388 of the scripts and run them at the same time.
2389- Multiple scripts demonstrating different features of the :mod:`turtle`
2390 module. Examples can be accessed via the Examples menu. They can also
2391 be run standalone.
2392- A :file:`turtle.cfg` file which serves as an example of how to write
2393 and use such files.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002394
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002395The demo scripts are:
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002396
Georg Brandl44ea77b2013-03-28 13:28:44 +01002397.. tabularcolumns:: |l|L|L|
2398
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002399+----------------+------------------------------+-----------------------+
2400| Name | Description | Features |
Georg Brandl44ea77b2013-03-28 13:28:44 +01002401+================+==============================+=======================+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002402| bytedesign | complex classical | :func:`tracer`, delay,|
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002403| | turtle graphics pattern | :func:`update` |
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002404+----------------+------------------------------+-----------------------+
Georg Brandlda227192011-03-06 10:53:55 +01002405| chaos | graphs Verhulst dynamics, | world coordinates |
2406| | shows that computer's | |
2407| | computations can generate | |
2408| | results sometimes against the| |
2409| | common sense expectations | |
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002410+----------------+------------------------------+-----------------------+
2411| clock | analog clock showing time | turtles as clock's |
2412| | of your computer | hands, ontimer |
2413+----------------+------------------------------+-----------------------+
2414| colormixer | experiment with r, g, b | :func:`ondrag` |
2415+----------------+------------------------------+-----------------------+
Terry Jan Reedy6e978d22014-10-02 00:16:31 -04002416| forest | 3 breadth-first trees | randomization |
2417+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002418| fractalcurves | Hilbert & Koch curves | recursion |
2419+----------------+------------------------------+-----------------------+
2420| lindenmayer | ethnomathematics | L-System |
2421| | (indian kolams) | |
2422+----------------+------------------------------+-----------------------+
2423| minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
2424| | | as Hanoi discs |
2425| | | (shape, shapesize) |
2426+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002427| nim | play the classical nim game | turtles as nimsticks, |
2428| | with three heaps of sticks | event driven (mouse, |
2429| | against the computer. | keyboard) |
2430+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002431| paint | super minimalistic | :func:`onclick` |
2432| | drawing program | |
2433+----------------+------------------------------+-----------------------+
2434| peace | elementary | turtle: appearance |
2435| | | and animation |
2436+----------------+------------------------------+-----------------------+
2437| penrose | aperiodic tiling with | :func:`stamp` |
2438| | kites and darts | |
2439+----------------+------------------------------+-----------------------+
2440| planet_and_moon| simulation of | compound shapes, |
2441| | gravitational system | :class:`Vec2D` |
2442+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002443| round_dance | dancing turtles rotating | compound shapes, clone|
2444| | pairwise in opposite | shapesize, tilt, |
Alexander Belopolskyc08f5442010-10-21 22:29:36 +00002445| | direction | get_shapepoly, update |
Georg Brandleaa84ef2009-05-05 08:14:33 +00002446+----------------+------------------------------+-----------------------+
Ethan Furman738f8052015-03-02 12:29:58 -08002447| sorting_animate| visual demonstration of | simple alignment, |
2448| | different sorting methods | randomization |
2449+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002450| tree | a (graphical) breadth | :func:`clone` |
2451| | first tree (using generators)| |
2452+----------------+------------------------------+-----------------------+
Terry Jan Reedy6e978d22014-10-02 00:16:31 -04002453| two_canvases | simple design | turtles on two |
2454| | | canvases |
2455+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002456| wikipedia | a pattern from the wikipedia | :func:`clone`, |
2457| | article on turtle graphics | :func:`undo` |
2458+----------------+------------------------------+-----------------------+
靳阳fff2a212017-07-20 21:58:40 +08002459| yinyang | another elementary example | :func:`circle` |
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002460+----------------+------------------------------+-----------------------+
2461
2462Have fun!
2463
2464
2465Changes since Python 2.6
2466========================
2467
Georg Brandl48310cd2009-01-03 21:18:54 +00002468- The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and
2469 :meth:`Turtle.window_height` have been eliminated.
2470 Methods with these names and functionality are now available only
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002471 as methods of :class:`Screen`. The functions derived from these remain
Georg Brandl48310cd2009-01-03 21:18:54 +00002472 available. (In fact already in Python 2.6 these methods were merely
2473 duplications of the corresponding
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002474 :class:`TurtleScreen`/:class:`Screen`-methods.)
2475
Georg Brandl48310cd2009-01-03 21:18:54 +00002476- The method :meth:`Turtle.fill` has been eliminated.
2477 The behaviour of :meth:`begin_fill` and :meth:`end_fill`
2478 have changed slightly: now every filling-process must be completed with an
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002479 ``end_fill()`` call.
Georg Brandl48310cd2009-01-03 21:18:54 +00002480
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002481- A method :meth:`Turtle.filling` has been added. It returns a boolean
2482 value: ``True`` if a filling process is under way, ``False`` otherwise.
2483 This behaviour corresponds to a ``fill()`` call without arguments in
Georg Brandl23d11d32008-09-21 07:50:52 +00002484 Python 2.6.
Georg Brandl116aa622007-08-15 14:28:22 +00002485
Georg Brandleaa84ef2009-05-05 08:14:33 +00002486Changes since Python 3.0
2487========================
2488
2489- The methods :meth:`Turtle.shearfactor`, :meth:`Turtle.shapetransform` and
2490 :meth:`Turtle.get_shapepoly` have been added. Thus the full range of
2491 regular linear transforms is now available for transforming turtle shapes.
2492 :meth:`Turtle.tiltangle` has been enhanced in functionality: it now can
2493 be used to get or set the tiltangle. :meth:`Turtle.settiltangle` has been
2494 deprecated.
2495
2496- The method :meth:`Screen.onkeypress` has been added as a complement to
2497 :meth:`Screen.onkey` which in fact binds actions to the keyrelease event.
2498 Accordingly the latter has got an alias: :meth:`Screen.onkeyrelease`.
2499
2500- The method :meth:`Screen.mainloop` has been added. So when working only
Donald Stufft8b852f12014-05-20 12:58:38 -04002501 with Screen and Turtle objects one must not additionally import
Georg Brandleaa84ef2009-05-05 08:14:33 +00002502 :func:`mainloop` anymore.
2503
2504- Two input methods has been added :meth:`Screen.textinput` and
2505 :meth:`Screen.numinput`. These popup input dialogs and return
2506 strings and numbers respectively.
2507
2508- Two example scripts :file:`tdemo_nim.py` and :file:`tdemo_round_dance.py`
Georg Brandl59b44722010-12-30 22:12:40 +00002509 have been added to the :file:`Lib/turtledemo` directory.
Georg Brandleaa84ef2009-05-05 08:14:33 +00002510
R. David Murrayf877feb2009-05-05 02:08:52 +00002511
2512.. doctest::
Stéphane Wirtel859c0682018-10-12 09:51:05 +02002513 :skipif: _tkinter is None
R. David Murrayf877feb2009-05-05 02:08:52 +00002514 :hide:
2515
2516 >>> for turtle in turtles():
2517 ... turtle.reset()
2518 >>> turtle.penup()
2519 >>> turtle.goto(-200,25)
2520 >>> turtle.pendown()
2521 >>> turtle.write("No one expects the Spanish Inquisition!",
2522 ... font=("Arial", 20, "normal"))
2523 >>> turtle.penup()
2524 >>> turtle.goto(-100,-50)
2525 >>> turtle.pendown()
2526 >>> turtle.write("Our two chief Turtles are...",
2527 ... font=("Arial", 16, "normal"))
2528 >>> turtle.penup()
2529 >>> turtle.goto(-450,-75)
2530 >>> turtle.write(str(turtles()))