Home | History | Annotate | Download | only in library
      1 =================================
      2 :mod:`turtle` --- Turtle graphics
      3 =================================
      4 
      5 .. module:: turtle
      6    :synopsis: An educational framework for simple graphics applications
      7 
      8 .. sectionauthor:: Gregor Lingl <gregor.lingl (a] aon.at>
      9 
     10 **Source code:** :source:`Lib/turtle.py`
     11 
     12 .. testsetup:: default
     13 
     14    from turtle import *
     15    turtle = Turtle()
     16 
     17 --------------
     18 
     19 Introduction
     20 ============
     21 
     22 Turtle graphics is a popular way for introducing programming to kids.  It was
     23 part of the original Logo programming language developed by Wally Feurzig and
     24 Seymour Papert in 1966.
     25 
     26 Imagine a robotic turtle starting at (0, 0) in the x-y plane.  After an ``import turtle``, give it the
     27 command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
     28 direction it is facing, drawing a line as it moves.  Give it the command
     29 ``turtle.right(25)``, and it rotates in-place 25 degrees clockwise.
     30 
     31 .. 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 
     41 By combining together these and similar commands, intricate shapes and pictures
     42 can easily be drawn.
     43 
     44 The :mod:`turtle` module is an extended reimplementation of the same-named
     45 module from the Python standard distribution up to version Python 2.5.
     46 
     47 It tries to keep the merits of the old turtle module and to be (nearly) 100%
     48 compatible with it.  This means in the first place to enable the learning
     49 programmer to use all the commands, classes and methods interactively when using
     50 the module from within IDLE run with the ``-n`` switch.
     51 
     52 The turtle module provides turtle graphics primitives, in both object-oriented
     53 and procedure-oriented ways.  Because it uses :mod:`tkinter` for the underlying
     54 graphics, it needs a version of Python installed with Tk support.
     55 
     56 The object-oriented interface uses essentially two+two classes:
     57 
     58 1. The :class:`TurtleScreen` class defines graphics windows as a playground for
     59    the drawing turtles.  Its constructor needs a :class:`tkinter.Canvas` or a
     60    :class:`ScrolledCanvas` as argument.  It should be used when :mod:`turtle` is
     61    used as part of some application.
     62 
     63    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.
     67 
     68    All methods of TurtleScreen/Screen also exist as functions, i.e. as part of
     69    the procedure-oriented interface.
     70 
     71 2. :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`),
     76    which draws on "the" :class:`Screen` instance which is automatically
     77    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 
     82 The procedural interface provides functions which are derived from the methods
     83 of the classes :class:`Screen` and :class:`Turtle`.  They have the same names as
     84 the corresponding methods.  A screen object is automatically created whenever a
     85 function derived from a Screen method is called.  An (unnamed) turtle object is
     86 automatically created whenever any of the functions derived from a Turtle method
     87 is called.
     88 
     89 To use multiple turtles on a screen one has to use the object-oriented interface.
     90 
     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.
     95 
     96 
     97 Overview of available Turtle and Screen methods
     98 =================================================
     99 
    100 Turtle methods
    101 --------------
    102 
    103 Turtle 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 
    134 Pen 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 
    157 Turtle 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`
    167       | :func:`shearfactor`
    168       | :func:`settiltangle`
    169       | :func:`tiltangle`
    170       | :func:`tilt`
    171       | :func:`shapetransform`
    172       | :func:`get_shapepoly`
    173 
    174 Using events
    175    | :func:`onclick`
    176    | :func:`onrelease`
    177    | :func:`ondrag`
    178 
    179 Special 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`
    188 
    189 
    190 Methods of TurtleScreen/Screen
    191 ------------------------------
    192 
    193 Window control
    194    | :func:`bgcolor`
    195    | :func:`bgpic`
    196    | :func:`clear` | :func:`clearscreen`
    197    | :func:`reset` | :func:`resetscreen`
    198    | :func:`screensize`
    199    | :func:`setworldcoordinates`
    200 
    201 Animation control
    202    | :func:`delay`
    203    | :func:`tracer`
    204    | :func:`update`
    205 
    206 Using screen events
    207    | :func:`listen`
    208    | :func:`onkey` | :func:`onkeyrelease`
    209    | :func:`onkeypress`
    210    | :func:`onclick` | :func:`onscreenclick`
    211    | :func:`ontimer`
    212    | :func:`mainloop` | :func:`done`
    213 
    214 Settings 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 
    224 Input methods
    225    | :func:`textinput`
    226    | :func:`numinput`
    227 
    228 Methods specific to Screen
    229    | :func:`bye`
    230    | :func:`exitonclick`
    231    | :func:`setup`
    232    | :func:`title`
    233 
    234 
    235 Methods of RawTurtle/Turtle and corresponding functions
    236 =======================================================
    237 
    238 Most of the examples in this section refer to a Turtle instance called
    239 ``turtle``.
    240 
    241 Turtle motion
    242 -------------
    243 
    244 .. function:: forward(distance)
    245               fd(distance)
    246 
    247    :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 
    252    .. doctest::
    253 
    254       >>> turtle.position()
    255       (0.00,0.00)
    256       >>> turtle.forward(25)
    257       >>> turtle.position()
    258       (25.00,0.00)
    259       >>> turtle.forward(-75)
    260       >>> turtle.position()
    261       (-50.00,0.00)
    262 
    263 
    264 .. function:: back(distance)
    265               bk(distance)
    266               backward(distance)
    267 
    268    :param distance: a number
    269 
    270    Move the turtle backward by *distance*, opposite to the direction the
    271    turtle is headed.  Do not change the turtle's heading.
    272 
    273    .. doctest::
    274       :hide:
    275 
    276       >>> turtle.goto(0, 0)
    277 
    278    .. doctest::
    279 
    280       >>> turtle.position()
    281       (0.00,0.00)
    282       >>> turtle.backward(30)
    283       >>> turtle.position()
    284       (-30.00,0.00)
    285 
    286 
    287 .. function:: right(angle)
    288               rt(angle)
    289 
    290    :param angle: a number (integer or float)
    291 
    292    Turn turtle right by *angle* units.  (Units are by default degrees, but
    293    can be set via the :func:`degrees` and :func:`radians` functions.)  Angle
    294    orientation depends on the turtle mode, see :func:`mode`.
    295 
    296    .. doctest::
    297       :hide:
    298 
    299       >>> turtle.setheading(22)
    300 
    301    .. doctest::
    302 
    303       >>> turtle.heading()
    304       22.0
    305       >>> turtle.right(45)
    306       >>> turtle.heading()
    307       337.0
    308 
    309 
    310 .. function:: left(angle)
    311               lt(angle)
    312 
    313    :param angle: a number (integer or float)
    314 
    315    Turn turtle left by *angle* units.  (Units are by default degrees, but
    316    can be set via the :func:`degrees` and :func:`radians` functions.)  Angle
    317    orientation depends on the turtle mode, see :func:`mode`.
    318 
    319    .. doctest::
    320       :hide:
    321 
    322       >>> turtle.setheading(22)
    323 
    324    .. doctest::
    325 
    326       >>> turtle.heading()
    327       22.0
    328       >>> turtle.left(45)
    329       >>> turtle.heading()
    330       67.0
    331 
    332 
    333 .. function:: goto(x, y=None)
    334               setpos(x, y=None)
    335               setposition(x, y=None)
    336 
    337    :param x: a number or a pair/vector of numbers
    338    :param y: a number or ``None``
    339 
    340    If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D`
    341    (e.g. as returned by :func:`pos`).
    342 
    343    Move turtle to an absolute position.  If the pen is down, draw line.  Do
    344    not change the turtle's orientation.
    345 
    346    .. doctest::
    347       :hide:
    348 
    349       >>> turtle.goto(0, 0)
    350 
    351    .. doctest::
    352 
    353        >>> tp = turtle.pos()
    354        >>> tp
    355        (0.00,0.00)
    356        >>> turtle.setpos(60,30)
    357        >>> turtle.pos()
    358        (60.00,30.00)
    359        >>> turtle.setpos((20,80))
    360        >>> turtle.pos()
    361        (20.00,80.00)
    362        >>> turtle.setpos(tp)
    363        >>> turtle.pos()
    364        (0.00,0.00)
    365 
    366 
    367 .. function:: setx(x)
    368 
    369    :param x: a number (integer or float)
    370 
    371    Set the turtle's first coordinate to *x*, leave second coordinate
    372    unchanged.
    373 
    374    .. doctest::
    375       :hide:
    376 
    377       >>> turtle.goto(0, 240)
    378 
    379    .. doctest::
    380 
    381       >>> turtle.position()
    382       (0.00,240.00)
    383       >>> turtle.setx(10)
    384       >>> turtle.position()
    385       (10.00,240.00)
    386 
    387 
    388 .. function:: sety(y)
    389 
    390    :param y: a number (integer or float)
    391 
    392    Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
    393 
    394    .. doctest::
    395       :hide:
    396 
    397       >>> turtle.goto(0, 40)
    398 
    399    .. doctest::
    400 
    401       >>> turtle.position()
    402       (0.00,40.00)
    403       >>> turtle.sety(-10)
    404       >>> turtle.position()
    405       (0.00,-10.00)
    406 
    407 
    408 .. function:: setheading(to_angle)
    409               seth(to_angle)
    410 
    411    :param to_angle: a number (integer or float)
    412 
    413    Set the orientation of the turtle to *to_angle*.  Here are some common
    414    directions in degrees:
    415 
    416    =================== ====================
    417     standard mode           logo mode
    418    =================== ====================
    419       0 - east                0 - north
    420      90 - north              90 - east
    421     180 - west              180 - south
    422     270 - south             270 - west
    423    =================== ====================
    424 
    425    .. doctest::
    426 
    427       >>> turtle.setheading(90)
    428       >>> turtle.heading()
    429       90.0
    430 
    431 
    432 .. function:: home()
    433 
    434    Move turtle to the origin -- coordinates (0,0) -- and set its heading to
    435    its start-orientation (which depends on the mode, see :func:`mode`).
    436 
    437    .. doctest::
    438       :hide:
    439 
    440       >>> turtle.setheading(90)
    441       >>> turtle.goto(0, -10)
    442 
    443    .. doctest::
    444 
    445       >>> turtle.heading()
    446       90.0
    447       >>> turtle.position()
    448       (0.00,-10.00)
    449       >>> turtle.home()
    450       >>> turtle.position()
    451       (0.00,0.00)
    452       >>> turtle.heading()
    453       0.0
    454 
    455 
    456 .. function:: circle(radius, extent=None, steps=None)
    457 
    458    :param radius: a number
    459    :param extent: a number (or ``None``)
    460    :param steps: an integer (or ``None``)
    461 
    462    Draw a circle with given *radius*.  The center is *radius* units left of
    463    the turtle; *extent* -- an angle -- determines which part of the circle
    464    is drawn.  If *extent* is not given, draw the entire circle.  If *extent*
    465    is not a full circle, one endpoint of the arc is the current pen
    466    position.  Draw the arc in counterclockwise direction if *radius* is
    467    positive, otherwise in clockwise direction.  Finally the direction of the
    468    turtle is changed by the amount of *extent*.
    469 
    470    As the circle is approximated by an inscribed regular polygon, *steps*
    471    determines the number of steps to use.  If not given, it will be
    472    calculated automatically.  May be used to draw regular polygons.
    473 
    474    .. doctest::
    475 
    476       >>> turtle.home()
    477       >>> turtle.position()
    478       (0.00,0.00)
    479       >>> turtle.heading()
    480       0.0
    481       >>> turtle.circle(50)
    482       >>> turtle.position()
    483       (-0.00,0.00)
    484       >>> turtle.heading()
    485       0.0
    486       >>> turtle.circle(120, 180)  # draw a semicircle
    487       >>> turtle.position()
    488       (0.00,240.00)
    489       >>> turtle.heading()
    490       180.0
    491 
    492 
    493 .. function:: dot(size=None, *color)
    494 
    495    :param size: an integer >= 1 (if given)
    496    :param color: a colorstring or a numeric color tuple
    497 
    498    Draw a circular dot with diameter *size*, using *color*.  If *size* is
    499    not given, the maximum of pensize+4 and 2*pensize is used.
    500 
    501 
    502    .. doctest::
    503 
    504       >>> turtle.home()
    505       >>> turtle.dot()
    506       >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
    507       >>> turtle.position()
    508       (100.00,-0.00)
    509       >>> turtle.heading()
    510       0.0
    511 
    512 
    513 .. function:: stamp()
    514 
    515    Stamp a copy of the turtle shape onto the canvas at the current turtle
    516    position.  Return a stamp_id for that stamp, which can be used to delete
    517    it by calling ``clearstamp(stamp_id)``.
    518 
    519    .. doctest::
    520 
    521       >>> turtle.color("blue")
    522       >>> turtle.stamp()
    523       11
    524       >>> turtle.fd(50)
    525 
    526 
    527 .. function:: clearstamp(stampid)
    528 
    529    :param stampid: an integer, must be return value of previous
    530                    :func:`stamp` call
    531 
    532    Delete stamp with given *stampid*.
    533 
    534    .. doctest::
    535 
    536       >>> turtle.position()
    537       (150.00,-0.00)
    538       >>> turtle.color("blue")
    539       >>> astamp = turtle.stamp()
    540       >>> turtle.fd(50)
    541       >>> turtle.position()
    542       (200.00,-0.00)
    543       >>> turtle.clearstamp(astamp)
    544       >>> turtle.position()
    545       (200.00,-0.00)
    546 
    547 
    548 .. function:: clearstamps(n=None)
    549 
    550    :param n: an integer (or ``None``)
    551 
    552    Delete all or first/last *n* of turtle's stamps.  If *n* is ``None``, delete
    553    all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete
    554    last *n* stamps.
    555 
    556    .. doctest::
    557 
    558       >>> for i in range(8):
    559       ...     turtle.stamp(); turtle.fd(30)
    560       13
    561       14
    562       15
    563       16
    564       17
    565       18
    566       19
    567       20
    568       >>> turtle.clearstamps(2)
    569       >>> turtle.clearstamps(-2)
    570       >>> turtle.clearstamps()
    571 
    572 
    573 .. function:: undo()
    574 
    575    Undo (repeatedly) the last turtle action(s).  Number of available
    576    undo actions is determined by the size of the undobuffer.
    577 
    578    .. doctest::
    579 
    580       >>> for i in range(4):
    581       ...     turtle.fd(50); turtle.lt(80)
    582       ...
    583       >>> for i in range(8):
    584       ...     turtle.undo()
    585 
    586 
    587 .. function:: speed(speed=None)
    588 
    589    :param speed: an integer in the range 0..10 or a speedstring (see below)
    590 
    591    Set the turtle's speed to an integer value in the range 0..10.  If no
    592    argument is given, return current speed.
    593 
    594    If input is a number greater than 10 or smaller than 0.5, speed is set
    595    to 0.  Speedstrings are mapped to speedvalues as follows:
    596 
    597    * "fastest":  0
    598    * "fast":  10
    599    * "normal":  6
    600    * "slow":  3
    601    * "slowest":  1
    602 
    603    Speeds from 1 to 10 enforce increasingly faster animation of line drawing
    604    and turtle turning.
    605 
    606    Attention: *speed* = 0 means that *no* animation takes
    607    place. forward/back makes turtle jump and likewise left/right make the
    608    turtle turn instantly.
    609 
    610    .. doctest::
    611 
    612       >>> turtle.speed()
    613       3
    614       >>> turtle.speed('normal')
    615       >>> turtle.speed()
    616       6
    617       >>> turtle.speed(9)
    618       >>> turtle.speed()
    619       9
    620 
    621 
    622 Tell Turtle's state
    623 -------------------
    624 
    625 .. function:: position()
    626               pos()
    627 
    628    Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
    629 
    630    .. doctest::
    631 
    632       >>> turtle.pos()
    633       (440.00,-0.00)
    634 
    635 
    636 .. function:: towards(x, y=None)
    637 
    638    :param x: a number or a pair/vector of numbers or a turtle instance
    639    :param y: a number if *x* is a number, else ``None``
    640 
    641    Return the angle between the line from turtle position to position specified
    642    by (x,y), the vector or the other turtle.  This depends on the turtle's start
    643    orientation which depends on the mode - "standard"/"world" or "logo").
    644 
    645    .. doctest::
    646 
    647       >>> turtle.goto(10, 10)
    648       >>> turtle.towards(0,0)
    649       225.0
    650 
    651 
    652 .. function:: xcor()
    653 
    654    Return the turtle's x coordinate.
    655 
    656    .. doctest::
    657 
    658       >>> turtle.home()
    659       >>> turtle.left(50)
    660       >>> turtle.forward(100)
    661       >>> turtle.pos()
    662       (64.28,76.60)
    663       >>> print(round(turtle.xcor(), 5))
    664       64.27876
    665 
    666 
    667 .. function:: ycor()
    668 
    669    Return the turtle's y coordinate.
    670 
    671    .. doctest::
    672 
    673       >>> turtle.home()
    674       >>> turtle.left(60)
    675       >>> turtle.forward(100)
    676       >>> print(turtle.pos())
    677       (50.00,86.60)
    678       >>> print(round(turtle.ycor(), 5))
    679       86.60254
    680 
    681 
    682 .. function:: heading()
    683 
    684    Return the turtle's current heading (value depends on the turtle mode, see
    685    :func:`mode`).
    686 
    687    .. doctest::
    688 
    689       >>> turtle.home()
    690       >>> turtle.left(67)
    691       >>> turtle.heading()
    692       67.0
    693 
    694 
    695 .. function:: distance(x, y=None)
    696 
    697    :param x: a number or a pair/vector of numbers or a turtle instance
    698    :param y: a number if *x* is a number, else ``None``
    699 
    700    Return the distance from the turtle to (x,y), the given vector, or the given
    701    other turtle, in turtle step units.
    702 
    703    .. doctest::
    704 
    705       >>> turtle.home()
    706       >>> turtle.distance(30,40)
    707       50.0
    708       >>> turtle.distance((30,40))
    709       50.0
    710       >>> joe = Turtle()
    711       >>> joe.forward(77)
    712       >>> turtle.distance(joe)
    713       77.0
    714 
    715 
    716 Settings for measurement
    717 ------------------------
    718 
    719 .. function:: degrees(fullcircle=360.0)
    720 
    721    :param fullcircle: a number
    722 
    723    Set angle measurement units, i.e. set number of "degrees" for a full circle.
    724    Default value is 360 degrees.
    725 
    726    .. doctest::
    727 
    728       >>> turtle.home()
    729       >>> turtle.left(90)
    730       >>> turtle.heading()
    731       90.0
    732 
    733       Change angle measurement unit to grad (also known as gon,
    734       grade, or gradian and equals 1/100-th of the right angle.)
    735       >>> turtle.degrees(400.0)
    736       >>> turtle.heading()
    737       100.0
    738       >>> turtle.degrees(360)
    739       >>> turtle.heading()
    740       90.0
    741 
    742 
    743 .. function:: radians()
    744 
    745    Set the angle measurement units to radians.  Equivalent to
    746    ``degrees(2*math.pi)``.
    747 
    748    .. doctest::
    749 
    750       >>> turtle.home()
    751       >>> turtle.left(90)
    752       >>> turtle.heading()
    753       90.0
    754       >>> turtle.radians()
    755       >>> turtle.heading()
    756       1.5707963267948966
    757 
    758    .. doctest::
    759       :hide:
    760 
    761       >>> turtle.degrees(360)
    762 
    763 
    764 Pen control
    765 -----------
    766 
    767 Drawing state
    768 ~~~~~~~~~~~~~
    769 
    770 .. function:: pendown()
    771               pd()
    772               down()
    773 
    774    Pull the pen down -- drawing when moving.
    775 
    776 
    777 .. function:: penup()
    778               pu()
    779               up()
    780 
    781    Pull the pen up -- no drawing when moving.
    782 
    783 
    784 .. function:: pensize(width=None)
    785               width(width=None)
    786 
    787    :param width: a positive number
    788 
    789    Set the line thickness to *width* or return it.  If resizemode is set to
    790    "auto" and turtleshape is a polygon, that polygon is drawn with the same line
    791    thickness.  If no argument is given, the current pensize is returned.
    792 
    793    .. doctest::
    794 
    795       >>> turtle.pensize()
    796       1
    797       >>> turtle.pensize(10)   # from here on lines of width 10 are drawn
    798 
    799 
    800 .. function:: pen(pen=None, **pendict)
    801 
    802    :param pen: a dictionary with some or all of the below listed keys
    803    :param pendict: one or more keyword-arguments with the below listed keys as keywords
    804 
    805    Return or set the pen's attributes in a "pen-dictionary" with the following
    806    key/value pairs:
    807 
    808    * "shown": True/False
    809    * "pendown": True/False
    810    * "pencolor": color-string or color-tuple
    811    * "fillcolor": color-string or color-tuple
    812    * "pensize": positive number
    813    * "speed": number in range 0..10
    814    * "resizemode": "auto" or "user" or "noresize"
    815    * "stretchfactor": (positive number, positive number)
    816    * "outline": positive number
    817    * "tilt": number
    818 
    819    This dictionary can be used as argument for a subsequent call to :func:`pen`
    820    to restore the former pen-state.  Moreover one or more of these attributes
    821    can be provided as keyword-arguments.  This can be used to set several pen
    822    attributes in one statement.
    823 
    824    .. doctest::
    825       :options: +NORMALIZE_WHITESPACE
    826 
    827       >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
    828       >>> sorted(turtle.pen().items())
    829       [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
    830        ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
    831        ('shearfactor', 0.0), ('shown', True), ('speed', 9),
    832        ('stretchfactor', (1.0, 1.0)), ('tilt', 0.0)]
    833       >>> penstate=turtle.pen()
    834       >>> turtle.color("yellow", "")
    835       >>> turtle.penup()
    836       >>> sorted(turtle.pen().items())[:3]
    837       [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow')]
    838       >>> turtle.pen(penstate, fillcolor="green")
    839       >>> sorted(turtle.pen().items())[:3]
    840       [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red')]
    841 
    842 .. function:: isdown()
    843 
    844    Return ``True`` if pen is down, ``False`` if it's up.
    845 
    846    .. doctest::
    847 
    848       >>> turtle.penup()
    849       >>> turtle.isdown()
    850       False
    851       >>> turtle.pendown()
    852       >>> turtle.isdown()
    853       True
    854 
    855 
    856 Color control
    857 ~~~~~~~~~~~~~
    858 
    859 .. function:: pencolor(*args)
    860 
    861    Return or set the pencolor.
    862 
    863    Four input formats are allowed:
    864 
    865    ``pencolor()``
    866       Return the current pencolor as color specification string or
    867       as a tuple (see example).  May be used as input to another
    868       color/pencolor/fillcolor call.
    869 
    870    ``pencolor(colorstring)``
    871       Set pencolor to *colorstring*, which is a Tk color specification string,
    872       such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
    873 
    874    ``pencolor((r, g, b))``
    875       Set pencolor to the RGB color represented by the tuple of *r*, *g*, and
    876       *b*.  Each of *r*, *g*, and *b* must be in the range 0..colormode, where
    877       colormode is either 1.0 or 255 (see :func:`colormode`).
    878 
    879    ``pencolor(r, g, b)``
    880       Set pencolor to the RGB color represented by *r*, *g*, and *b*.  Each of
    881       *r*, *g*, and *b* must be in the range 0..colormode.
    882 
    883     If turtleshape is a polygon, the outline of that polygon is drawn with the
    884     newly set pencolor.
    885 
    886    .. doctest::
    887 
    888        >>> colormode()
    889        1.0
    890        >>> turtle.pencolor()
    891        'red'
    892        >>> turtle.pencolor("brown")
    893        >>> turtle.pencolor()
    894        'brown'
    895        >>> tup = (0.2, 0.8, 0.55)
    896        >>> turtle.pencolor(tup)
    897        >>> turtle.pencolor()
    898        (0.2, 0.8, 0.5490196078431373)
    899        >>> colormode(255)
    900        >>> turtle.pencolor()
    901        (51.0, 204.0, 140.0)
    902        >>> turtle.pencolor('#32c18f')
    903        >>> turtle.pencolor()
    904        (50.0, 193.0, 143.0)
    905 
    906 
    907 .. function:: fillcolor(*args)
    908 
    909    Return or set the fillcolor.
    910 
    911    Four input formats are allowed:
    912 
    913    ``fillcolor()``
    914       Return the current fillcolor as color specification string, possibly
    915       in tuple format (see example).  May be used as input to another
    916       color/pencolor/fillcolor call.
    917 
    918    ``fillcolor(colorstring)``
    919       Set fillcolor to *colorstring*, which is a Tk color specification string,
    920       such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
    921 
    922    ``fillcolor((r, g, b))``
    923       Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and
    924       *b*.  Each of *r*, *g*, and *b* must be in the range 0..colormode, where
    925       colormode is either 1.0 or 255 (see :func:`colormode`).
    926 
    927    ``fillcolor(r, g, b)``
    928       Set fillcolor to the RGB color represented by *r*, *g*, and *b*.  Each of
    929       *r*, *g*, and *b* must be in the range 0..colormode.
    930 
    931     If turtleshape is a polygon, the interior of that polygon is drawn
    932     with the newly set fillcolor.
    933 
    934    .. doctest::
    935 
    936        >>> turtle.fillcolor("violet")
    937        >>> turtle.fillcolor()
    938        'violet'
    939        >>> turtle.pencolor()
    940        (50.0, 193.0, 143.0)
    941        >>> turtle.fillcolor((50, 193, 143))  # Integers, not floats
    942        >>> turtle.fillcolor()
    943        (50.0, 193.0, 143.0)
    944        >>> turtle.fillcolor('#ffffff')
    945        >>> turtle.fillcolor()
    946        (255.0, 255.0, 255.0)
    947 
    948 
    949 .. function:: color(*args)
    950 
    951    Return or set pencolor and fillcolor.
    952 
    953    Several input formats are allowed.  They use 0 to 3 arguments as
    954    follows:
    955 
    956    ``color()``
    957       Return the current pencolor and the current fillcolor as a pair of color
    958       specification strings or tuples as returned by :func:`pencolor` and
    959       :func:`fillcolor`.
    960 
    961    ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)``
    962       Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the
    963       given value.
    964 
    965    ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))``
    966       Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)``
    967       and analogously if the other input format is used.
    968 
    969     If turtleshape is a polygon, outline and interior of that polygon is drawn
    970     with the newly set colors.
    971 
    972    .. doctest::
    973 
    974        >>> turtle.color("red", "green")
    975        >>> turtle.color()
    976        ('red', 'green')
    977        >>> color("#285078", "#a0c8f0")
    978        >>> color()
    979        ((40.0, 80.0, 120.0), (160.0, 200.0, 240.0))
    980 
    981 
    982 See also: Screen method :func:`colormode`.
    983 
    984 
    985 Filling
    986 ~~~~~~~
    987 
    988 .. doctest::
    989    :hide:
    990 
    991    >>> turtle.home()
    992 
    993 .. function:: filling()
    994 
    995    Return fillstate (``True`` if filling, ``False`` else).
    996 
    997    .. doctest::
    998 
    999        >>> turtle.begin_fill()
   1000        >>> if turtle.filling():
   1001        ...    turtle.pensize(5)
   1002        ... else:
   1003        ...    turtle.pensize(3)
   1004 
   1005 
   1006 
   1007 .. function:: begin_fill()
   1008 
   1009    To be called just before drawing a shape to be filled.
   1010 
   1011 
   1012 .. function:: end_fill()
   1013 
   1014    Fill the shape drawn after the last call to :func:`begin_fill`.
   1015 
   1016    .. doctest::
   1017 
   1018       >>> turtle.color("black", "red")
   1019       >>> turtle.begin_fill()
   1020       >>> turtle.circle(80)
   1021       >>> turtle.end_fill()
   1022 
   1023 
   1024 More drawing control
   1025 ~~~~~~~~~~~~~~~~~~~~
   1026 
   1027 .. function:: reset()
   1028 
   1029    Delete the turtle's drawings from the screen, re-center the turtle and set
   1030    variables to the default values.
   1031 
   1032    .. doctest::
   1033 
   1034       >>> turtle.goto(0,-22)
   1035       >>> turtle.left(100)
   1036       >>> turtle.position()
   1037       (0.00,-22.00)
   1038       >>> turtle.heading()
   1039       100.0
   1040       >>> turtle.reset()
   1041       >>> turtle.position()
   1042       (0.00,0.00)
   1043       >>> turtle.heading()
   1044       0.0
   1045 
   1046 
   1047 .. function:: clear()
   1048 
   1049    Delete the turtle's drawings from the screen.  Do not move turtle.  State and
   1050    position of the turtle as well as drawings of other turtles are not affected.
   1051 
   1052 
   1053 .. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal"))
   1054 
   1055    :param arg: object to be written to the TurtleScreen
   1056    :param move: True/False
   1057    :param align: one of the strings "left", "center" or right"
   1058    :param font: a triple (fontname, fontsize, fonttype)
   1059 
   1060    Write text - the string representation of *arg* - at the current turtle
   1061    position according to *align* ("left", "center" or right") and with the given
   1062    font.  If *move* is true, the pen is moved to the bottom-right corner of the
   1063    text.  By default, *move* is ``False``.
   1064 
   1065    >>> turtle.write("Home = ", True, align="center")
   1066    >>> turtle.write((0,0), True)
   1067 
   1068 
   1069 Turtle state
   1070 ------------
   1071 
   1072 Visibility
   1073 ~~~~~~~~~~
   1074 
   1075 .. function:: hideturtle()
   1076               ht()
   1077 
   1078    Make the turtle invisible.  It's a good idea to do this while you're in the
   1079    middle of doing some complex drawing, because hiding the turtle speeds up the
   1080    drawing observably.
   1081 
   1082    .. doctest::
   1083 
   1084       >>> turtle.hideturtle()
   1085 
   1086 
   1087 .. function:: showturtle()
   1088               st()
   1089 
   1090    Make the turtle visible.
   1091 
   1092    .. doctest::
   1093 
   1094       >>> turtle.showturtle()
   1095 
   1096 
   1097 .. function:: isvisible()
   1098 
   1099    Return ``True`` if the Turtle is shown, ``False`` if it's hidden.
   1100 
   1101    >>> turtle.hideturtle()
   1102    >>> turtle.isvisible()
   1103    False
   1104    >>> turtle.showturtle()
   1105    >>> turtle.isvisible()
   1106    True
   1107 
   1108 
   1109 Appearance
   1110 ~~~~~~~~~~
   1111 
   1112 .. function:: shape(name=None)
   1113 
   1114    :param name: a string which is a valid shapename
   1115 
   1116    Set turtle shape to shape with given *name* or, if name is not given, return
   1117    name of current shape.  Shape with *name* must exist in the TurtleScreen's
   1118    shape dictionary.  Initially there are the following polygon shapes: "arrow",
   1119    "turtle", "circle", "square", "triangle", "classic".  To learn about how to
   1120    deal with shapes see Screen method :func:`register_shape`.
   1121 
   1122    .. doctest::
   1123 
   1124       >>> turtle.shape()
   1125       'classic'
   1126       >>> turtle.shape("turtle")
   1127       >>> turtle.shape()
   1128       'turtle'
   1129 
   1130 
   1131 .. function:: resizemode(rmode=None)
   1132 
   1133    :param rmode: one of the strings "auto", "user", "noresize"
   1134 
   1135    Set resizemode to one of the values: "auto", "user", "noresize".  If *rmode*
   1136    is not given, return current resizemode.  Different resizemodes have the
   1137    following effects:
   1138 
   1139    - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
   1140    - "user": adapts the appearance of the turtle according to the values of
   1141      stretchfactor and outlinewidth (outline), which are set by
   1142      :func:`shapesize`.
   1143    - "noresize": no adaption of the turtle's appearance takes place.
   1144 
   1145    resizemode("user") is called by :func:`shapesize` when used with arguments.
   1146 
   1147    .. doctest::
   1148 
   1149       >>> turtle.resizemode()
   1150       'noresize'
   1151       >>> turtle.resizemode("auto")
   1152       >>> turtle.resizemode()
   1153       'auto'
   1154 
   1155 
   1156 .. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None)
   1157               turtlesize(stretch_wid=None, stretch_len=None, outline=None)
   1158 
   1159    :param stretch_wid: positive number
   1160    :param stretch_len: positive number
   1161    :param outline: positive number
   1162 
   1163    Return or set the pen's attributes x/y-stretchfactors and/or outline.  Set
   1164    resizemode to "user".  If and only if resizemode is set to "user", the turtle
   1165    will be displayed stretched according to its stretchfactors: *stretch_wid* is
   1166    stretchfactor perpendicular to its orientation, *stretch_len* is
   1167    stretchfactor in direction of its orientation, *outline* determines the width
   1168    of the shapes's outline.
   1169 
   1170    .. doctest::
   1171 
   1172       >>> turtle.shapesize()
   1173       (1.0, 1.0, 1)
   1174       >>> turtle.resizemode("user")
   1175       >>> turtle.shapesize(5, 5, 12)
   1176       >>> turtle.shapesize()
   1177       (5, 5, 12)
   1178       >>> turtle.shapesize(outline=8)
   1179       >>> turtle.shapesize()
   1180       (5, 5, 8)
   1181 
   1182 
   1183 .. function:: shearfactor(shear=None)
   1184 
   1185    :param shear: number (optional)
   1186 
   1187    Set or return the current shearfactor. Shear the turtleshape according to
   1188    the given shearfactor shear, which is the tangent of the shear angle.
   1189    Do *not* change the turtle's heading (direction of movement).
   1190    If shear is not given: return the current shearfactor, i. e. the
   1191    tangent of the shear angle, by which lines parallel to the
   1192    heading of the turtle are sheared.
   1193 
   1194    .. doctest::
   1195 
   1196        >>> turtle.shape("circle")
   1197        >>> turtle.shapesize(5,2)
   1198        >>> turtle.shearfactor(0.5)
   1199        >>> turtle.shearfactor()
   1200        0.5
   1201 
   1202 
   1203 .. function:: tilt(angle)
   1204 
   1205    :param angle: a number
   1206 
   1207    Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
   1208    change the turtle's heading (direction of movement).
   1209 
   1210    .. doctest::
   1211 
   1212       >>> turtle.reset()
   1213       >>> turtle.shape("circle")
   1214       >>> turtle.shapesize(5,2)
   1215       >>> turtle.tilt(30)
   1216       >>> turtle.fd(50)
   1217       >>> turtle.tilt(30)
   1218       >>> turtle.fd(50)
   1219 
   1220 
   1221 .. function:: settiltangle(angle)
   1222 
   1223    :param angle: a number
   1224 
   1225    Rotate the turtleshape to point in the direction specified by *angle*,
   1226    regardless of its current tilt-angle.  *Do not* change the turtle's heading
   1227    (direction of movement).
   1228 
   1229    .. doctest::
   1230 
   1231       >>> turtle.reset()
   1232       >>> turtle.shape("circle")
   1233       >>> turtle.shapesize(5,2)
   1234       >>> turtle.settiltangle(45)
   1235       >>> turtle.fd(50)
   1236       >>> turtle.settiltangle(-45)
   1237       >>> turtle.fd(50)
   1238 
   1239    .. deprecated:: 3.1
   1240 
   1241 
   1242 .. function:: tiltangle(angle=None)
   1243 
   1244    :param angle: a number (optional)
   1245 
   1246    Set or return the current tilt-angle. If angle is given, rotate the
   1247    turtleshape to point in the direction specified by angle,
   1248    regardless of its current tilt-angle. Do *not* change the turtle's
   1249    heading (direction of movement).
   1250    If angle is not given: return the current tilt-angle, i. e. the angle
   1251    between the orientation of the turtleshape and the heading of the
   1252    turtle (its direction of movement).
   1253 
   1254    .. doctest::
   1255 
   1256       >>> turtle.reset()
   1257       >>> turtle.shape("circle")
   1258       >>> turtle.shapesize(5,2)
   1259       >>> turtle.tilt(45)
   1260       >>> turtle.tiltangle()
   1261       45.0
   1262 
   1263 
   1264 .. function:: shapetransform(t11=None, t12=None, t21=None, t22=None)
   1265 
   1266    :param t11: a number (optional)
   1267    :param t12: a number (optional)
   1268    :param t21: a number (optional)
   1269    :param t12: a number (optional)
   1270 
   1271    Set or return the current transformation matrix of the turtle shape.
   1272 
   1273    If none of the matrix elements are given, return the transformation
   1274    matrix as a tuple of 4 elements.
   1275    Otherwise set the given elements and transform the turtleshape
   1276    according to the matrix consisting of first row t11, t12 and
   1277    second row t21, 22. The determinant t11 * t22 - t12 * t21 must not be
   1278    zero, otherwise an error is raised.
   1279    Modify stretchfactor, shearfactor and tiltangle according to the
   1280    given matrix.
   1281 
   1282    .. doctest::
   1283 
   1284       >>> turtle = Turtle()
   1285       >>> turtle.shape("square")
   1286       >>> turtle.shapesize(4,2)
   1287       >>> turtle.shearfactor(-0.5)
   1288       >>> turtle.shapetransform()
   1289       (4.0, -1.0, -0.0, 2.0)
   1290 
   1291 
   1292 .. function:: get_shapepoly()
   1293 
   1294    Return the current shape polygon as tuple of coordinate pairs. This
   1295    can be used to define a new shape or components of a compound shape.
   1296 
   1297    .. doctest::
   1298 
   1299       >>> turtle.shape("square")
   1300       >>> turtle.shapetransform(4, -1, 0, 2)
   1301       >>> turtle.get_shapepoly()
   1302       ((50, -20), (30, 20), (-50, 20), (-30, -20))
   1303 
   1304 
   1305 Using events
   1306 ------------
   1307 
   1308 .. function:: onclick(fun, btn=1, add=None)
   1309 
   1310    :param fun: a function with two arguments which will be called with the
   1311                coordinates of the clicked point on the canvas
   1312    :param btn: number of the mouse-button, defaults to 1 (left mouse button)
   1313    :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
   1314                added, otherwise it will replace a former binding
   1315 
   1316    Bind *fun* to mouse-click events on this turtle.  If *fun* is ``None``,
   1317    existing bindings are removed.  Example for the anonymous turtle, i.e. the
   1318    procedural way:
   1319 
   1320    .. doctest::
   1321 
   1322       >>> def turn(x, y):
   1323       ...     left(180)
   1324       ...
   1325       >>> onclick(turn)  # Now clicking into the turtle will turn it.
   1326       >>> onclick(None)  # event-binding will be removed
   1327 
   1328 
   1329 .. function:: onrelease(fun, btn=1, add=None)
   1330 
   1331    :param fun: a function with two arguments which will be called with the
   1332                coordinates of the clicked point on the canvas
   1333    :param btn: number of the mouse-button, defaults to 1 (left mouse button)
   1334    :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
   1335                added, otherwise it will replace a former binding
   1336 
   1337    Bind *fun* to mouse-button-release events on this turtle.  If *fun* is
   1338    ``None``, existing bindings are removed.
   1339 
   1340    .. doctest::
   1341 
   1342       >>> class MyTurtle(Turtle):
   1343       ...     def glow(self,x,y):
   1344       ...         self.fillcolor("red")
   1345       ...     def unglow(self,x,y):
   1346       ...         self.fillcolor("")
   1347       ...
   1348       >>> turtle = MyTurtle()
   1349       >>> turtle.onclick(turtle.glow)     # clicking on turtle turns fillcolor red,
   1350       >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
   1351 
   1352 
   1353 .. function:: ondrag(fun, btn=1, add=None)
   1354 
   1355    :param fun: a function with two arguments which will be called with the
   1356                coordinates of the clicked point on the canvas
   1357    :param btn: number of the mouse-button, defaults to 1 (left mouse button)
   1358    :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
   1359                added, otherwise it will replace a former binding
   1360 
   1361    Bind *fun* to mouse-move events on this turtle.  If *fun* is ``None``,
   1362    existing bindings are removed.
   1363 
   1364    Remark: Every sequence of mouse-move-events on a turtle is preceded by a
   1365    mouse-click event on that turtle.
   1366 
   1367    .. doctest::
   1368 
   1369       >>> turtle.ondrag(turtle.goto)
   1370 
   1371    Subsequently, clicking and dragging the Turtle will move it across
   1372    the screen thereby producing handdrawings (if pen is down).
   1373 
   1374 
   1375 Special Turtle methods
   1376 ----------------------
   1377 
   1378 .. function:: begin_poly()
   1379 
   1380    Start recording the vertices of a polygon.  Current turtle position is first
   1381    vertex of polygon.
   1382 
   1383 
   1384 .. function:: end_poly()
   1385 
   1386    Stop recording the vertices of a polygon.  Current turtle position is last
   1387    vertex of polygon.  This will be connected with the first vertex.
   1388 
   1389 
   1390 .. function:: get_poly()
   1391 
   1392    Return the last recorded polygon.
   1393 
   1394    .. doctest::
   1395 
   1396       >>> turtle.home()
   1397       >>> turtle.begin_poly()
   1398       >>> turtle.fd(100)
   1399       >>> turtle.left(20)
   1400       >>> turtle.fd(30)
   1401       >>> turtle.left(60)
   1402       >>> turtle.fd(50)
   1403       >>> turtle.end_poly()
   1404       >>> p = turtle.get_poly()
   1405       >>> register_shape("myFavouriteShape", p)
   1406 
   1407 
   1408 .. function:: clone()
   1409 
   1410    Create and return a clone of the turtle with same position, heading and
   1411    turtle properties.
   1412 
   1413    .. doctest::
   1414 
   1415       >>> mick = Turtle()
   1416       >>> joe = mick.clone()
   1417 
   1418 
   1419 .. function:: getturtle()
   1420               getpen()
   1421 
   1422    Return the Turtle object itself.  Only reasonable use: as a function to
   1423    return the "anonymous turtle":
   1424 
   1425    .. doctest::
   1426 
   1427       >>> pet = getturtle()
   1428       >>> pet.fd(50)
   1429       >>> pet
   1430       <turtle.Turtle object at 0x...>
   1431 
   1432 
   1433 .. function:: getscreen()
   1434 
   1435    Return the :class:`TurtleScreen` object the turtle is drawing on.
   1436    TurtleScreen methods can then be called for that object.
   1437 
   1438    .. doctest::
   1439 
   1440       >>> ts = turtle.getscreen()
   1441       >>> ts
   1442       <turtle._Screen object at 0x...>
   1443       >>> ts.bgcolor("pink")
   1444 
   1445 
   1446 .. function:: setundobuffer(size)
   1447 
   1448    :param size: an integer or ``None``
   1449 
   1450    Set or disable undobuffer.  If *size* is an integer an empty undobuffer of
   1451    given size is installed.  *size* gives the maximum number of turtle actions
   1452    that can be undone by the :func:`undo` method/function.  If *size* is
   1453    ``None``, the undobuffer is disabled.
   1454 
   1455    .. doctest::
   1456 
   1457       >>> turtle.setundobuffer(42)
   1458 
   1459 
   1460 .. function:: undobufferentries()
   1461 
   1462    Return number of entries in the undobuffer.
   1463 
   1464    .. doctest::
   1465 
   1466       >>> while undobufferentries():
   1467       ...     undo()
   1468 
   1469 
   1470 
   1471 .. _compoundshapes:
   1472 
   1473 Compound shapes
   1474 ---------------
   1475 
   1476 To use compound turtle shapes, which consist of several polygons of different
   1477 color, you must use the helper class :class:`Shape` explicitly as described
   1478 below:
   1479 
   1480 1. Create an empty Shape object of type "compound".
   1481 2. Add as many components to this object as desired, using the
   1482    :meth:`addcomponent` method.
   1483 
   1484    For example:
   1485 
   1486    .. doctest::
   1487 
   1488       >>> s = Shape("compound")
   1489       >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
   1490       >>> s.addcomponent(poly1, "red", "blue")
   1491       >>> poly2 = ((0,0),(10,-5),(-10,-5))
   1492       >>> s.addcomponent(poly2, "blue", "red")
   1493 
   1494 3. Now add the Shape to the Screen's shapelist and use it:
   1495 
   1496    .. doctest::
   1497 
   1498       >>> register_shape("myshape", s)
   1499       >>> shape("myshape")
   1500 
   1501 
   1502 .. note::
   1503 
   1504    The :class:`Shape` class is used internally by the :func:`register_shape`
   1505    method in different ways.  The application programmer has to deal with the
   1506    Shape class *only* when using compound shapes like shown above!
   1507 
   1508 
   1509 Methods of TurtleScreen/Screen and corresponding functions
   1510 ==========================================================
   1511 
   1512 Most of the examples in this section refer to a TurtleScreen instance called
   1513 ``screen``.
   1514 
   1515 .. doctest::
   1516    :hide:
   1517 
   1518    >>> screen = Screen()
   1519 
   1520 Window control
   1521 --------------
   1522 
   1523 .. function:: bgcolor(*args)
   1524 
   1525    :param args: a color string or three numbers in the range 0..colormode or a
   1526                 3-tuple of such numbers
   1527 
   1528 
   1529    Set or return background color of the TurtleScreen.
   1530 
   1531    .. doctest::
   1532 
   1533       >>> screen.bgcolor("orange")
   1534       >>> screen.bgcolor()
   1535       'orange'
   1536       >>> screen.bgcolor("#800080")
   1537       >>> screen.bgcolor()
   1538       (128.0, 0.0, 128.0)
   1539 
   1540 
   1541 .. function:: bgpic(picname=None)
   1542 
   1543    :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
   1544 
   1545    Set background image or return name of current backgroundimage.  If *picname*
   1546    is a filename, set the corresponding image as background.  If *picname* is
   1547    ``"nopic"``, delete background image, if present.  If *picname* is ``None``,
   1548    return the filename of the current backgroundimage. ::
   1549 
   1550        >>> screen.bgpic()
   1551        'nopic'
   1552        >>> screen.bgpic("landscape.gif")
   1553        >>> screen.bgpic()
   1554        "landscape.gif"
   1555 
   1556 
   1557 .. function:: clear()
   1558               clearscreen()
   1559 
   1560    Delete all drawings and all turtles from the TurtleScreen.  Reset the now
   1561    empty TurtleScreen to its initial state: white background, no background
   1562    image, no event bindings and tracing on.
   1563 
   1564    .. note::
   1565       This TurtleScreen method is available as a global function only under the
   1566       name ``clearscreen``.  The global function ``clear`` is a different one
   1567       derived from the Turtle method ``clear``.
   1568 
   1569 
   1570 .. function:: reset()
   1571               resetscreen()
   1572 
   1573    Reset all Turtles on the Screen to their initial state.
   1574 
   1575    .. note::
   1576       This TurtleScreen method is available as a global function only under the
   1577       name ``resetscreen``.  The global function ``reset`` is another one
   1578       derived from the Turtle method ``reset``.
   1579 
   1580 
   1581 .. function:: screensize(canvwidth=None, canvheight=None, bg=None)
   1582 
   1583    :param canvwidth: positive integer, new width of canvas in pixels
   1584    :param canvheight: positive integer, new height of canvas in pixels
   1585    :param bg: colorstring or color-tuple, new background color
   1586 
   1587    If no arguments are given, return current (canvaswidth, canvasheight).  Else
   1588    resize the canvas the turtles are drawing on.  Do not alter the drawing
   1589    window.  To observe hidden parts of the canvas, use the scrollbars. With this
   1590    method, one can make visible those parts of a drawing which were outside the
   1591    canvas before.
   1592 
   1593       >>> screen.screensize()
   1594       (400, 300)
   1595       >>> screen.screensize(2000,1500)
   1596       >>> screen.screensize()
   1597       (2000, 1500)
   1598 
   1599    e.g. to search for an erroneously escaped turtle ;-)
   1600 
   1601 
   1602 .. function:: setworldcoordinates(llx, lly, urx, ury)
   1603 
   1604    :param llx: a number, x-coordinate of lower left corner of canvas
   1605    :param lly: a number, y-coordinate of lower left corner of canvas
   1606    :param urx: a number, x-coordinate of upper right corner of canvas
   1607    :param ury: a number, y-coordinate of upper right corner of canvas
   1608 
   1609    Set up user-defined coordinate system and switch to mode "world" if
   1610    necessary.  This performs a ``screen.reset()``.  If mode "world" is already
   1611    active, all drawings are redrawn according to the new coordinates.
   1612 
   1613    **ATTENTION**: in user-defined coordinate systems angles may appear
   1614    distorted.
   1615 
   1616    .. doctest::
   1617 
   1618       >>> screen.reset()
   1619       >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
   1620       >>> for _ in range(72):
   1621       ...     left(10)
   1622       ...
   1623       >>> for _ in range(8):
   1624       ...     left(45); fd(2)   # a regular octagon
   1625 
   1626    .. doctest::
   1627       :hide:
   1628 
   1629       >>> screen.reset()
   1630       >>> for t in turtles():
   1631       ...      t.reset()
   1632 
   1633 
   1634 Animation control
   1635 -----------------
   1636 
   1637 .. function:: delay(delay=None)
   1638 
   1639    :param delay: positive integer
   1640 
   1641    Set or return the drawing *delay* in milliseconds.  (This is approximately
   1642    the time interval between two consecutive canvas updates.)  The longer the
   1643    drawing delay, the slower the animation.
   1644 
   1645    Optional argument:
   1646 
   1647    .. doctest::
   1648 
   1649       >>> screen.delay()
   1650       10
   1651       >>> screen.delay(5)
   1652       >>> screen.delay()
   1653       5
   1654 
   1655 
   1656 .. function:: tracer(n=None, delay=None)
   1657 
   1658    :param n: nonnegative integer
   1659    :param delay: nonnegative integer
   1660 
   1661    Turn turtle animation on/off and set delay for update drawings.  If
   1662    *n* is given, only each n-th regular screen update is really
   1663    performed.  (Can be used to accelerate the drawing of complex
   1664    graphics.)  When called without arguments, returns the currently
   1665    stored value of n. Second argument sets delay value (see
   1666    :func:`delay`).
   1667 
   1668    .. doctest::
   1669 
   1670       >>> screen.tracer(8, 25)
   1671       >>> dist = 2
   1672       >>> for i in range(200):
   1673       ...     fd(dist)
   1674       ...     rt(90)
   1675       ...     dist += 2
   1676 
   1677 
   1678 .. function:: update()
   1679 
   1680    Perform a TurtleScreen update. To be used when tracer is turned off.
   1681 
   1682 See also the RawTurtle/Turtle method :func:`speed`.
   1683 
   1684 
   1685 Using screen events
   1686 -------------------
   1687 
   1688 .. function:: listen(xdummy=None, ydummy=None)
   1689 
   1690    Set focus on TurtleScreen (in order to collect key-events).  Dummy arguments
   1691    are provided in order to be able to pass :func:`listen` to the onclick method.
   1692 
   1693 
   1694 .. function:: onkey(fun, key)
   1695               onkeyrelease(fun, key)
   1696 
   1697    :param fun: a function with no arguments or ``None``
   1698    :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
   1699 
   1700    Bind *fun* to key-release event of key.  If *fun* is ``None``, event bindings
   1701    are removed. Remark: in order to be able to register key-events, TurtleScreen
   1702    must have the focus. (See method :func:`listen`.)
   1703 
   1704    .. doctest::
   1705 
   1706       >>> def f():
   1707       ...     fd(50)
   1708       ...     lt(60)
   1709       ...
   1710       >>> screen.onkey(f, "Up")
   1711       >>> screen.listen()
   1712 
   1713 
   1714 .. function:: onkeypress(fun, key=None)
   1715 
   1716    :param fun: a function with no arguments or ``None``
   1717    :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
   1718 
   1719    Bind *fun* to key-press event of key if key is given,
   1720    or to any key-press-event if no key is given.
   1721    Remark: in order to be able to register key-events, TurtleScreen
   1722    must have focus. (See method :func:`listen`.)
   1723 
   1724    .. doctest::
   1725 
   1726       >>> def f():
   1727       ...     fd(50)
   1728       ...
   1729       >>> screen.onkey(f, "Up")
   1730       >>> screen.listen()
   1731 
   1732 
   1733 .. function:: onclick(fun, btn=1, add=None)
   1734               onscreenclick(fun, btn=1, add=None)
   1735 
   1736    :param fun: a function with two arguments which will be called with the
   1737                coordinates of the clicked point on the canvas
   1738    :param btn: number of the mouse-button, defaults to 1 (left mouse button)
   1739    :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
   1740                added, otherwise it will replace a former binding
   1741 
   1742    Bind *fun* to mouse-click events on this screen.  If *fun* is ``None``,
   1743    existing bindings are removed.
   1744 
   1745    Example for a TurtleScreen instance named ``screen`` and a Turtle instance
   1746    named turtle:
   1747 
   1748    .. doctest::
   1749 
   1750       >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
   1751       >>>                             # make the turtle move to the clicked point.
   1752       >>> screen.onclick(None)        # remove event binding again
   1753 
   1754    .. note::
   1755       This TurtleScreen method is available as a global function only under the
   1756       name ``onscreenclick``.  The global function ``onclick`` is another one
   1757       derived from the Turtle method ``onclick``.
   1758 
   1759 
   1760 .. function:: ontimer(fun, t=0)
   1761 
   1762    :param fun: a function with no arguments
   1763    :param t: a number >= 0
   1764 
   1765    Install a timer that calls *fun* after *t* milliseconds.
   1766 
   1767    .. doctest::
   1768 
   1769       >>> running = True
   1770       >>> def f():
   1771       ...     if running:
   1772       ...         fd(50)
   1773       ...         lt(60)
   1774       ...         screen.ontimer(f, 250)
   1775       >>> f()   ### makes the turtle march around
   1776       >>> running = False
   1777 
   1778 
   1779 .. function:: mainloop()
   1780               done()
   1781 
   1782    Starts event loop - calling Tkinter's mainloop function.
   1783    Must be the last statement in a turtle graphics program.
   1784    Must *not* be used if a script is run from within IDLE in -n mode
   1785    (No subprocess) - for interactive use of turtle graphics. ::
   1786 
   1787       >>> screen.mainloop()
   1788 
   1789 
   1790 Input methods
   1791 -------------
   1792 
   1793 .. function:: textinput(title, prompt)
   1794 
   1795    :param title: string
   1796    :param prompt: string
   1797 
   1798    Pop up a dialog window for input of a string. Parameter title is
   1799    the title of the dialog window, prompt is a text mostly describing
   1800    what information to input.
   1801    Return the string input. If the dialog is canceled, return ``None``. ::
   1802 
   1803       >>> screen.textinput("NIM", "Name of first player:")
   1804 
   1805 
   1806 .. function:: numinput(title, prompt, default=None, minval=None, maxval=None)
   1807 
   1808    :param title: string
   1809    :param prompt: string
   1810    :param default: number (optional)
   1811    :param minval: number (optional)
   1812    :param maxval: number (optional)
   1813 
   1814    Pop up a dialog window for input of a number. title is the title of the
   1815    dialog window, prompt is a text mostly describing what numerical information
   1816    to input. default: default value, minval: minimum value for input,
   1817    maxval: maximum value for input
   1818    The number input must be in the range minval .. maxval if these are
   1819    given. If not, a hint is issued and the dialog remains open for
   1820    correction.
   1821    Return the number input. If the dialog is canceled,  return ``None``. ::
   1822 
   1823       >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000)
   1824 
   1825 
   1826 Settings and special methods
   1827 ----------------------------
   1828 
   1829 .. function:: mode(mode=None)
   1830 
   1831    :param mode: one of the strings "standard", "logo" or "world"
   1832 
   1833    Set turtle mode ("standard", "logo" or "world") and perform reset.  If mode
   1834    is not given, current mode is returned.
   1835 
   1836    Mode "standard" is compatible with old :mod:`turtle`.  Mode "logo" is
   1837    compatible with most Logo turtle graphics.  Mode "world" uses user-defined
   1838    "world coordinates". **Attention**: in this mode angles appear distorted if
   1839    ``x/y`` unit-ratio doesn't equal 1.
   1840 
   1841    ============ ========================= ===================
   1842        Mode      Initial turtle heading     positive angles
   1843    ============ ========================= ===================
   1844     "standard"    to the right (east)       counterclockwise
   1845       "logo"        upward    (north)         clockwise
   1846    ============ ========================= ===================
   1847 
   1848    .. doctest::
   1849 
   1850       >>> mode("logo")   # resets turtle heading to north
   1851       >>> mode()
   1852       'logo'
   1853 
   1854 
   1855 .. function:: colormode(cmode=None)
   1856 
   1857    :param cmode: one of the values 1.0 or 255
   1858 
   1859    Return the colormode or set it to 1.0 or 255.  Subsequently *r*, *g*, *b*
   1860    values of color triples have to be in the range 0..\ *cmode*.
   1861 
   1862    .. doctest::
   1863 
   1864       >>> screen.colormode(1)
   1865       >>> turtle.pencolor(240, 160, 80)
   1866       Traceback (most recent call last):
   1867            ...
   1868       TurtleGraphicsError: bad color sequence: (240, 160, 80)
   1869       >>> screen.colormode()
   1870       1.0
   1871       >>> screen.colormode(255)
   1872       >>> screen.colormode()
   1873       255
   1874       >>> turtle.pencolor(240,160,80)
   1875 
   1876 
   1877 .. function:: getcanvas()
   1878 
   1879    Return the Canvas of this TurtleScreen.  Useful for insiders who know what to
   1880    do with a Tkinter Canvas.
   1881 
   1882    .. doctest::
   1883 
   1884       >>> cv = screen.getcanvas()
   1885       >>> cv
   1886       <turtle.ScrolledCanvas object ...>
   1887 
   1888 
   1889 .. function:: getshapes()
   1890 
   1891    Return a list of names of all currently available turtle shapes.
   1892 
   1893    .. doctest::
   1894 
   1895       >>> screen.getshapes()
   1896       ['arrow', 'blank', 'circle', ..., 'turtle']
   1897 
   1898 
   1899 .. function:: register_shape(name, shape=None)
   1900               addshape(name, shape=None)
   1901 
   1902    There are three different ways to call this function:
   1903 
   1904    (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
   1905        corresponding image shape. ::
   1906 
   1907        >>> screen.register_shape("turtle.gif")
   1908 
   1909        .. note::
   1910           Image shapes *do not* rotate when turning the turtle, so they do not
   1911           display the heading of the turtle!
   1912 
   1913    (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
   1914        coordinates: Install the corresponding polygon shape.
   1915 
   1916        .. doctest::
   1917 
   1918           >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
   1919 
   1920    (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
   1921        object: Install the corresponding compound shape.
   1922 
   1923    Add a turtle shape to TurtleScreen's shapelist.  Only thusly registered
   1924    shapes can be used by issuing the command ``shape(shapename)``.
   1925 
   1926 
   1927 .. function:: turtles()
   1928 
   1929    Return the list of turtles on the screen.
   1930 
   1931    .. doctest::
   1932 
   1933       >>> for turtle in screen.turtles():
   1934       ...     turtle.color("red")
   1935 
   1936 
   1937 .. function:: window_height()
   1938 
   1939    Return the height of the turtle window. ::
   1940 
   1941        >>> screen.window_height()
   1942        480
   1943 
   1944 
   1945 .. function:: window_width()
   1946 
   1947    Return the width of the turtle window. ::
   1948 
   1949        >>> screen.window_width()
   1950        640
   1951 
   1952 
   1953 .. _screenspecific:
   1954 
   1955 Methods specific to Screen, not inherited from TurtleScreen
   1956 -----------------------------------------------------------
   1957 
   1958 .. function:: bye()
   1959 
   1960    Shut the turtlegraphics window.
   1961 
   1962 
   1963 .. function:: exitonclick()
   1964 
   1965    Bind bye() method to mouse clicks on the Screen.
   1966 
   1967 
   1968    If the value "using_IDLE" in the configuration dictionary is ``False``
   1969    (default value), also enter mainloop.  Remark: If IDLE with the ``-n`` switch
   1970    (no subprocess) is used, this value should be set to ``True`` in
   1971    :file:`turtle.cfg`.  In this case IDLE's own mainloop is active also for the
   1972    client script.
   1973 
   1974 
   1975 .. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
   1976 
   1977    Set the size and position of the main window.  Default values of arguments
   1978    are stored in the configuration dictionary and can be changed via a
   1979    :file:`turtle.cfg` file.
   1980 
   1981    :param width: if an integer, a size in pixels, if a float, a fraction of the
   1982                  screen; default is 50% of screen
   1983    :param height: if an integer, the height in pixels, if a float, a fraction of
   1984                   the screen; default is 75% of screen
   1985    :param startx: if positive, starting position in pixels from the left
   1986                   edge of the screen, if negative from the right edge, if ``None``,
   1987                   center window horizontally
   1988    :param starty: if positive, starting position in pixels from the top
   1989                   edge of the screen, if negative from the bottom edge, if ``None``,
   1990                   center window vertically
   1991 
   1992    .. doctest::
   1993 
   1994       >>> screen.setup (width=200, height=200, startx=0, starty=0)
   1995       >>>              # sets window to 200x200 pixels, in upper left of screen
   1996       >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
   1997       >>>              # sets window to 75% of screen by 50% of screen and centers
   1998 
   1999 
   2000 .. function:: title(titlestring)
   2001 
   2002    :param titlestring: a string that is shown in the titlebar of the turtle
   2003                        graphics window
   2004 
   2005    Set title of turtle window to *titlestring*.
   2006 
   2007    .. doctest::
   2008 
   2009       >>> screen.title("Welcome to the turtle zoo!")
   2010 
   2011 
   2012 Public classes
   2013 ==============
   2014 
   2015 
   2016 .. class:: RawTurtle(canvas)
   2017            RawPen(canvas)
   2018 
   2019    :param canvas: a :class:`tkinter.Canvas`, a :class:`ScrolledCanvas` or a
   2020                   :class:`TurtleScreen`
   2021 
   2022    Create a turtle.  The turtle has all methods described above as "methods of
   2023    Turtle/RawTurtle".
   2024 
   2025 
   2026 .. class:: Turtle()
   2027 
   2028    Subclass of RawTurtle, has the same interface but draws on a default
   2029    :class:`Screen` object created automatically when needed for the first time.
   2030 
   2031 
   2032 .. class:: TurtleScreen(cv)
   2033 
   2034    :param cv: a :class:`tkinter.Canvas`
   2035 
   2036    Provides screen oriented methods like :func:`setbg` etc. that are described
   2037    above.
   2038 
   2039 .. class:: Screen()
   2040 
   2041    Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
   2042 
   2043 
   2044 .. class:: ScrolledCanvas(master)
   2045 
   2046    :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
   2047       a Tkinter-canvas with scrollbars added
   2048 
   2049    Used by class Screen, which thus automatically provides a ScrolledCanvas as
   2050    playground for the turtles.
   2051 
   2052 .. class:: Shape(type_, data)
   2053 
   2054    :param type\_: one of the strings "polygon", "image", "compound"
   2055 
   2056    Data structure modeling shapes.  The pair ``(type_, data)`` must follow this
   2057    specification:
   2058 
   2059 
   2060    =========== ===========
   2061    *type_*     *data*
   2062    =========== ===========
   2063    "polygon"   a polygon-tuple, i.e. a tuple of pairs of coordinates
   2064    "image"     an image  (in this form only used internally!)
   2065    "compound"  ``None`` (a compound shape has to be constructed using the
   2066                :meth:`addcomponent` method)
   2067    =========== ===========
   2068 
   2069    .. method:: addcomponent(poly, fill, outline=None)
   2070 
   2071       :param poly: a polygon, i.e. a tuple of pairs of numbers
   2072       :param fill: a color the *poly* will be filled with
   2073       :param outline: a color for the poly's outline (if given)
   2074 
   2075       Example:
   2076 
   2077       .. doctest::
   2078 
   2079          >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
   2080          >>> s = Shape("compound")
   2081          >>> s.addcomponent(poly, "red", "blue")
   2082          >>> # ... add more components and then use register_shape()
   2083 
   2084       See :ref:`compoundshapes`.
   2085 
   2086 
   2087 .. class:: Vec2D(x, y)
   2088 
   2089    A two-dimensional vector class, used as a helper class for implementing
   2090    turtle graphics.  May be useful for turtle graphics programs too.  Derived
   2091    from tuple, so a vector is a tuple!
   2092 
   2093    Provides (for *a*, *b* vectors, *k* number):
   2094 
   2095    * ``a + b`` vector addition
   2096    * ``a - b`` vector subtraction
   2097    * ``a * b`` inner product
   2098    * ``k * a`` and ``a * k`` multiplication with scalar
   2099    * ``abs(a)`` absolute value of a
   2100    * ``a.rotate(angle)`` rotation
   2101 
   2102 
   2103 Help and configuration
   2104 ======================
   2105 
   2106 How to use help
   2107 ---------------
   2108 
   2109 The public methods of the Screen and Turtle classes are documented extensively
   2110 via docstrings.  So these can be used as online-help via the Python help
   2111 facilities:
   2112 
   2113 - When using IDLE, tooltips show the signatures and first lines of the
   2114   docstrings of typed in function-/method calls.
   2115 
   2116 - Calling :func:`help` on methods or functions displays the docstrings::
   2117 
   2118      >>> help(Screen.bgcolor)
   2119      Help on method bgcolor in module turtle:
   2120 
   2121      bgcolor(self, *args) unbound turtle.Screen method
   2122          Set or return backgroundcolor of the TurtleScreen.
   2123 
   2124          Arguments (if given): a color string or three numbers
   2125          in the range 0..colormode or a 3-tuple of such numbers.
   2126 
   2127 
   2128            >>> screen.bgcolor("orange")
   2129            >>> screen.bgcolor()
   2130            "orange"
   2131            >>> screen.bgcolor(0.5,0,0.5)
   2132            >>> screen.bgcolor()
   2133            "#800080"
   2134 
   2135      >>> help(Turtle.penup)
   2136      Help on method penup in module turtle:
   2137 
   2138      penup(self) unbound turtle.Turtle method
   2139          Pull the pen up -- no drawing when moving.
   2140 
   2141          Aliases: penup | pu | up
   2142 
   2143          No argument
   2144 
   2145          >>> turtle.penup()
   2146 
   2147 - The docstrings of the functions which are derived from methods have a modified
   2148   form::
   2149 
   2150      >>> help(bgcolor)
   2151      Help on function bgcolor in module turtle:
   2152 
   2153      bgcolor(*args)
   2154          Set or return backgroundcolor of the TurtleScreen.
   2155 
   2156          Arguments (if given): a color string or three numbers
   2157          in the range 0..colormode or a 3-tuple of such numbers.
   2158 
   2159          Example::
   2160 
   2161            >>> bgcolor("orange")
   2162            >>> bgcolor()
   2163            "orange"
   2164            >>> bgcolor(0.5,0,0.5)
   2165            >>> bgcolor()
   2166            "#800080"
   2167 
   2168      >>> help(penup)
   2169      Help on function penup in module turtle:
   2170 
   2171      penup()
   2172          Pull the pen up -- no drawing when moving.
   2173 
   2174          Aliases: penup | pu | up
   2175 
   2176          No argument
   2177 
   2178          Example:
   2179          >>> penup()
   2180 
   2181 These modified docstrings are created automatically together with the function
   2182 definitions that are derived from the methods at import time.
   2183 
   2184 
   2185 Translation of docstrings into different languages
   2186 --------------------------------------------------
   2187 
   2188 There is a utility to create a dictionary the keys of which are the method names
   2189 and the values of which are the docstrings of the public methods of the classes
   2190 Screen and Turtle.
   2191 
   2192 .. function:: write_docstringdict(filename="turtle_docstringdict")
   2193 
   2194    :param filename: a string, used as filename
   2195 
   2196    Create and write docstring-dictionary to a Python script with the given
   2197    filename.  This function has to be called explicitly (it is not used by the
   2198    turtle graphics classes).  The docstring dictionary will be written to the
   2199    Python script :file:`{filename}.py`.  It is intended to serve as a template
   2200    for translation of the docstrings into different languages.
   2201 
   2202 If you (or your students) want to use :mod:`turtle` with online help in your
   2203 native language, you have to translate the docstrings and save the resulting
   2204 file as e.g. :file:`turtle_docstringdict_german.py`.
   2205 
   2206 If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
   2207 will be read in at import time and will replace the original English docstrings.
   2208 
   2209 At the time of this writing there are docstring dictionaries in German and in
   2210 Italian.  (Requests please to glingl (a] aon.at.)
   2211 
   2212 
   2213 
   2214 How to configure Screen and Turtles
   2215 -----------------------------------
   2216 
   2217 The built-in default configuration mimics the appearance and behaviour of the
   2218 old turtle module in order to retain best possible compatibility with it.
   2219 
   2220 If you want to use a different configuration which better reflects the features
   2221 of this module or which better fits to your needs, e.g. for use in a classroom,
   2222 you can prepare a configuration file ``turtle.cfg`` which will be read at import
   2223 time and modify the configuration according to its settings.
   2224 
   2225 The built in configuration would correspond to the following turtle.cfg::
   2226 
   2227    width = 0.5
   2228    height = 0.75
   2229    leftright = None
   2230    topbottom = None
   2231    canvwidth = 400
   2232    canvheight = 300
   2233    mode = standard
   2234    colormode = 1.0
   2235    delay = 10
   2236    undobuffersize = 1000
   2237    shape = classic
   2238    pencolor = black
   2239    fillcolor = black
   2240    resizemode = noresize
   2241    visible = True
   2242    language = english
   2243    exampleturtle = turtle
   2244    examplescreen = screen
   2245    title = Python Turtle Graphics
   2246    using_IDLE = False
   2247 
   2248 Short explanation of selected entries:
   2249 
   2250 - The first four lines correspond to the arguments of the :meth:`Screen.setup`
   2251   method.
   2252 - Line 5 and 6 correspond to the arguments of the method
   2253   :meth:`Screen.screensize`.
   2254 - *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc.  For more
   2255   info try ``help(shape)``.
   2256 - If you want to use no fillcolor (i.e. make the turtle transparent), you have
   2257   to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
   2258   the cfg-file).
   2259 - If you want to reflect the turtle its state, you have to use ``resizemode =
   2260   auto``.
   2261 - If you set e.g. ``language = italian`` the docstringdict
   2262   :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
   2263   present on the import path, e.g. in the same directory as :mod:`turtle`.
   2264 - The entries *exampleturtle* and *examplescreen* define the names of these
   2265   objects as they occur in the docstrings.  The transformation of
   2266   method-docstrings to function-docstrings will delete these names from the
   2267   docstrings.
   2268 - *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
   2269   switch ("no subprocess").  This will prevent :func:`exitonclick` to enter the
   2270   mainloop.
   2271 
   2272 There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
   2273 stored and an additional one in the current working directory.  The latter will
   2274 override the settings of the first one.
   2275 
   2276 The :file:`Lib/turtledemo` directory contains a :file:`turtle.cfg` file.  You can
   2277 study it as an example and see its effects when running the demos (preferably
   2278 not from within the demo-viewer).
   2279 
   2280 
   2281 :mod:`turtledemo` --- Demo scripts
   2282 ==================================
   2283 
   2284 .. module:: turtledemo
   2285    :synopsis: A viewer for example turtle scripts
   2286 
   2287 The :mod:`turtledemo` package includes a set of demo scripts.  These
   2288 scripts can be run and viewed using the supplied demo viewer as follows::
   2289 
   2290    python -m turtledemo
   2291 
   2292 Alternatively, you can run the demo scripts individually.  For example, ::
   2293 
   2294    python -m turtledemo.bytedesign
   2295 
   2296 The :mod:`turtledemo` package directory contains:
   2297 
   2298 - A demo viewer :file:`__main__.py` which can be used to view the sourcecode
   2299   of the scripts and run them at the same time.
   2300 - Multiple scripts demonstrating different features of the :mod:`turtle`
   2301   module.  Examples can be accessed via the Examples menu.  They can also
   2302   be run standalone.
   2303 - A :file:`turtle.cfg` file which serves as an example of how to write
   2304   and use such files.
   2305 
   2306 The demo scripts are:
   2307 
   2308 .. tabularcolumns:: |l|L|L|
   2309 
   2310 +----------------+------------------------------+-----------------------+
   2311 | Name           | Description                  | Features              |
   2312 +================+==============================+=======================+
   2313 | bytedesign     | complex classical            | :func:`tracer`, delay,|
   2314 |                | turtle graphics pattern      | :func:`update`        |
   2315 +----------------+------------------------------+-----------------------+
   2316 | chaos          | graphs Verhulst dynamics,    | world coordinates     |
   2317 |                | shows that computer's        |                       |
   2318 |                | computations can generate    |                       |
   2319 |                | results sometimes against the|                       |
   2320 |                | common sense expectations    |                       |
   2321 +----------------+------------------------------+-----------------------+
   2322 | clock          | analog clock showing time    | turtles as clock's    |
   2323 |                | of your computer             | hands, ontimer        |
   2324 +----------------+------------------------------+-----------------------+
   2325 | colormixer     | experiment with r, g, b      | :func:`ondrag`        |
   2326 +----------------+------------------------------+-----------------------+
   2327 | forest         | 3 breadth-first trees        | randomization         |
   2328 +----------------+------------------------------+-----------------------+
   2329 | fractalcurves  | Hilbert & Koch curves        | recursion             |
   2330 +----------------+------------------------------+-----------------------+
   2331 | lindenmayer    | ethnomathematics             | L-System              |
   2332 |                | (indian kolams)              |                       |
   2333 +----------------+------------------------------+-----------------------+
   2334 | minimal_hanoi  | Towers of Hanoi              | Rectangular Turtles   |
   2335 |                |                              | as Hanoi discs        |
   2336 |                |                              | (shape, shapesize)    |
   2337 +----------------+------------------------------+-----------------------+
   2338 | nim            | play the classical nim game  | turtles as nimsticks, |
   2339 |                | with three heaps of sticks   | event driven (mouse,  |
   2340 |                | against the computer.        | keyboard)             |
   2341 +----------------+------------------------------+-----------------------+
   2342 | paint          | super minimalistic           | :func:`onclick`       |
   2343 |                | drawing program              |                       |
   2344 +----------------+------------------------------+-----------------------+
   2345 | peace          | elementary                   | turtle: appearance    |
   2346 |                |                              | and animation         |
   2347 +----------------+------------------------------+-----------------------+
   2348 | penrose        | aperiodic tiling with        | :func:`stamp`         |
   2349 |                | kites and darts              |                       |
   2350 +----------------+------------------------------+-----------------------+
   2351 | planet_and_moon| simulation of                | compound shapes,      |
   2352 |                | gravitational system         | :class:`Vec2D`        |
   2353 +----------------+------------------------------+-----------------------+
   2354 | round_dance    | dancing turtles rotating     | compound shapes, clone|
   2355 |                | pairwise in opposite         | shapesize, tilt,      |
   2356 |                | direction                    | get_shapepoly, update |
   2357 +----------------+------------------------------+-----------------------+
   2358 | sorting_animate| visual demonstration of      | simple alignment,     |
   2359 |                | different sorting methods    | randomization         |
   2360 +----------------+------------------------------+-----------------------+
   2361 | tree           | a (graphical) breadth        | :func:`clone`         |
   2362 |                | first tree (using generators)|                       |
   2363 +----------------+------------------------------+-----------------------+
   2364 | two_canvases   | simple design                | turtles on two        |
   2365 |                |                              | canvases              |
   2366 +----------------+------------------------------+-----------------------+
   2367 | wikipedia      | a pattern from the wikipedia | :func:`clone`,        |
   2368 |                | article on turtle graphics   | :func:`undo`          |
   2369 +----------------+------------------------------+-----------------------+
   2370 | yinyang        | another elementary example   | :func:`circle`        |
   2371 +----------------+------------------------------+-----------------------+
   2372 
   2373 Have fun!
   2374 
   2375 
   2376 Changes since Python 2.6
   2377 ========================
   2378 
   2379 - The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and
   2380   :meth:`Turtle.window_height` have been eliminated.
   2381   Methods with these names and functionality are now available only
   2382   as methods of :class:`Screen`. The functions derived from these remain
   2383   available. (In fact already in Python 2.6 these methods were merely
   2384   duplications of the corresponding
   2385   :class:`TurtleScreen`/:class:`Screen`-methods.)
   2386 
   2387 - The method :meth:`Turtle.fill` has been eliminated.
   2388   The behaviour of :meth:`begin_fill` and :meth:`end_fill`
   2389   have changed slightly: now  every filling-process must be completed with an
   2390   ``end_fill()`` call.
   2391 
   2392 - A method :meth:`Turtle.filling` has been added. It returns a boolean
   2393   value: ``True`` if a filling process is under way, ``False`` otherwise.
   2394   This behaviour corresponds to a ``fill()`` call without arguments in
   2395   Python 2.6.
   2396 
   2397 Changes since Python 3.0
   2398 ========================
   2399 
   2400 - The methods :meth:`Turtle.shearfactor`, :meth:`Turtle.shapetransform` and
   2401   :meth:`Turtle.get_shapepoly` have been added. Thus the full range of
   2402   regular linear transforms is now available for transforming turtle shapes.
   2403   :meth:`Turtle.tiltangle` has been enhanced in functionality: it now can
   2404   be used to get or set the tiltangle. :meth:`Turtle.settiltangle` has been
   2405   deprecated.
   2406 
   2407 - The method :meth:`Screen.onkeypress` has been added as a complement to
   2408   :meth:`Screen.onkey` which in fact binds actions to the keyrelease event.
   2409   Accordingly the latter has got an alias: :meth:`Screen.onkeyrelease`.
   2410 
   2411 - The method  :meth:`Screen.mainloop` has been added. So when working only
   2412   with Screen and Turtle objects one must not additionally import
   2413   :func:`mainloop` anymore.
   2414 
   2415 - Two input methods has been added :meth:`Screen.textinput` and
   2416   :meth:`Screen.numinput`. These popup input dialogs and return
   2417   strings and numbers respectively.
   2418 
   2419 - Two example scripts :file:`tdemo_nim.py` and :file:`tdemo_round_dance.py`
   2420   have been added to the :file:`Lib/turtledemo` directory.
   2421 
   2422 
   2423 .. doctest::
   2424    :hide:
   2425 
   2426    >>> for turtle in turtles():
   2427    ...      turtle.reset()
   2428    >>> turtle.penup()
   2429    >>> turtle.goto(-200,25)
   2430    >>> turtle.pendown()
   2431    >>> turtle.write("No one expects the Spanish Inquisition!",
   2432    ...      font=("Arial", 20, "normal"))
   2433    >>> turtle.penup()
   2434    >>> turtle.goto(-100,-50)
   2435    >>> turtle.pendown()
   2436    >>> turtle.write("Our two chief Turtles are...",
   2437    ...      font=("Arial", 16, "normal"))
   2438    >>> turtle.penup()
   2439    >>> turtle.goto(-450,-75)
   2440    >>> turtle.write(str(turtles()))
   2441