Search code examples
pythontkintericons

Using Tkinter command "iconbitmap" to set window icon


I have a program with a Tkinter window and I want to set an icon for the window. I use this code:window.iconbitmap(os.path.dirname(os.path.abspath(__file__))+"/icon.png") but the following error is thrown:

Traceback (most recent call last):
  File "myprogram.py", line 241, in <module>
    window.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/guest/documents/myprogramdir/icon.png" not defined

I think it is because I have the icon file in the same directory with the rest of my code. But that's how I want it to be. Is there a solution?


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.