Python turtle.Terminator error [duplicate] - python

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.

Related

Terminator Error and Python turtle not running properly

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?

Keep getting errors of ElementTree and pytmx when importing a .tmx file in pygame [duplicate]

This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
Closed 1 year ago.
I have made a tiled map, but when i try to import it just keeps giving errors i've been trying this for days and still cant resolve the errors
Also forgive me for my english errors
Traceback (most recent call last):
File "C:\Users\davim\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 188, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "C:\Users\davim\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 111, in _get_module_details
__import__(pkg_name)
File "C:\Users\davim\Documents\outros\Programação\Python\Jodo\main.py", line 12, in <module>
game_map = load_pygame("Testmap.tmx")
File "C:\Users\davim\AppData\Local\Programs\Python\Python39\lib\site-packages\pytmx\util_pygame.py", line 140, in load_pygame
return pytmx.TiledMap(filename, *args, **kwargs)
File "C:\Users\davim\AppData\Local\Programs\Python\Python39\lib\site-packages\pytmx\pytmx.py", line 370, in __init__
self.parse_xml(ElementTree.parse(self.filename).getroot())
File "C:\Users\davim\AppData\Local\Programs\Python\Python39\lib\site-packages\pytmx\pytmx.py", line 438, in parse_xml
self.reload_images()
File "C:\Users\davim\AppData\Local\Programs\Python\Python39\lib\site-packages\pytmx\pytmx.py", line 461, in reload_images
loader = self.image_loader(path, colorkey, tileset=ts)
File "C:\Users\davim\AppData\Local\Programs\Python\Python39\lib\site-packages\pytmx\util_pygame.py", line 99, in pygame_image_loader
image = pygame.image.load(filename)
FileNotFoundError: No such file or directory.
Here's my code so you can see what is wrong and help me
import pygame
from pygame.locals import *
from pytmx.util_pygame import load_pygame
pygame.init()
# -----Variaveis------
largura = 1000
altura = 600
background = (0,0,0)
screen = pygame.display.set_mode((largura, altura))
game_map = load_pygame("Testmap.tmx")
# ----MapLoader----
images = []
for y in range(50):
for x in range(50):
image = game_map.get_tile_image(x,y,0)
images.append(image)
# Desenhe todos os tiles na tela
i = 0
for y in range(50):
for x in range(50):
screen.blit(images[i],(x * 32, y * 32))
i += 1
# -----Display-----
pygame.display.set_caption('Jodo foda')
tmxdata = load_pygame('Jodo\maps\map.tmx')
# -----Loop-----
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
It appears as though all your problems source from this line: game_map = load_pygame("Testmap.tmx"). The traceback claims that there is no such file in existence. If you are sure the file does exist (double checking wouldn't hurt), try and path to it directly.
This is what a direct path looks like: C:\Users\davim\Documents\outros\Programação\Python\Jodo\Testmap.tmx, and this is a relative path, the file path you are using: Testmap.tmx. Because the file you are referencing doesn't exist in the main python folder (but instead in the documents folder), you sadly do have to do this every single time you load a file in this program.
To sum, change this: game_map = load_pygame("Testmap.tmx") to this: game_map = load_pygame("C:\Users\davim\Documents\outros\Programação\Python\Jodo\Testmap.tmx") (only if that is the path of your file.) and you should be all set.
In the future, if you ever need to path to a file directly, File Explorer can do most of the work for you. Just click on the bar on top, and it will automatically generate the file path, which you then put in front of the file name:
In my case, the parent folders are C:\Windows\diagnostics\index, and the file name is AppsDiagnostic.xml. The direct path is those two combined: C:\Windows\diagnostics\index\AppsDiagnostic.xml, with an extra backslash in between them.

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.

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.

Python - 'bgpic' Not Working

I'm using python's Turtle Graphics and I want to set a background picture. However, it's not working. I need an answer quick as I have an assignment for my Computer Science class tomorrow >_<. Here's my code:
import time
import sys
import turtle
##Render
turtle.bgpic("background.png")
##End
turtle.done()
And I'm getting this error:
Traceback (most recent call last):
File "C:/Users/Alfie/Desktop/Youtube Game/Youtube.py", line 6, in <module>
turtle.bgpic("background.png")
File "<string>", line 8, in bgpic
File "C:\Python27\lib\lib-tk\turtle.py", line 1397, in bgpic
self._bgpics[picname] = self._image(picname)
File "C:\Python27\lib\lib-tk\turtle.py", line 503, in _image
return TK.PhotoImage(file=filename)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3366, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3320, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't recognize data in image file "background.png"
Followed with a Not Responding screen. Anyone know what the error is?
Tk only supports GIF, PGM and PPM according to this question. Your turtle library uses Tk internally and so you have to use a GIF file for your background.
I have tried that function.
I have used format .png --> OK
Remember \\ instead of \
ex: turtle.bgpic("""C:\\Users\\ASUS\\Downloads\\test.gif""")
--> So I think at that time, we can use .png in Python turtle.

Categories