I have a program with Tkinter window and I want to set an icon for the window. I coded this:okno.iconbitmap(os.path.dirname(os.path.abspath(__file__))+"/icon.png")
but it writes:
Traceback (most recent call last):
File "Alchymist_lab.py", line 241, in <module>
okno.iconbitmap(os.path.dirname(os.path.abspath(__file__))+"/icon.png")
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1591, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "/home/lenka/documents/Alchymist_Lab/icon.png" not defined
and i know why: because i have the icon file in the same directory as my code. But i do want to have it in the same directory as my code. Is there a solution?
Assuming this error is thrown in Windows OS, problem is that iconbitmap
does not seem to support png
filetype in Windows. Use .ico
filetype instead. This webtool works superb for me - https://iconverticons.com/online/. For Linux OS, use xbm
filetype.
PS- Please provide relevant details when asking questions next time. For example: name and version of OS where you got this error.
And if I want have one icon file for all OS?
Unfortunately, no. You may need to code in a way to determine the OS platform under use and specify the icon file to use. See stackoverflow.com/questions/1854/python-what-os-am-i-running-on/… to find out the OS platform.
On Linux it writes the same error when i rename from icon.png to icon.xbm. On windows when i rename icon.png to icon.ico it writes too the same error. Your ansewer is not functioning.
png
,xbm
andico
filetypes have different file structures and hence simple renaming will not do the job, of course. You will have to use conversion tools to convert one to another file type. Use iconverticons.com/online to convert frompng
toico
, for example.This answer sounds promising. But somehow it does not work on my Python 2.7 on a Debian variant (with kernel 4.9.235). I can even provide my xbm icon, in case whoever wants to test it. Just put the following as 3 lines and save it into a text file:
#define grin2_width 15 #define grin2_height 15 static unsigned char grin2_bits[] = { 0xE0, 0x03, 0x18, 0x0C, 0x04, 0x10, 0x32, 0x26, 0x4A, 0x29, 0x01, 0x40, 0xFD, 0x5F, 0x25, 0x72, 0x25, 0x52, 0x25, 0x52, 0x2A, 0x3A, 0x32, 0x26, 0xE4, 0x1B, 0x58, 0x0C, 0xE0, 0x03 };