hi.
i am trying to use two turtles in separate windows simultaneously in python but nothing found yet.
i have tried multithreadding but still the same.
can somebody help me please.
thanks.

Recommended Answers

All 5 Replies

At first sight, I would say that it is not possible. If you look in the source code of the turtle module (in python 2.7), you'll see that the Turtle instances all use a singleton object called turtle._screen. It means that multiple windows are probably not implemented.

At first sight, I would say that it is not possible. If you look in the source code of the turtle module (in python 2.7), you'll see that the Turtle instances all use a singleton object called turtle._screen. It means that multiple windows are probably not implemented.

but i think because turtle library is made from Tkinter and in Tkinter
you can open multiple windows ,in this case you can do it too

but i think because turtle library is made from Tkinter and in Tkinter
you can open multiple windows ,in this case you can do it too

I agree in theory, but the turtle module is built on top of Tkinter and is more restrictive. Here is the relevant part of the module's source code

###  Screen - Singleton  ########################

def Screen():
    """Return the singleton screen object.
    If none exists at the moment, create a new one and return it,
    else return the existing one."""
    if Turtle._screen is None:
        Turtle._screen = _Screen()
    return Turtle._screen

class _Screen(TurtleScreen):

    _root = None
    _canvas = None
    _title = _CFG["title"]

    def __init__(self):
        # XXX there is no need for this code to be conditional,
        # as there will be only a single _Screen instance, anyway
        # XXX actually, the turtle demo is injecting root window,
        # so perhaps the conditional creation of a root should be
        # preserved (perhaps by passing it as an optional parameter)
        if _Screen._root is None:
            _Screen._root = self._root = _Root()
            self._root.title(_Screen._title)
            self._root.ondestroy(self._destroy)
        if _Screen._canvas is None:
            width = _CFG["width"]
            height = _CFG["height"]
            canvwidth = _CFG["canvwidth"]
            canvheight = _CFG["canvheight"]
            leftright = _CFG["leftright"]
            topbottom = _CFG["topbottom"]
            self._root.setupcanvas(width, height, canvwidth, canvheight)
            _Screen._canvas = self._root._getcanvas()
            TurtleScreen.__init__(self, _Screen._canvas)
            self.setup(width, height, leftright, topbottom)

...

class Turtle(RawTurtle):
    """RawTurtle auto-creating (scrolled) canvas.

    When a Turtle object is created or a function derived from some
    Turtle method is called a TurtleScreen object is automatically created.
    """
    _pen = None
    _screen = None

    def __init__(self,
                 shape=_CFG["shape"],
                 undobuffersize=_CFG["undobuffersize"],
                 visible=_CFG["visible"]):
        if Turtle._screen is None:
            Turtle._screen = Screen()
        RawTurtle.__init__(self, Turtle._screen,
                           shape=shape,
                           undobuffersize=undobuffersize,
                           visible=visible)

You can see that the Turtle class uses a global member Turtle._screen which is a _Screen instance, and the _Screen class uses a global _Screen._canvas object. It means that all the turtles share the same tkinter Canvas instance.

What you can do is copy the file lib-tk/turtle.py from your python distribution to create a module myturtle.py . Then you can write

import turtle
import myturtle

joe = turtle.Turtle()
jack = myturtle.Turtle()

each turtle will use a different module and there will be a global Canvas for the turtle module and another one for the myturtle module.

This could be the start of a better turtle module !

The only time you can do multiple things at once is when you use a tool for that, like multiprocessing or parallelpython, otherwise you have to do things one at a time.

The only time you can do multiple things at once is when you use a tool for that, like multiprocessing or parallelpython, otherwise you have to do things one at a time.

thank you about your attention.Gribouillis way that he suggests ,works properly but not
simultaneously ,it means first one turtle does something and after a while other turtle
does something else.i read multiprocessing in python but can someone help me more with it?
thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.