rootwindow = cv.winfo_toplevel()
rootwindow.call('wm', 'attributes', '.', '-topmost', '1')
rootwindow.call('wm', 'attributes', '.', '-topmost', '0')
"""Delete all drawings and all turtles from the TurtleScreen.
Reset empty TurtleScreen to its initial state: white background,
no backgroundimage, no eventbindings and tracing on.
Example (for a TurtleScreen instance named screen):
Note: this method is not available as function.
self._delayvalue = _CFG["delay"]
self._colormode = _CFG["colormode"]
self._bgpic = self._createimage("")
self._bgpicname = "nopic"
for key in self._keys[:]:
self.onkeypress(None, key)
def mode(self, mode=None):
"""Set turtle-mode ('standard', 'logo' or 'world') and perform reset.
mode -- one of the strings 'standard', 'logo' or 'world'
Mode 'standard' is compatible with turtle.py.
Mode 'logo' is compatible with most Logo-Turtle-Graphics.
Mode 'world' uses userdefined 'worldcoordinates'. *Attention*: in
this mode angles appear distorted if x/y unit-ratio doesn't equal 1.
If mode is not given, return the current mode.
Mode Initial turtle heading positive angles
------------|-------------------------|-------------------
'standard' to the right (east) counterclockwise
'logo' upward (north) clockwise
>>> mode('logo') # resets turtle heading to north
if mode not in ["standard", "logo", "world"]:
raise TurtleGraphicsError("No turtle-graphics-mode %s" % mode)
if mode in ["standard", "logo"]:
self._setscrollregion(-self.canvwidth//2, -self.canvheight//2,
self.canvwidth//2, self.canvheight//2)
self.xscale = self.yscale = 1.0
def setworldcoordinates(self, llx, lly, urx, ury):
"""Set up a user defined coordinate-system.
llx -- a number, x-coordinate of lower left corner of canvas
lly -- a number, y-coordinate of lower left corner of canvas
urx -- a number, x-coordinate of upper right corner of canvas
ury -- a number, y-coordinate of upper right corner of canvas
Set up user coodinat-system and switch to mode 'world' if necessary.
This performs a screen.reset. If mode 'world' is already active,
all drawings are redrawn according to the new coordinates.
But ATTENTION: in user-defined coordinatesystems angles may appear
distorted. (see Screen.mode())
Example (for a TurtleScreen instance named screen):
>>> screen.setworldcoordinates(-10,-0.5,50,1.5)
if self.mode() != "world":
wx, wy = self._window_size()
self.screensize(wx-20, wy-20)
oldxscale, oldyscale = self.xscale, self.yscale
self.xscale = self.canvwidth / xspan
self.yscale = self.canvheight / yspan
sry1 = -ury * self.yscale
srx2 = self.canvwidth + srx1
sry2 = self.canvheight + sry1
self._setscrollregion(srx1, sry1, srx2, sry2)
self._rescale(self.xscale/oldxscale, self.yscale/oldyscale)
def register_shape(self, name, shape=None):
"""Adds a turtle shape to TurtleScreen's shapelist.
(1) name is the name of a gif-file and shape is None.
Installs the corresponding image shape.
!! Image-shapes DO NOT rotate when turning the turtle,
!! so they do not display the heading of the turtle!
(2) name is an arbitrary string and shape is a tuple
of pairs of coordinates. Installs the corresponding
(3) name is an arbitrary string and shape is a
(compound) Shape object. Installs the corresponding
To use a shape, you have to issue the command shape(shapename).
call: register_shape("turtle.gif")
--or: register_shape("tri", ((0,0), (10,10), (-10,10)))
Example (for a TurtleScreen instance named screen):
>>> screen.register_shape("triangle", ((5,-3),(0,5),(-5,-3)))
if name.lower().endswith(".gif"):
shape = Shape("image", self._image(name))
raise TurtleGraphicsError("Bad arguments for register_shape.\n"
+ "Use help(register_shape)" )
elif isinstance(shape, tuple):
shape = Shape("polygon", shape)
## else shape assumed to be Shape-instance
self._shapes[name] = shape
def _colorstr(self, color):
"""Return color string corresponding to args.
Argument may be a string or a tuple of three
numbers corresponding to actual colormode,
i.e. in the range 0<=n<=colormode.
If the argument doesn't represent a color,
if isinstance(color, str):
if self._iscolorstring(color) or color == "":
raise TurtleGraphicsError("bad color string: %s" % str(color))
except (TypeError, ValueError):
raise TurtleGraphicsError("bad color arguments: %s" % str(color))
if self._colormode == 1.0:
r, g, b = [round(255.0*x) for x in (r, g, b)]
if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
raise TurtleGraphicsError("bad color sequence: %s" % str(color))
return "#%02x%02x%02x" % (r, g, b)
if not cstr.startswith("#"):
cl = [int(cstr[i:i+2], 16) for i in (1, 3, 5)]
cl = [16*int(cstr[h], 16) for h in cstr[1:]]
raise TurtleGraphicsError("bad colorstring: %s" % cstr)
return tuple(c * self._colormode/255 for c in cl)
def colormode(self, cmode=None):
"""Return the colormode or set it to 1.0 or 255.
cmode -- one of the values 1.0 or 255
r, g, b values of colortriples have to be in range 0..cmode.
Example (for a TurtleScreen instance named screen):
>>> screen.colormode(255)
self._colormode = float(cmode)
self._colormode = int(cmode)
"""Reset all Turtles on the Screen to their initial state.
Example (for a TurtleScreen instance named screen):
for turtle in self._turtles:
turtle._setmode(self._mode)
"""Return the list of turtles on the screen.
Example (for a TurtleScreen instance named screen):
[<turtle.Turtle object at 0x00E11FB0>]
def bgcolor(self, *args):
"""Set or return backgroundcolor of the TurtleScreen.
Arguments (if given): a color string or three numbers
in the range 0..colormode or a 3-tuple of such numbers.
Example (for a TurtleScreen instance named screen):
>>> screen.bgcolor("orange")
>>> screen.bgcolor(0.5,0,0.5)
color = self._colorstr(args)
color = self._bgcolor(color)
color = self._color(color)
def tracer(self, n=None, delay=None):
"""Turns turtle animation on/off and set delay for update drawings.
delay -- nonnegative integer
If n is given, only each n-th regular screen update is really performed.
(Can be used to accelerate the drawing of complex graphics.)
Second arguments sets delay value (see RawTurtle.delay())
Example (for a TurtleScreen instance named screen):
self._delayvalue = int(delay)
def delay(self, delay=None):
""" Return or set the drawing delay in milliseconds.
delay -- positive integer
Example (for a TurtleScreen instance named screen):
self._delayvalue = int(delay)
"""Increment update counter."""
if not TurtleScreen._RUNNING:
TurtleScreen._RUNNING = True
self._updatecounter %= self._tracing
"""Perform a TurtleScreen update.
""" Return the width of the turtle window.
Example (for a TurtleScreen instance named screen):
>>> screen.window_width()
return self._window_size()[0]
""" Return the height of the turtle window.
Example (for a TurtleScreen instance named screen):
>>> screen.window_height()
return self._window_size()[1]
"""Return the Canvas of this TurtleScreen.
Example (for a Screen instance named screen):
>>> cv = screen.getcanvas()
<turtle.ScrolledCanvas instance at 0x010742D8>
"""Return a list of names of all currently available turtle shapes.
Example (for a TurtleScreen instance named screen):
['arrow', 'blank', 'circle', ... , 'turtle']
return sorted(self._shapes.keys())
def onclick(self, fun, btn=1, add=None):
"""Bind fun to mouse-click event on canvas.
fun -- a function with two arguments, the coordinates of the
clicked point on the canvas.
btn -- the number of the mouse-button, defaults to 1
Example (for a TurtleScreen instance named screen)
>>> # Subsequently clicking into the TurtleScreen will
>>> # make the turtle move to the clicked point.
self._onscreenclick(fun, btn, add)
def onkey(self, fun, key):
"""Bind fun to key-release event of key.
fun -- a function with no arguments
key -- a string: key (e.g. "a") or key-symbol (e.g. "space")
In order to be able to register key-events, TurtleScreen
must have focus. (See method listen.)
Example (for a TurtleScreen instance named screen):
>>> screen.onkey(f, "Up")
Subsequently the turtle can be moved by repeatedly pressing
the up-arrow key, consequently drawing a hexagon
elif key not in self._keys:
self._onkeyrelease(fun, key)
def onkeypress(self, fun, key=None):
"""Bind fun to key-press event of key if key is given,
or to any key-press-event if no key is given.
fun -- a function with no arguments
key -- a string: key (e.g. "a") or key-symbol (e.g. "space")
In order to be able to register key-events, TurtleScreen
must have focus. (See method listen.)
Example (for a TurtleScreen instance named screen
and a Turtle instance named turtle):
>>> screen.onkeypress(f, "Up")
Subsequently the turtle can be moved by repeatedly pressing
the up-arrow key, or by keeping pressed the up-arrow key.
consequently drawing a hexagon.
elif key is not None and key not in self._keys:
self._onkeypress(fun, key)
def listen(self, xdummy=None, ydummy=None):
"""Set focus on TurtleScreen (in order to collect key-events)
Dummy arguments are provided in order
to be able to pass listen to the onclick method.
Example (for a TurtleScreen instance named screen):
def ontimer(self, fun, t=0):
"""Install a timer, which calls fun after t milliseconds.
fun -- a function with no arguments.
Example (for a TurtleScreen instance named screen):
... screen.ontimer(f, 250)
>>> f() # makes the turtle marching around
def bgpic(self, picname=None):
"""Set background image or return name of current backgroundimage.
picname -- a string, name of a gif-file or "nopic".
If picname is a filename, set the corresponding image as background.
If picname is "nopic", delete backgroundimage, if present.
If picname is None, return the filename of the current backgroundimage.
Example (for a TurtleScreen instance named screen):
>>> screen.bgpic("landscape.gif")
if picname not in self._bgpics:
self._bgpics[picname] = self._image(picname)
self._setbgpic(self._bgpic, self._bgpics[picname])
self._bgpicname = picname
def screensize(self, canvwidth=None, canvheight=None, bg=None):
"""Resize the canvas the turtles are drawing on.
canvwidth -- positive integer, new width of canvas in pixels
canvheight -- positive integer, new height of canvas in pixels
bg -- colorstring or color-tuple, new backgroundcolor
If no arguments are given, return current (canvaswidth, canvasheight)
Do not alter the drawing window. To observe hidden parts of
the canvas use the scrollbars. (Can make visible those parts
of a drawing, which were outside the canvas before!)
Example (for a Turtle instance named turtle):
>>> turtle.screensize(2000,1500)