| 1 | \section{\module{turtle} ---
|
|---|
| 2 | Turtle graphics for Tk}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{turtle}
|
|---|
| 5 | \platform{Tk}
|
|---|
| 6 | \moduleauthor{Guido van Rossum}{[email protected]}
|
|---|
| 7 | \modulesynopsis{An environment for turtle graphics.}
|
|---|
| 8 |
|
|---|
| 9 | \sectionauthor{Moshe Zadka}{[email protected]}
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 | The \module{turtle} module provides turtle graphics primitives, in both an
|
|---|
| 13 | object-oriented and procedure-oriented ways. Because it uses \module{Tkinter}
|
|---|
| 14 | for the underlying graphics, it needs a version of python installed with
|
|---|
| 15 | Tk support.
|
|---|
| 16 |
|
|---|
| 17 | The procedural interface uses a pen and a canvas which are automagically
|
|---|
| 18 | created when any of the functions are called.
|
|---|
| 19 |
|
|---|
| 20 | The \module{turtle} module defines the following functions:
|
|---|
| 21 |
|
|---|
| 22 | \begin{funcdesc}{degrees}{}
|
|---|
| 23 | Set angle measurement units to degrees.
|
|---|
| 24 | \end{funcdesc}
|
|---|
| 25 |
|
|---|
| 26 | \begin{funcdesc}{radians}{}
|
|---|
| 27 | Set angle measurement units to radians.
|
|---|
| 28 | \end{funcdesc}
|
|---|
| 29 |
|
|---|
| 30 | \begin{funcdesc}{setup}{**kwargs}
|
|---|
| 31 | Sets the size and position of the main window. Keywords are:
|
|---|
| 32 | \begin{itemize}
|
|---|
| 33 | \item \code{width}: either a size in pixels or a fraction of the screen.
|
|---|
| 34 | The default is 50\% of the screen.
|
|---|
| 35 | \item \code{height}: either a size in pixels or a fraction of the screen.
|
|---|
| 36 | The default is 50\% of the screen.
|
|---|
| 37 | \item \code{startx}: starting position in pixels from the left edge
|
|---|
| 38 | of the screen. \code{None} is the default value and
|
|---|
| 39 | centers the window horizontally on screen.
|
|---|
| 40 | \item \code{starty}: starting position in pixels from the top edge
|
|---|
| 41 | of the screen. \code{None} is the default value and
|
|---|
| 42 | centers the window vertically on screen.
|
|---|
| 43 | \end{itemize}
|
|---|
| 44 |
|
|---|
| 45 | Examples:
|
|---|
| 46 |
|
|---|
| 47 | \begin{verbatim}
|
|---|
| 48 | # Uses default geometry: 50% x 50% of screen, centered.
|
|---|
| 49 | setup()
|
|---|
| 50 |
|
|---|
| 51 | # Sets window to 200x200 pixels, in upper left of screen
|
|---|
| 52 | setup (width=200, height=200, startx=0, starty=0)
|
|---|
| 53 |
|
|---|
| 54 | # Sets window to 75% of screen by 50% of screen, and centers it.
|
|---|
| 55 | setup(width=.75, height=0.5, startx=None, starty=None)
|
|---|
| 56 | \end{verbatim}
|
|---|
| 57 |
|
|---|
| 58 | \end{funcdesc}
|
|---|
| 59 |
|
|---|
| 60 | \begin{funcdesc}{title}{title_str}
|
|---|
| 61 | Set the window's title to \var{title}.
|
|---|
| 62 | \end{funcdesc}
|
|---|
| 63 |
|
|---|
| 64 | \begin{funcdesc}{done}{}
|
|---|
| 65 | Enters the Tk main loop. The window will continue to
|
|---|
| 66 | be displayed until the user closes it or the process is killed.
|
|---|
| 67 | \end{funcdesc}
|
|---|
| 68 |
|
|---|
| 69 | \begin{funcdesc}{reset}{}
|
|---|
| 70 | Clear the screen, re-center the pen, and set variables to the default
|
|---|
| 71 | values.
|
|---|
| 72 | \end{funcdesc}
|
|---|
| 73 |
|
|---|
| 74 | \begin{funcdesc}{clear}{}
|
|---|
| 75 | Clear the screen.
|
|---|
| 76 | \end{funcdesc}
|
|---|
| 77 |
|
|---|
| 78 | \begin{funcdesc}{tracer}{flag}
|
|---|
| 79 | Set tracing on/off (according to whether flag is true or not). Tracing
|
|---|
| 80 | means line are drawn more slowly, with an animation of an arrow along the
|
|---|
| 81 | line.
|
|---|
| 82 | \end{funcdesc}
|
|---|
| 83 |
|
|---|
| 84 | \begin{funcdesc}{speed}{speed}
|
|---|
| 85 | Set the speed of the turtle. Valid values for the parameter
|
|---|
| 86 | \var{speed} are \code{'fastest'} (no delay), \code{'fast'},
|
|---|
| 87 | (delay 5ms), \code{'normal'} (delay 10ms), \code{'slow'}
|
|---|
| 88 | (delay 15ms), and \code{'slowest'} (delay 20ms).
|
|---|
| 89 | \versionadded{2.5}
|
|---|
| 90 | \end{funcdesc}
|
|---|
| 91 |
|
|---|
| 92 | \begin{funcdesc}{delay}{delay}
|
|---|
| 93 | Set the speed of the turtle to \var{delay}, which is given
|
|---|
| 94 | in ms. \versionadded{2.5}
|
|---|
| 95 | \end{funcdesc}
|
|---|
| 96 |
|
|---|
| 97 | \begin{funcdesc}{forward}{distance}
|
|---|
| 98 | Go forward \var{distance} steps.
|
|---|
| 99 | \end{funcdesc}
|
|---|
| 100 |
|
|---|
| 101 | \begin{funcdesc}{backward}{distance}
|
|---|
| 102 | Go backward \var{distance} steps.
|
|---|
| 103 | \end{funcdesc}
|
|---|
| 104 |
|
|---|
| 105 | \begin{funcdesc}{left}{angle}
|
|---|
| 106 | Turn left \var{angle} units. Units are by default degrees, but can be
|
|---|
| 107 | set via the \function{degrees()} and \function{radians()} functions.
|
|---|
| 108 | \end{funcdesc}
|
|---|
| 109 |
|
|---|
| 110 | \begin{funcdesc}{right}{angle}
|
|---|
| 111 | Turn right \var{angle} units. Units are by default degrees, but can be
|
|---|
| 112 | set via the \function{degrees()} and \function{radians()} functions.
|
|---|
| 113 | \end{funcdesc}
|
|---|
| 114 |
|
|---|
| 115 | \begin{funcdesc}{up}{}
|
|---|
| 116 | Move the pen up --- stop drawing.
|
|---|
| 117 | \end{funcdesc}
|
|---|
| 118 |
|
|---|
| 119 | \begin{funcdesc}{down}{}
|
|---|
| 120 | Move the pen down --- draw when moving.
|
|---|
| 121 | \end{funcdesc}
|
|---|
| 122 |
|
|---|
| 123 | \begin{funcdesc}{width}{width}
|
|---|
| 124 | Set the line width to \var{width}.
|
|---|
| 125 | \end{funcdesc}
|
|---|
| 126 |
|
|---|
| 127 | \begin{funcdesc}{color}{s}
|
|---|
| 128 | \funclineni{color}{(r, g, b)}
|
|---|
| 129 | \funclineni{color}{r, g, b}
|
|---|
| 130 | Set the pen color. In the first form, the color is specified as a
|
|---|
| 131 | Tk color specification as a string. The second form specifies the
|
|---|
| 132 | color as a tuple of the RGB values, each in the range [0..1]. For the
|
|---|
| 133 | third form, the color is specified giving the RGB values as three
|
|---|
| 134 | separate parameters (each in the range [0..1]).
|
|---|
| 135 | \end{funcdesc}
|
|---|
| 136 |
|
|---|
| 137 | \begin{funcdesc}{write}{text\optional{, move}}
|
|---|
| 138 | Write \var{text} at the current pen position. If \var{move} is true,
|
|---|
| 139 | the pen is moved to the bottom-right corner of the text. By default,
|
|---|
| 140 | \var{move} is false.
|
|---|
| 141 | \end{funcdesc}
|
|---|
| 142 |
|
|---|
| 143 | \begin{funcdesc}{fill}{flag}
|
|---|
| 144 | The complete specifications are rather complex, but the recommended
|
|---|
| 145 | usage is: call \code{fill(1)} before drawing a path you want to fill,
|
|---|
| 146 | and call \code{fill(0)} when you finish to draw the path.
|
|---|
| 147 | \end{funcdesc}
|
|---|
| 148 |
|
|---|
| 149 | \begin{funcdesc}{begin\_fill}{}
|
|---|
| 150 | Switch turtle into filling mode;
|
|---|
| 151 | Must eventually be followed by a corresponding end_fill() call.
|
|---|
|
|---|