Registering a shape in python's turtle module

Question:

My problem:

I am attempting to register a shape with the python turtle module, however it is refusing to work. The gif in question is this and it is located in my downloads folder with the title tenor.gif. I am running this code in the IDE, Canopy, in Python 3.

If I restart the kernel and then run the code, I consistently get the same output, verifying I have the right directory:

%run "C:/Users/AlexPC/Python Programs/Chess.py"
C:UsersAlexPCDownloads
C:UsersAlexPCDownloads

and the same error:

---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
C:UsersAlexPCPython ProgramsChess.py in <module>()
     37 print(os.getcwd())
     38 s.register_shape("tenor.gif")
---> 39 t.shape("tenor.gif")
     40 
     41 
C:UsersAlexPCAppDataLocalEnthoughtCanopyedmenvsUserlibturtle.py in shape(self, name)
   2775         if not name in self.screen.getshapes():
   2776             raise TurtleGraphicsError("There is no shape named %s" % name)
-> 2777         self.turtle._setshape(name)
   2778         self._update()
   2779 
C:UsersAlexPCAppDataLocalEnthoughtCanopyedmenvsUserlibturtle.py in _setshape(self, shapeIndex)
   2504             self._item = screen._createpoly()
   2505         elif self._type == "image":
-> 2506             self._item = screen._createimage(screen._shapes["blank"]._data)
   2507         elif self._type == "compound":
   2508             self._item = [screen._createpoly() for item in
C:UsersAlexPCAppDataLocalEnthoughtCanopyedmenvsUserlibturtle.py in _createimage(self, image)
    721         """Create and return image item on canvas.
    722         """
--> 723         return self.cv.create_image(0, 0, image=image)
    724 
    725     def _drawimage(self, item, pos, image):
<string> in create_image(self, *args, **kw)
C:UsersAlexPCAppDataLocalEnthoughtCanopyedmenvsUserlibtkinter__init__.py in create_image(self, *args, **kw)
   2327     def create_image(self, *args, **kw):
   2328         """Create image item with coordinates x1,y1."""
-> 2329         return self._create('image', args, kw)
   2330     def create_line(self, *args, **kw):
   2331         """Create line with coordinates x1,y1,...,xn,yn."""
C:UsersAlexPCAppDataLocalEnthoughtCanopyedmenvsUserlibtkinter__init__.py in _create(self, itemType, args, kw)
   2318         return self.tk.getint(self.tk.call(
   2319             self._w, 'create', itemType,
-> 2320             *(args + self._options(cnf, kw))))
   2321     def create_arc(self, *args, **kw):
   2322         """Create arc shaped region with coordinates x1,y1,x2,y2."""
TclError: image "pyimage1" doesn't exist 

My code:

from turtle import Turtle, Screen
import os


t = Turtle("square")
t.shapesize(4,4)
t.hideturtle()
t.pu()  
t.goto(-280,-280)

s = Screen()
s.clearscreen()
s.tracer(False)
s.screensize(800,800)

#register the piece shapes
print(os.getcwd())
os.chdir("C:\Users\AlexPC\Downloads")
print(os.getcwd())
s.register_shape("tenor.gif")
t.shape("tenor.gif")
Asked By: aargon

||

Answers:

A couple of issues. First, your image link led to an animated GIF — there’s nothing in the turtle documentation about animated GIF support. You’ll likely just get a static GIF image at best, nothing at worst.

Second, your example code has unrelated and unecessary stuff. Let’s simplify and focus on the problem:

from turtle import Turtle, Screen
import os

screen = Screen()
screen.screensize(800, 800)
os.chdir(""C:\Users\AlexPC\Downloads"")
screen.register_shape("tenor.gif")

turtle = Turtle("tenor.gif")
turtle.penup()  
turtle.goto(-280, -280)

screen.mainloop()

This worked for me on a Unix system, putting the image in a “Downloads” directory under my current directory and using a relative os.chdir() to relocate there — I don’t know if this is a Windows-specific problem. Give the above a try to see if it works any better for you.

Answered By: cdlane

First
turtle.addshape("tenor.gif")

Then
pen.shape("tenor.gif")

Answered By: Daniel