Terminator Error and Python turtle not running properly - python

I'm trying to make a snake game and when I write the codes to create the window, I get an image like this:
and the codes I wrote:
import turtle
window = turtle.Screen()
window.title("Snake Game")
window.bgcolor('lightgreen')
window.setup(width=600 , height=600)
window.tracer(0)
head = turtle.Turtle()
head.speed(0)
head.color('white')
head.shape('square')
head.penup()
head.goto(0, 100)
head.direction = 'stop'
while True :
window.update()
turtle.done
The other problem is when i close the window i get an error like this:
Traceback (most recent call last):
File "practice.py", line 19, in <module>
window.update()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/turtle.py", line 1304, in update
t._update_data()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/turtle.py", line 2647, in _update_data
self.screen._incrementudc()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/turtle.py", line 1293, in _incrementudc
raise Terminator
turtle.Terminator
Do you have any solution for this problem?

Related

Having issues running turtle.py, can't seem to get the module to run properly

I'm fairly new here and still in school so apologies if this is simple but I can't seem to get turtle to work in pycharm or any other IDE. If anyone has any info please let me know.
My code:
#Super basic code to test turtle in pycharm
import turtle
wn = turtle.Screen()
yertle = turtle.Turtle()
wn.exitonclick()
yertle.forward(100)
yertle.left(120)
yertle.forward(100)
Error after the window has closed:
Traceback (most recent call last):
File "C:\Users\Tom\PycharmProjects\pythonProject\main.py", line 6, in <module>
yertle.forward(100)
File "C:\Program Files\Python39\lib\turtle.py", line 1638, in forward
self._go(distance)
File "C:\Program Files\Python39\lib\turtle.py", line 1606, in _go
self._goto(ende)
File "C:\Program Files\Python39\lib\turtle.py", line 3159, in _goto
screen._pointlist(self.currentLineItem),
File "C:\Program Files\Python39\lib\turtle.py", line 756, in _pointlist
cl = self.cv.coords(item)
File "<string>", line 1, in coords
File "C:\Program Files\Python39\lib\tkinter\__init__.py", line 2762, in coords
self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: invalid command name ".!canvas"
The problem is that when you call the turtle.exitonclick() method, it's telling turtle
Hey, the program is over, time to allow the user to close the window by clicking.
Now, you get the error because after the window is closed, there are some more turtle command for the program to go through, and those command need a turtle window to run on.
The fix is simple, relocate the wn.exitonclick() call to the bottom of your code, where you are sure that the program is over:
import turtle
wn = turtle.Screen()
yertle = turtle.Turtle()
yertle.forward(100)
yertle.left(120)
yertle.forward(100)
wn.exitonclick()
You need to move wn.exitonclick() down to the end of your script. The call doesn't return until the window is closed, so your three commands to yertle are being executed when it is too late to do so.

Snake game in Python using Turtle module

im getting errors:
I tried update python, but there was no update, so just did repair, didnt help
I dont know what do add, im begginer.
File "C, line 11, in <module>
head = turtle.Turtle()
File "C, line 3814, in __init__
RawTurtle.__init__(self, Turtle._screen,
File "C, line 2558, in __init__
self._update()
File "C, line 2661, in _update
self._update_data()
File "C, line 2647, in _update_data
self.screen._incrementudc()
File "C, line 1293, in _incrementudc
raise Terminator
turtle.Terminator
Process finished with exit code 1
deleted paths
import turtle
window = turtle.Screen()
window.title("Snake")
window.bgcolor("green")
window.setup(width=600, height=600)
window.tracer(0)
window.mainloop()
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("orange")
head.penup()
head.goto(0,0)
head.direction = "stop"
while True:
window.update()

Can't add shape in Python turtle module - no such file or directory error

I'm trying to change the shape of the turtle to an 8bit link image. I have the image I want saved to the same directory as my source code (you can see that with os.getcwd().) I'm wondering why I am getting this error and how to fix it. Thanks!
from turtle import Turtle, Screen
import os
print(os.getcwd())
wn = Screen()
wn.bgcolor('lightblue')
spaceship = Turtle()
spaceship.color('red')
newshape = Screen().addshape('8bitlink.png.gif')
spaceship.shape(newshape)
spaceship.penup()
The error looks like this:
Traceback (most recent call last):
File "/Users/colind/Desktop/Spaceship Game.py", line 12, in <module>
newshape = Screen().addshape('8bitlink.png.gif')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 1133, in register_shape
shape = Shape("image", self._image(name))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 479, in _image
return TK.PhotoImage(file=filename)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 3498, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "8bitlink.png.gif": no such file or directory
One issue is that you're not invoking addshape() correctly:
newshape = Screen().addshape('8bitlink.png.gif')
spaceship.shape(newshape)
As addshape() doesn't return anything, it registers the image shape under the name of the file, so you should instead do something like:
from turtle import Turtle, Screen
wn = Screen()
wn.bgcolor('lightblue')
wn.addshape('8bitlink.png.gif')
spaceship = Turtle()
spaceship.shape('8bitlink.png.gif')
spaceship.color('red')
spaceship.penup()
# ...
wn.mainloop()
This works for me when I store a GIF image under the name '8bitlink.png.gif'. You might convince yourself, and us, that you've an actual GIF image by running the Unix file command:
> file 8bitlink.png.gif
8bitlink.png.gif: GIF image data, version 89a, 24 x 24
>
You're output should be similar though not identical.

Python turtle.Terminator error [duplicate]

This question already has answers here:
Python Turtle.Terminator even after using exitonclick()
(5 answers)
Closed 5 months ago.
When i am using turtle module to draw a circle with this simple function:
def draw_shape(self):
canvas = Screen()
t = Turtle()
t.circle(self.r)
canvas.exitonclick()
For the first time when i call this function it opens a new window and draw a circle, i click on it to exit and when i try to again call this function from menu in console i got an error:
Original exception was:
Traceback (most recent call last):
File "main.py", line 136, in <module>
main()
File "main.py", line 132, in main
OPTIONS[user_input][1](shapes)
File "main.py", line 48, in handle_sixth_menu_option
t = Turtle()
File "/usr/lib/python3.6/turtle.py", line 3816, in __init__
visible=visible)
File "/usr/lib/python3.6/turtle.py", line 2557, in __init__
self._update()
File "/usr/lib/python3.6/turtle.py", line 2660, in _update
self._update_data()
File "/usr/lib/python3.6/turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "/usr/lib/python3.6/turtle.py", line 1292, in _incrementudc
raise Terminator
turtle.Terminator
This is because the turtle module (most reference implementations as of today) uses a class variable called _RUNNING. This becomes false during the exitonclick() method.
Changing your code to below should help.
import turtle
def draw_shape(self):
canvas = Screen()
turtle.TurtleScreen._RUNNING=True
t = turtle.Turtle()
t.circle(self.r)
canvas.exitonclick()
You can try the following:
def draw_shape(self):
import turtle as t
canvas = Screen()
t.circle(self.r)
canvas.exitonclick()
The reason your code wasn't working was because of the fact that you had already deleted or exited the instance of the turtle in the def function already once before by clicking on exit. Hence by using import turtle as t you are calling it again and creating a new instance.

tkinter tk wiget turtle python

i am trying to do a turtle game with the tkinter.Tk() class.
Here is the code:
from tkinter import *
import turtle
main = Tk(className="Castle Game")
s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
cover=turtle.RawTurtle(s)
It tries to return the following:
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "H:\2 Computer science\Year 9\4Python\Castle.py", line 7, in <module>
s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
File "C:\Python33\lib\turtle.py", line 989, in __init__
TurtleScreenBase.__init__(self, cv)
File "C:\Python33\lib\turtle.py", line 488, in __init__
self.cv.config(scrollregion = (-w//2, -h//2, w//2, h//2 ))
File "C:\Python33\lib\tkinter\__init__.py", line 1263, in configure
return self._configure('configure', cnf, kw)
File "C:\Python33\lib\tkinter\__init__.py", line 1254, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-scrollregion"
>>>
In turtle graphics, the cv option stands for canvas, but you're giving it a Tk. That's why there's an error. To fix that, you will need to do this:
from tkinter import *
import turtle
root=Tk(className="Castle Game")
main = Canvas(root)
main.pack()
s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
cover=turtle.RawTurtle(s)
By changing main to Canvas and pack it onto a root.

Categories