0

I've got a label in tkinter that's displaying an image corresponding to a json request, however, when i try to change the image which is in the same directory as the first image i am faced with an error.

my code as follows (simplified):

class Application(tk.Tk):
    def __init__(self):
        super().__init__()

        img_file = (r"C:\Users\Serge\Desktop\TravelApp\Assets\01d.png")

        self.title("Travel Application")
        self.geometry("500x300")

        self.weather_image = tk.PhotoImage(file=img_file)
        self.weather_image_label = tk.Label(self.box1, image=self.weather_image, width='100', height='100')

    def json_Request(self, url):
        try:
            icon = data['weather'][0]['icon']
            os.chdir(os.path.dirname(__file__))
            currentDIR = os.getcwd()
            img_file = (currentDIR + '\\Assets\\' + str(icon) + '.png')
            self.weather_image_label.configure(image=img_file)
            self.weather_image_label.image = img_file

When ever the json request comes back with the icon ID i just get an error that says:

image 'C:\Users\Serge\Desktop\TravelApp\Assets\09d.png' doesn't exist

however its in the same folder as the first image which is being displayed correctly and is named correctly. Can any one possibly tell me why my image cannot be located, Thanks

SurgeCode
  • 1
  • 1
  • Does it happens only with image 09d or with any requested image? – Valentino May 09 '19 at 15:59
  • @Valentino Happens with every image code, changed the icon manually and still had the same error with every different ID. – SurgeCode May 09 '19 at 16:40
  • 1
    The `image=` option of a Label has to be something like a `PhotoImage` object (as you did in `__init__()`. It cannot be a filename, you have to load the image from the file! – jasonharper May 09 '19 at 17:42

1 Answers1

1

If you have double checked that the image is where it is supposed to be (your error would suggest otherwise) try to use os.path.join to concatenate paths. It's better than doing it manually, as it can handle automatically folder separator. You do not need to escape things, and is less error prone.

I'm not a Windows user, so I'm shooting in the dark, but in your case should be:

img_file = os.path.join(currentDIR, 'Assets', str(icon) + '.png')

Have a look at this SO question for more details on os.path.join on Windows.

EDIT after comments

Try to work with absolute paths.
If the ptyhon script is located at:

'C:\Users\Serge\Desktop\TravelApp

and the images are stored in the folded:

'C:\Users\Serge\Desktop\TravelApp\Assets'

this should work:

icon = data['weather'][0]['icon']
currentDIR = os.abspath(os.path.dirname(__file__))
img_file = os.path.join(currentDIR, 'Assets', str(icon) + '.png')
Community
  • 1
  • 1
Valentino
  • 7,291
  • 6
  • 18
  • 34
  • Tried this and was faced with the same error, i know the error looks like that file doesn't exist however i am 100% sure that, that specific file is located in that directory. I am really puzzled. This is the last thing i need to for my assignment – SurgeCode May 09 '19 at 17:22
  • Even tried moving the images into the same directory as the python file and it still shows that file not existing – SurgeCode May 09 '19 at 17:24