0

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()
Swift S1
  • 15
  • 4
  • When you run the script from the `.bat` file, the current directory is `C:\WINDOWS\system32\python` so the `.gif` file won't be in the current directory. The current directory is likely set to something else when you run the code from PyCharm. You may be able to workaround the problem by extracting the script file's directory from the predefined `__file__` variable using `os.path.dirname()` and then using `os.path.join()` to append it to the image's filename to get a valid absolute path to the file. – martineau Sep 21 '19 at 22:24

1 Answers1

1

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()) 
figbeam
  • 7,001
  • 2
  • 12
  • 18
  • You are right the problem was connected file localization. When I run program from “.vbs” I need to show path to file “implicit” for example `E:\images\my.gif ` or run python code from desired directory with using `os.getcwd()`.Thank You for help. – Swift S1 Sep 22 '19 at 06:46