4
votes

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?

4

4 Answers

1
votes

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.

1
votes

Code to turn files into ico format with pillow library. Available formats: https://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html

from PIL import Image
filen = r'icon.png'
img = Image.open(filen)
img.save('icon.ico',format = 'ICO', sizes=[(32,32)])
0
votes

in PyCharm before mainloop add

app.iconbitmap(r'C:\Users\User\PycharmProjects\HelloWorld\my.ico')

in console input

pyinstaller --onefile -w -F -i "my.ico" my.py
0
votes

you can set your icon directory , it does not matter that be in the same folder of your code for example i save my icon on images folder at this aیdress C:\PYTHON\library\images

from tkinter import *

win = Tk() win.iconbitmap('C:/PYTHON/library/images/book5.ico') win.mainloop()