[Tutor] Help

Peter Otten __peter__ at web.de
Thu Aug 4 04:55:09 EDT 2016


CJ wrote:

> Hi,
>   I’m coding a game for fun and i keep trying to run it but this keeps
>   showing up:
> traceback (most recent call last):
>   File "/Volumes/name/box move thing(wed).py", line 13, in <module>
>     player_img = PhotoImage(file="astronaut.png")
>   File
>   
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py",
>   line 3394, in __init__
>     Image.__init__(self, 'photo', name, cnf, master, **kw)
>   File
>   
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py",
>   line 3350, in __init__
>     self.tk.call(('image', 'create', imgtype, name,) + options)
> _tkinter.TclError: couldn't open "astronaut.png": no such file or
> directory
> 
> Even though I have a file named “astronaut.png” I’ve tried a lot of things
> if I could be helped that would be awesome.

Print the working directory before the line creating the image:

      import os
      os.print(os.getcwd())
>     player_img = PhotoImage(file="astronaut.png")


Chances are that "astronaut.png" is in another directory. If so you have to 
provide the path to that directory, e. g.

player_img = PhotoImage(file="images/astronaut.png")

If the image is in the same directory as the script you can construct the 
path with

import os
image_folder = os.path.dirname(__file__)
...
player_img = PhotoImage(file=os.path.join(image_folder, "astronaut.png"))



More information about the Tutor mailing list