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.

Dani AI

Generated

You can get two turtles drawing at the same time, but not by threads touching Tkinter. Tkinter’s event loop is effectively single-threaded; GUI work should run in the main thread, or in separate processes if you want multiple independent windows working concurrently. See the official notes on Tkinter’s threading model and event loop in the tkinter documentation. For turtle itself, the convenience turtle.Turtle() uses a default singleton Screen, but you can bypass that by creating your own TurtleScreen on separate Tk canvases and using RawTurtle. This is documented in the turtle module classes.

Example: two windows, two turtles, one process, both animate via timers (no threads):

import tkinter as tk
import turtle

def make_window(title):
    win = tk.Toplevel()
    win.title(title)
    cv = tk.Canvas(win, width=300, height=300)
    cv.pack()
    ts = turtle.TurtleScreen(cv)
    t = turtle.RawTurtle(ts)
    return ts, t

root = tk.Tk(); root.withdraw()
ts1, t1 = make_window("A")
ts2, t2 = make_window("B")

def step():
    t1.forward(2); t1.left(3)
    t2.forward(3); t2.right(2)
    ts1.ontimer(step, 20)  # reschedule

step()
tk.mainloop()

If you truly need separate OS windows running independently and at full speed, spawn two processes (each with its own turtle program) using multiprocessing and keep all Tk calls inside each child process. This avoids Tk inter-thread issues while running both windows simultaneously.

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/ from your python distribution to create a module . 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.