TclError Bitmap is not defined error with relative path on Ubuntu

I'm facing a weird problem with Python Tkinter: code that runs correctly on Windows, on Ubunbtu doesn't work because for some unknown reason it is unable to load the icon file in the folder where the script is located if I don't use the full path, returning the error Exception has occurred: TclError Bitmap im.ico is not defined

E.g. suppose to have a mycode.py with this hello world

from tkinter import *
  
root = Tk()
a = Label(root, text ="Hello World")
a.pack()
root.iconbitmap('im.ico')
root.mainloop()

where im.ico is a file in the same path of mycode.py. If i run this on Windows no problem at all. if I run it on Ubuntu it crashes reporting that im.ico is not defined and to make it runnable I'm forced to put the absolute path using root.iconbitmap('/home/andrew/im.ico') for icon load.

That isn't what I want.

So in order to normalize path I have also tried to use

ICONPATH = os.path.join(os.path.dirname(__file__), 'im.ico')
root.iconbitmap(ICONPATH)

but I get same error Exception has occurred: TclError Bitmap path/im.ico is not defined

and strangely in the error output path/im.ico reported in the error itself exists and is correct

Why this error happens? How could I fix making sure that the file in current folder where script is located is loaded correctly?



Read more here: https://stackoverflow.com/questions/68462245/tclerror-bitmap-is-not-defined-error-with-relative-path-on-ubuntu

Content Attribution

This content was originally published by AndreaF at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: