1999-11-15 13:03:41 -04:00
|
|
|
\section{\module{turtle} ---
|
|
|
|
Turtle graphics for Tk}
|
|
|
|
|
|
|
|
\declaremodule{standard}{turtle}
|
|
|
|
\platform{Tk}
|
|
|
|
\moduleauthor{Guido van Rossum}{guido@python.org}
|
|
|
|
\modulesynopsis{An environment for turtle graphics.}
|
|
|
|
|
2000-12-01 11:25:23 -04:00
|
|
|
\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
|
1999-11-15 13:03:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
The \module{turtle} module provides turtle graphics primitives, in both an
|
|
|
|
object-oriented and procedure-oriented ways. Because it uses \module{Tkinter}
|
|
|
|
for the underlying graphics, it needs a version of python installed with
|
|
|
|
Tk support.
|
|
|
|
|
|
|
|
The procedural interface uses a pen and a canvas which are automagically
|
|
|
|
created when any of the functions are called.
|
|
|
|
|
|
|
|
The \module{turtle} module defines the following functions:
|
|
|
|
|
|
|
|
\begin{funcdesc}{degrees}{}
|
|
|
|
Set angle measurement units to degrees.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{radians}{}
|
|
|
|
Set angle measurement units to radians.
|
|
|
|
\end{funcdesc}
|
2006-07-29 11:42:48 -03:00
|
|
|
|
|
|
|
\begin{funcdesc}{setup}{**kwargs}
|
|
|
|
Sets the size and position of the main window. Keywords are:
|
|
|
|
\begin{itemize}
|
|
|
|
\item \code{width}: either a size in pixels or a fraction of the screen.
|
|
|
|
The default is 50\% of the screen.
|
|
|
|
\item \code{height}: either a size in pixels or a fraction of the screen.
|
|
|
|
The default is 50\% of the screen.
|
|
|
|
\item \code{startx}: starting position in pixels from the left edge
|
|
|
|
of the screen. \code{None} is the default value and
|
|
|
|
centers the window horizontally on screen.
|
|
|
|
\item \code{starty}: starting position in pixels from the top edge
|
|
|
|
of the screen. \code{None} is the default value and
|
|
|
|
centers the window vertically on screen.
|
|
|
|
\end{itemize}
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
# Uses default geometry: 50% x 50% of screen, centered.
|
|
|
|
setup()
|
|
|
|
|
|
|
|
# Sets window to 200x200 pixels, in upper left of screen
|
|
|
|
setup (width=200, height=200, startx=0, starty=0)
|
|
|
|
|
|
|
|
# Sets window to 75% of screen by 50% of screen, and centers it.
|
|
|
|
setup(width=.75, height=0.5, startx=None, starty=None)
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{title}{title_str}
|
|
|
|
Set the window's title to \var{title}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{done}{}
|
|
|
|
Enters the Tk main loop. The window will continue to
|
|
|
|
be displayed until the user closes it or the process is killed.
|
|
|
|
\end{funcdesc}
|
1999-11-15 13:03:41 -04:00
|
|
|
|
|
|
|
\begin{funcdesc}{reset}{}
|
|
|
|
Clear the screen, re-center the pen, and set variables to the default
|
|
|
|
values.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{clear}{}
|
|
|
|
Clear the screen.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{tracer}{flag}
|
|
|
|
Set tracing on/off (according to whether flag is true or not). Tracing
|
|
|
|
means line are drawn more slowly, with an animation of an arrow along the
|
|
|
|
line.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2006-07-03 08:12:06 -03:00
|
|
|
\begin{funcdesc}{speed}{speed}
|
|
|
|
Set the speed of the turtle. Valid values for the parameter
|
|
|
|
\var{speed} are \code{'fastest'} (no delay), \code{'fast'},
|
|
|
|
(delay 5ms), \code{'normal'} (delay 10ms), \code{'slow'}
|
|
|
|
(delay 15ms), and \code{'slowest'} (delay 20ms).
|
|
|
|
\versionadded{2.5}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{delay}{delay}
|
|
|
|
Set the speed of the turtle to \var{delay}, which is given
|
|
|
|
in ms. \versionadded{2.5}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1999-11-15 13:03:41 -04:00
|
|
|
\begin{funcdesc}{forward}{distance}
|
|
|
|
Go forward \var{distance} steps.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{backward}{distance}
|
|
|
|
Go backward \var{distance} steps.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{left}{angle}
|
|
|
|
Turn left \var{angle} units. Units are by default degrees, but can be
|
|
|
|
set via the \function{degrees()} and \function{radians()} functions.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{right}{angle}
|
|
|
|
Turn right \var{angle} units. Units are by default degrees, but can be
|
|
|
|
set via the \function{degrees()} and \function{radians()} functions.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{up}{}
|
|
|
|
Move the pen up --- stop drawing.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{down}{}
|
2003-12-05 21:35:56 -04:00
|
|
|
Move the pen down --- draw when moving.
|
1999-11-15 13:03:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{width}{width}
|
|
|
|
Set the line width to \var{width}.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{color}{s}
|
2001-11-15 16:41:03 -04:00
|
|
|
\funclineni{color}{(r, g, b)}
|
|
|
|
\funclineni{color}{r, g, b}
|
|
|
|
Set the pen color. In the first form, the color is specified as a
|
|
|
|
Tk color specification as a string. The second form specifies the
|
|
|
|
color as a tuple of the RGB values, each in the range [0..1]. For the
|
|
|
|
third form, the color is specified giving the RGB values as three
|
|
|
|
separate parameters (each in the range [0..1]).
|
1999-11-15 13:03:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{write}{text\optional{, move}}
|
|
|
|
Write \var{text} at the current pen position. If \var{move} is true,
|
|
|
|
the pen is moved to the bottom-right corner of the text. By default,
|
|
|
|
\var{move} is false.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{fill}{flag}
|
|
|
|
The complete specifications are rather complex, but the recommended
|
|
|
|
usage is: call \code{fill(1)} before drawing a path you want to fill,
|
|
|
|
and call \code{fill(0)} when you finish to draw the path.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
2006-07-03 08:12:06 -03:00
|
|
|
\begin{funcdesc}{begin\_fill}{}
|
2006-07-10 19:11:28 -03:00
|
|
|
Switch turtle into filling mode;
|
|
|
|
Must eventually be followed by a corresponding end_fill() call.
|
|
|
|
Otherwise it will be ignored.
|
2006-07-03 08:12:06 -03:00
|
|
|
\versionadded{2.5}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{end\_fill}{}
|
|
|
|
End filling mode, and fill the shape; equivalent to \code{fill(0)}.
|
|
|
|
\versionadded{2.5}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1999-11-15 13:03:41 -04:00
|
|
|
\begin{funcdesc}{circle}{radius\optional{, extent}}
|
2003-02-20 23:14:08 -04:00
|
|
|
Draw a circle with radius \var{radius} whose center-point is
|
|
|
|
\var{radius} units left of the turtle.
|
|
|
|
\var{extent} determines which part of a circle is drawn: if
|
1999-11-15 13:03:41 -04:00
|
|
|
not given it defaults to a full circle.
|
|
|
|
|
|
|
|
If \var{extent} is not a full circle, one endpoint of the arc is the
|
|
|
|
current pen position. The arc is drawn in a counter clockwise
|
|
|
|
direction if \var{radius} is positive, otherwise in a clockwise
|
2003-02-20 23:14:08 -04:00
|
|
|
direction. In the process, the direction of the turtle is changed
|
|
|
|
by the amount of the \var{extent}.
|
1999-11-15 13:03:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{goto}{x, y}
|
2001-11-15 16:41:03 -04:00
|
|
|
\funclineni{goto}{(x, y)}
|
|
|
|
Go to co-ordinates \var{x}, \var{y}. The co-ordinates may be
|
|
|
|
specified either as two separate arguments or as a 2-tuple.
|
1999-11-15 13:03:41 -04:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2006-07-03 08:12:06 -03:00
|
|
|
\begin{funcdesc}{towards}{x, y}
|
|
|
|
Return the angle of the line from the turtle's position
|
|
|
|
to the point \var{x}, \var{y}. The co-ordinates may be
|
|
|
|
specified either as two separate arguments, as a 2-tuple,
|
|
|
|
or as another pen object.
|
|
|
|
\versionadded{2.5}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{heading}{}
|
|
|
|
Return the current orientation of the turtle.
|
|
|
|
\versionadded{2.3}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{setheading}{angle}
|
|
|
|
Set the orientation of the turtle to \var{angle}.
|
|
|
|
\versionadded{2.3}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{position}{}
|
|
|
|
Return the current location of the turtle as an \code{(x,y)} pair.
|
|
|
|
\versionadded{2.3}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{setx}{x}
|
|
|
|
Set the x coordinate of the turtle to \var{x}.
|
|
|
|
\versionadded{2.3}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{sety}{y}
|
|
|
|
Set the y coordinate of the turtle to \var{y}.
|
|
|
|
\versionadded{2.3}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{window\_width}{}
|
|
|
|
Return the width of the canvas window.
|
|
|
|
\versionadded{2.3}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{window\_height}{}
|
|
|
|
Return the height of the canvas window.
|
|
|
|
\versionadded{2.3}
|
|
|
|
\end{funcdesc}
|
|
|
|
|
1999-11-17 12:09:57 -04:00
|
|
|
This module also does \code{from math import *}, so see the
|
|
|
|
documentation for the \refmodule{math} module for additional constants
|
|
|
|
and functions useful for turtle graphics.
|
1999-11-15 13:03:41 -04:00
|
|
|
|
|
|
|
\begin{funcdesc}{demo}{}
|
|
|
|
Exercise the module a bit.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{excdesc}{Error}
|
|
|
|
Exception raised on any error caught by this module.
|
|
|
|
\end{excdesc}
|
|
|
|
|
|
|
|
For examples, see the code of the \function{demo()} function.
|
|
|
|
|
|
|
|
This module defines the following classes:
|
|
|
|
|
|
|
|
\begin{classdesc}{Pen}{}
|
|
|
|
Define a pen. All above functions can be called as a methods on the given
|
|
|
|
pen. The constructor automatically creates a canvas do be drawn on.
|
|
|
|
\end{classdesc}
|
|
|
|
|
2006-07-03 08:12:06 -03:00
|
|
|
\begin{classdesc}{Turtle}{}
|
|
|
|
Define a pen. This is essentially a synonym for \code{Pen()};
|
|
|
|
\class{Turtle} is an empty subclass of \class{Pen}.
|
|
|
|
\end{classdesc}
|
|
|
|
|
1999-11-15 13:03:41 -04:00
|
|
|
\begin{classdesc}{RawPen}{canvas}
|
|
|
|
Define a pen which draws on a canvas \var{canvas}. This is useful if
|
|
|
|
you want to use the module to create graphics in a ``real'' program.
|
|
|
|
\end{classdesc}
|
|
|
|
|
2006-07-03 08:12:06 -03:00
|
|
|
\subsection{Turtle, Pen and RawPen Objects \label{pen-rawpen-objects}}
|
1999-11-15 13:03:41 -04:00
|
|
|
|
2006-07-03 08:12:06 -03:00
|
|
|
\class{Turtle}, \class{Pen} and \class{RawPen} objects have all the
|
|
|
|
global functions described above, except for \function{demo()} as
|
|
|
|
methods, which manipulate the given pen.
|
1999-11-15 13:03:41 -04:00
|
|
|
|
|
|
|
The only method which is more powerful as a method is
|
|
|
|
\function{degrees()}.
|
|
|
|
|
|
|
|
\begin{methoddesc}{degrees}{\optional{fullcircle}}
|
|
|
|
\var{fullcircle} is by default 360. This can cause the pen to have any
|
|
|
|
angular units whatever: give \var{fullcircle} 2*$\pi$ for radians, or
|
|
|
|
400 for gradians.
|
|
|
|
\end{methoddesc}
|