Can't import .gif file into python turtle library

Viewed 26

Trying to import some shapes into a simple code but getting the error: _tkinter.TclError: couldn't open "tess.gif": no such file or directory

edit: everything is in the same folder



Kal = turtle.Turtle()
Arrow = turtle.Turtle()
Turtlebutton = turtle.Turtle()

turtle.register_shape("tess.gif")
turtle.register_shape("btnTurtle.gif")

Kal.shape("turtle")

Monitor =turtle.Screen()
Monitor.setup(800, 600)

def Switcharrow(x,y):
    Kal.shape("arrow")

def Switchturtle(x,y):
    Kal.shape("turtle")

Arrow.hideturtle()
Arrow.shape("btnArrow.gif")
Arrow.penup()
Arrow.goto(-315,260)
Arrow.onclick(Switcharrow)
Arrow.showturtle()

Monitor.mainloop()
1 Answers

You either need to change your current working directory to the folder containing your python script and image files or change your code to use paths relative to the script instead of relative to the CWD

For example:

import os  



def imgpath(imgname):  # this function will convert translate the paths
    parent = os.path.dirname(__file__)
    os.path.join(parent, imgname)


Kal = turtle.Turtle()
Arrow = turtle.Turtle()
Turtlebutton = turtle.Turtle()

turtle.register_shape(imgpath("tess.gif"))  # call the function on each
turtle.register_shape(imgpath("btnTurtle.gif"))  # of the img file names

Kal.shape("turtle")

Monitor =turtle.Screen()
Monitor.setup(800, 600)

def Switcharrow(x,y):
    Kal.shape("arrow")

def Switchturtle(x,y):
    Kal.shape("turtle")

Arrow.hideturtle()
Arrow.shape(imgpath("btnArrow.gif"))  # here too
Arrow.penup()
Arrow.goto(-315,260)
Arrow.onclick(Switcharrow)
Arrow.showturtle()

Monitor.mainloop()
Related