0

I have been making a simple list function to make add a bunch of images for turtles to use, however I keep getting this error when running it: _tkinter.TclError: couldn't open "<turtle.Turtle object at 0x000001C20E311D90>_1.gif": no such file or directory

This is my code for it.

for i in Citygroups:
        i.up()
        wn.addshape(str(i)+'_1.gif')
        wn.addshape(str(i)+'_2.gif')
        wn.addshape(str(i)+'_3.gif')

So I was wondering if there was a way to convert the <turtle.Turtle object at 0x000001C20E311D90> to the name of the turtle. (IE: City_l1)

  • How did you set the turtle name? Where did you provide the value "City_I1" to your turtle? – aaossa Mar 31 '22 at 01:05
  • City_L1 = t.Turtle(), Then City_L1 is put into a list named Citygroups – Redflame1022 Mar 31 '22 at 16:44
  • You can't easily retrieve the name of the variable where something is contained. Maybe you could use a dictionary instead of a list and use the keys as their names. Would that work for you? – aaossa Mar 31 '22 at 21:25
  • I've posted my commend as an answer in case you want to comment on it or [accept it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – aaossa Apr 01 '22 at 03:24

1 Answers1

0

You could use a dictionary to store the Turtle objects and use the keys as names:

citygroups = dict()
citygroups["City_L1"] = t.Turtle()
# and so on...

for turtle_name, turtle_object in citygroups.items():
        turtle_object.up()
        wn.addshape(f"{turtle_name}_1.gif")
        wn.addshape(f"{turtle_name}_2.gif")
        wn.addshape(f"{turtle_name}_3.gif")
aaossa
  • 3,763
  • 2
  • 21
  • 34