pythonfiletkinterdirectoryphotoimage

tk.PhotoImage couldn`t open "gif"


I have strange behavior of the below code. When I run this code from PyCharm level it works properly and return this window (including gif with Python logo!): Window with Python logo

When I run the same python code but with using .bat and .vbs files. It will give me such error:

Error

One more thing... ".vbs" file is automatically started when windows (operating system) is turn on. ".vbs" file opening ".bat" file and ".bat" file opening ".py" file.

Do you know why python code is no able to find ".gif" when it is starting with windows operating system.

Here is code:

import tkinter as tk
class KeypadTest():
    def __init__(self, master):
        self.master = master
        self.time_display = tk.IntVar()
        global logo
        logo = tk.PhotoImage(file="indeks_gif.gif")
        tk.Label(self.master, height=10, width=10 , textvariable=self.time_display,bg="red").pack(side="right")
        Info = """INFO"""
        tk.Label(self.master,compound = tk.CENTER,image=logo,text=Info).pack()
master = tk.Tk()
KT = KeypadTest(master)
master.mainloop()

Solution

  • The program can't find the file. I suspect this is because you are not running the Python program from the directory where the gif file is located.

    This can be adressed by supplying the full path to the image or by running the bat file from that directory regardless of where the bat file is located. I haven't tried with vbs file but I think the problem is the same.

    You can experiment by including in your program a line which prints out the working directory when it starts.

    import os
    print(os.getcwd())