## three dummy methods to be implemented by child class:
"""dummy method - to be overwritten by child class"""
def _tracer(self, a=None, b=None):
"""dummy method - to be overwritten by child class"""
def _delay(self, n=None):
"""dummy method - to be overwritten by child class"""
"""Drawing part of the RawTurtle.
Implements drawing properties.
def __init__(self, resizemode=_CFG["resizemode"]):
self._resizemode = resizemode # or "user" or "noresize"
def _reset(self, pencolor=_CFG["pencolor"],
fillcolor=_CFG["fillcolor"]):
self._pencolor = pencolor
self._fillcolor = fillcolor
self._stretchfactor = (1., 1.)
self._shapetrafo = (1., 0., 0., 1.)
def resizemode(self, rmode=None):
"""Set resizemode to one of the values: "auto", "user", "noresize".
rmode -- one of the strings "auto", "user", "noresize"
Different resizemodes have the following effects:
- "auto" adapts the appearance of the turtle
corresponding to the value of pensize.
- "user" adapts the appearance of the turtle according to the
values of stretchfactor and outlinewidth (outline),
which are set by shapesize()
- "noresize" no adaption of the turtle's appearance takes place.
If no argument is given, return current resizemode.
resizemode("user") is called by a call of shapesize with arguments.
Examples (for a Turtle instance named turtle):
>>> turtle.resizemode("noresize")
if rmode in ["auto", "user", "noresize"]:
self.pen(resizemode=rmode)
def pensize(self, width=None):
"""Set or return the line thickness.
Set the line thickness to width or return it. If resizemode is set
to "auto" and turtleshape is a polygon, that polygon is drawn with
the same line thickness. If no argument is given, current pensize
Example (for a Turtle instance named turtle):
>>> turtle.pensize(10) # from here on lines of width 10 are drawn
"""Pull the pen up -- no drawing when moving.
Example (for a Turtle instance named turtle):
"""Pull the pen down -- drawing when moving.
Aliases: pendown | pd | down
Example (for a Turtle instance named turtle):
"""Return True if pen is down, False if it's up.
Example (for a Turtle instance named turtle):
def speed(self, speed=None):
""" Return or set the turtle's speed.
speed -- an integer in the range 0..10 or a speedstring (see below)
Set the turtle's speed to an integer value in the range 0 .. 10.
If no argument is given: return current speed.
If input is a number greater than 10 or smaller than 0.5,
Speedstrings are mapped to speedvalues in the following way:
speeds from 1 to 10 enforce increasingly faster animation of
line drawing and turtle turning.
speed = 0 : *no* animation takes place. forward/back makes turtle jump
and likewise left/right make the turtle turn instantly.
Example (for a Turtle instance named turtle):
speeds = {'fastest':0, 'fast':10, 'normal':6, 'slow':3, 'slowest':1 }
speed = int(round(speed))
"""Return or set the pencolor and fillcolor.
Several input formats are allowed.
They use 0, 1, 2, or 3 arguments as follows:
Return the current pencolor and the current fillcolor
as a pair of color specification strings as are returned
by pencolor and fillcolor.
color(colorstring), color((r,g,b)), color(r,g,b)
inputs as in pencolor, set both, fillcolor and pencolor,
color(colorstring1, colorstring2),
color((r1,g1,b1), (r2,g2,b2))
equivalent to pencolor(colorstring1) and fillcolor(colorstring2)
and analogously, if the other input format is used.
If turtleshape is a polygon, outline and interior of that polygon
is drawn with the newly set colors.
For more info see: pencolor, fillcolor
Example (for a Turtle instance named turtle):
>>> turtle.color('red', 'green')
>>> color((40, 80, 120), (160, 200, 240))
pcolor = fcolor = args[0]
pcolor = self._colorstr(pcolor)
fcolor = self._colorstr(fcolor)
self.pen(pencolor=pcolor, fillcolor=fcolor)
return self._color(self._pencolor), self._color(self._fillcolor)
def pencolor(self, *args):
""" Return or set the pencolor.
Four input formats are allowed:
Return the current pencolor as color specification string,
possibly in hex-number format (see example).
May be used as input to another color/pencolor/fillcolor call.
s is a Tk color specification string, such as "red" or "yellow"
*a tuple* of r, g, and b, which represent, an RGB color,
and each of r, g, and b are in the range 0..colormode,
where colormode is either 1.0 or 255
r, g, and b represent an RGB color, and each of r, g, and b
are in the range 0..colormode
If turtleshape is a polygon, the outline of that polygon is drawn
with the newly set pencolor.
Example (for a Turtle instance named turtle):
>>> turtle.pencolor('brown')
>>> tup = (0.2, 0.8, 0.55)
color = self._colorstr(args)
if color == self._pencolor:
return self._color(self._pencolor)
def fillcolor(self, *args):
""" Return or set the fillcolor.
Four input formats are allowed:
Return the current fillcolor as color specification string,
possibly in hex-number format (see example).
May be used as input to another color/pencolor/fillcolor call.
s is a Tk color specification string, such as "red" or "yellow"
*a tuple* of r, g, and b, which represent, an RGB color,
and each of r, g, and b are in the range 0..colormode,
where colormode is either 1.0 or 255
r, g, and b represent an RGB color, and each of r, g, and b
are in the range 0..colormode
If turtleshape is a polygon, the interior of that polygon is drawn
with the newly set fillcolor.
Example (for a Turtle instance named turtle):
>>> turtle.fillcolor('violet')
>>> col = turtle.pencolor()
>>> turtle.fillcolor(col)
>>> turtle.fillcolor(0, .5, 0)
color = self._colorstr(args)
if color == self._fillcolor:
self.pen(fillcolor=color)
return self._color(self._fillcolor)
"""Makes the turtle visible.
Example (for a Turtle instance named turtle):
"""Makes the turtle invisible.
It's a good idea to do this while you're in the
middle of a complicated drawing, because hiding
the turtle speeds up the drawing observably.
Example (for a Turtle instance named turtle):
"""Return True if the Turtle is shown, False if it's hidden.
Example (for a Turtle instance named turtle):
>>> print turtle.isvisible():
def pen(self, pen=None, **pendict):
"""Return or set the pen's attributes.
pen -- a dictionary with some or all of the below listed keys.
**pendict -- one or more keyword-arguments with the below
Return or set the pen's attributes in a 'pen-dictionary'
with the following key/value pairs:
"pencolor" : color-string or color-tuple
"fillcolor" : color-string or color-tuple
"pensize" : positive number
"speed" : number in range 0..10
"resizemode" : "auto" or "user" or "noresize"
"stretchfactor": (positive number, positive number)
"outline" : positive number
This dictionary can be used as argument for a subsequent
pen()-call to restore the former pen-state. Moreover one
or more of these attributes can be provided as keyword-arguments.
This can be used to set several pen attributes in one statement.
Examples (for a Turtle instance named turtle):
>>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
{'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
'pencolor': 'red', 'pendown': True, 'fillcolor': 'black',
'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0}
>>> penstate=turtle.pen()
>>> turtle.color("yellow","")
{'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
'pencolor': 'yellow', 'pendown': False, 'fillcolor': '',
'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0}
>>> p.pen(penstate, fillcolor="green")
{'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
'pencolor': 'red', 'pendown': True, 'fillcolor': 'green',
'stretchfactor': (1,1), 'speed': 3, 'shearfactor': 0.0}
_pd = {"shown" : self._shown,
"pendown" : self._drawing,
"pencolor" : self._pencolor,
"fillcolor" : self._fillcolor,
"pensize" : self._pensize,
"resizemode" : self._resizemode,
"stretchfactor" : self._stretchfactor,
"shearfactor" : self._shearfactor,
"outline" : self._outlinewidth,
if isinstance(pen, dict):
self.undobuffer.push(("pen", _p_buf))
if self._drawing != p["pendown"]:
if isinstance(p["pencolor"], tuple):
p["pencolor"] = self._colorstr((p["pencolor"],))
if self._pencolor != p["pencolor"]:
if self._pensize != p["pensize"]:
self._drawing = p["pendown"]
self._pencolor = p["pencolor"]
self._pensize = p["pensize"]
if isinstance(p["fillcolor"], tuple):
p["fillcolor"] = self._colorstr((p["fillcolor"],))
self._fillcolor = p["fillcolor"]
self._resizemode = p["resizemode"]
if isinstance(sf, (int, float)):
self._shearfactor = p["shearfactor"]
self._outlinewidth = p["outline"]
if "stretchfactor" in p or "tilt" in p or "shearfactor" in p:
scx, scy = self._stretchfactor
sa, ca = math.sin(self._tilt), math.cos(self._tilt)
self._shapetrafo = ( scx*ca, scy*(shf*ca + sa),
-scx*sa, scy*(ca - shf*sa))
## three dummy methods to be implemented by child class:
def _newLine(self, usePos = True):
"""dummy method - to be overwritten by child class"""
def _update(self, count=True, forced=False):
"""dummy method - to be overwritten by child class"""
"""dummy method - to be overwritten by child class"""
def _colorstr(self, args):
"""dummy method - to be overwritten by child class"""
class _TurtleImage(object):
"""Helper class: Datatype to store Turtle attributes
def __init__(self, screen, shapeIndex):
self._setshape(shapeIndex)
def _setshape(self, shapeIndex):
self.shapeIndex = shapeIndex
if self._type == "polygon" == screen._shapes[shapeIndex]._type:
if self._type == "image" == screen._shapes[shapeIndex]._type:
if self._type in ["image", "polygon"]:
screen._delete(self._item)
elif self._type == "compound":