Python Forum

Full Version: Help With Displaying Turtle Screen
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I am trying to run a turtle application on VSCode and tkinter is seeming to have a problem and none of my code works because of it.

from turtle import *
from tkinter import *
import os

def clear():
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')
        
clear()

sc = Screen() 
sc.setup(400,400)
sc.title("Graphing Calculator")
sc.bgcolor("white")

home()
hideturtle()
speed(0)
forward(100)

input("Press any key to exit ...")
Error:
Traceback (most recent call last): File "/Documents/Programs/GraphingCalculator/main.py", line 13, in <module> Screen() File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 3663, in Screen Turtle._screen = _Screen() File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 3691, in __init__ TurtleScreen.__init__(self, _Screen._canvas) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 996, in __init__ self.clear() File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 1028, in clear self.onclick(None, btn) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 1365, in onclick self._onscreenclick(fun, btn, add) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 669, in _onscreenclick self.cv.unbind("<Button-%s>" % num) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 422, in unbind self._canvas.unbind(*args, **kwargs) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1397, in unbind self.tk.call('bind', self._w, sequence, '') _tkinter.TclError: can't invoke "bind" command: application has been destroyed
It is bad practice to do wildcard imports.
I recommend doing a tutorial search for turtle.
(Mar-05-2025, 11:04 PM)menator01 Wrote: [ -> ]It is bad practice to do wildcard imports.
I recommend doing a tutorial search for turtle.

I used the turtle code on the Python Turtle website, but it still resulted in the same error.
tkinter.Tk() and Screen are incompatible. Both are trying to be the only GUI. If you want to use turtle graphics in a tkinter GUI you need to use RawTurtle and TurtleScreen. It is discussed in this post on stackoverflow.

https://stackoverflow.com/questions/4463...1#44639041
That still doesn't work for some reason. The screen isn't displaying right and I still get the same error.
from turtle import Turtle, Screen
import os

def clear():
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')
        
clear()


screen = Screen()
screen.setup(600, 600)
screen.screensize(600, 600)
turtle = Turtle()

turtle.home()
turtle.hideturtle()
turtle.speed(0)
turtle.forward(100)

screen.mainloop()

input("Press any key to exit ...")
When I run the posted code I don't get an error and the window looks exactly as I would expect.

You should not do this:
input("Press any key to exit ...")
The mainloop() waits for you to close the screen window. The additional wait for input after that is confusing. I also don't understand the purpose of clear(). This isn't a console app. Or is it.

Are you trying to run multiple turtle sessions from one program. Like this?
screen = Screen()
screen.setup(600, 600)
# screen.screensize(600, 600)
turtle = Turtle()

turtle.home()
turtle.hideturtle()
turtle.speed(0)
turtle.forward(100)

screen.mainloop()

input("Press any key to exit ...")

screen = Screen()
screen.setup(600, 600)
# screen.screensize(600, 600)
turtle = Turtle()

turtle.home()
turtle.hideturtle()
turtle.speed(0)
turtle.forward(100)

screen.mainloop()
This is a problem. You cannot call Screen() more than once. Screen does not create a screen. Screen initializes the windowing system that will display turtle graphics and returns a screen. It is very similar to how Tk() initializes tkinter and returns a toplevel window. You cannot call Tk() more than once either. I think python Turtle sits on top of tkinter, and Screen() probably uses Tk() to create the screen window. That would explain a lot.

If you want to be pop up multiple turtle screens I don't think you have any choice other than using tkinter to handle creating windows and use RawTurtle and TurtleScreen instead of Turtle and Screen. Turtle and Screen are for single windows. You can clear and draw a new image, but you are stuck with one Screen.
(Mar-06-2025, 05:59 PM)deanhystad Wrote: [ -> ]tkinter.Tk() and Screen are incompatible. Both are trying to be the only GUI. If you want to use turtle graphics in a tkinter GUI you need to use RawTurtle and TurtleScreen. It is discussed in this post on stackoverflow.

https://stackoverflow.com/questions/4463...1#44639041

(Mar-07-2025, 04:05 PM)deanhystad Wrote: [ -> ]When I run the posted code I don't get an error and the window looks exactly as I would expect.

You should not do this:
input("Press any key to exit ...")
The mainloop() waits for you to close the screen window. The additional wait for input after that is confusing. I also don't understand the purpose of clear(). This isn't a console app. Or is it.

Are you trying to run multiple turtle sessions from one program. Like this?
screen = Screen()
screen.setup(600, 600)
# screen.screensize(600, 600)
turtle = Turtle()

turtle.home()
turtle.hideturtle()
turtle.speed(0)
turtle.forward(100)

screen.mainloop()

input("Press any key to exit ...")

screen = Screen()
screen.setup(600, 600)
# screen.screensize(600, 600)
turtle = Turtle()

turtle.home()
turtle.hideturtle()
turtle.speed(0)
turtle.forward(100)

screen.mainloop()
This is a problem. You cannot call Screen() more than once. Screen does not create a screen. Screen initializes the windowing system that will display turtle graphics and returns a screen. It is very similar to how Tk() initializes tkinter and returns a toplevel window. You cannot call Tk() more than once either. I think python Turtle sits on top of tkinter, and Screen() probably uses Tk() to create the screen window. That would explain a lot.

If you want to be pop up multiple turtle screens I don't think you have any choice other than using tkinter to handle creating windows and use RawTurtle and TurtleScreen instead of Turtle and Screen. Turtle and Screen are for single windows. You can clear and draw a new image, but you are stuck with one Screen.
I still don't see the turtle or drawing even after showing it, and it gives the same error. So, I'm not certain what the underlying problem is.
from turtle import Turtle, Screen
import os

def clear():
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')
        
clear()

screen = Screen()
screen.setup(600, 600)
screen.screensize(600, 600)
turtle = Turtle()

turtle.home()
turtle.showturtle()
turtle.speed(0)
turtle.forward(100)

screen.mainloop()
I would test that tkinter works.
import tkinter as tk


def increment():
    counter.set(counter.get() + 1)


root = tk.Tk()
counter = tk.IntVar(root, 0)
tk.Label(root, textvariable=counter).pack(padx=50, pady=20)
tk.Button(root, text="Press Me", command=increment).pack(padx=50, pady=(0, 20))
root.mainloop()
It could be that you don't have working versions of tkinter or turtle. The error message in your first post has an odd path to the python packages.

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/

I've never seen a python path that looks like that. What is your OS? How did you install python?
(Mar-07-2025, 07:13 PM)deanhystad Wrote: [ -> ]I've never seen a python path that looks like that. What is your OS? How did you install python?
MacOS, I installed it through VSCode.
(Mar-07-2025, 07:13 PM)deanhystad Wrote: [ -> ]I would test that tkinter works.
import tkinter as tk


def increment():
    counter.set(counter.get() + 1)


root = tk.Tk()
counter = tk.IntVar(root, 0)
tk.Label(root, textvariable=counter).pack(padx=50, pady=20)
tk.Button(root, text="Press Me", command=increment).pack(padx=50, pady=(0, 20))
root.mainloop()
It could be that you don't have working versions of tkinter or turtle. The error message in your first post has an odd path to the python packages.

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/

I've never seen a python path that looks like that. What is your OS? How did you install python?
The tkinter works, I wonder why the Turtle doesn't?
Pages: 1 2