4

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?

Vítek Peterka
  • 89
  • 1
  • 2
  • 10

5 Answers5

4

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.

Manavalan Gajapathy
  • 3,900
  • 2
  • 20
  • 43
  • 1
    And if I want have one icon file for all OS? – Vítek Peterka May 17 '15 at 07:39
  • 1
    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 http://stackoverflow.com/questions/1854/python-what-os-am-i-running-on/25863224#25863224 to find out the OS platform. – Manavalan Gajapathy May 17 '15 at 19:52
  • 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. – Vítek Peterka May 21 '15 at 16:44
  • 6
    `png`, `xbm` and `ico` 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 https://iconverticons.com/online/ to convert from `png` to `ico`, for example. – Manavalan Gajapathy May 21 '15 at 19:09
  • 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 };` – RayLuo Jan 08 '21 at 04:41
4

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)])
GERMAN RODRIGUEZ
  • 397
  • 1
  • 4
  • 9
0

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
Julian
  • 33,915
  • 22
  • 119
  • 174
Shaun
  • 71
  • 1
  • 1
0

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()

noushin
  • 9
  • 3
  • Hello @noushin, thank you for your answer. You are right that where the icon actually is doesn't matter, I figured that out approximately six years ago :) The problem was in the icon format, as explained in the accepted answer. – Vítek Peterka May 05 '21 at 15:36
0

You need to use the keyword "bitmap" or "default" before the path:

window.iconbitmap(bitmap="Icon path.ico")

or:

window.iconbitmap(default="Icon path.ico")

If you're using windows, the "default" option set the file as the icon no only for the specified window, but for all its descendents that don't have any icon set explicitly. Note that you have to use .ico files (I have tried using other type of files but it didn't work).

Hiram
  • 1