Using Tkinter command "iconbitmap" to set window icon
Asked Answered
H

6

5

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?

Hengel answered 30/4, 2015 at 16:37 Comment(0)
I
5

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.

Ineffaceable answered 16/5, 2015 at 3:18 Comment(5)
And if I want have one icon file for all OS?Dyan
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 https://mcmap.net/q/53315/-how-to-identify-which-os-python-is-running-on/… to find out the OS platform.Ineffaceable
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.Dyan
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 iconverticons.com/online to convert from png to ico, for example.Ineffaceable
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 };Gerontology
H
3

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)])
Herrick answered 4/7, 2020 at 23:21 Comment(0)
I
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
Iq answered 6/12, 2020 at 0:47 Comment(1)
You should never use absolute paths in programming. They lead to huge problems when you try to share the program with someone.Ethnocentrism
C
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()

Consuetudinary answered 1/5, 2021 at 9:42 Comment(1)
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.Dyan
D
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).

Ding answered 9/11, 2021 at 17:28 Comment(1)
.iconbitmap(default=...) is the same as .iconbitmap(...) which is what OP was using.Partition
S
0

if you are using: [main_application.wm_iconbitmap("icon.ico")]

and it shows an error like this: [self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "icon.ico": no such file or directory]

The best and easiest way to avoid this error is to go to the terminal of your VS code or whatever IDE you are using, set the path to the current path with the cd command and then run it. It absolutely run.

As an example, this is my current directory (/d/py3/other_important/tkinter) and I was using it : ->/d/py3 So it has to be changed in terminal by going to this type: ->/d/py3/other_important/tkinter and after run.

Santa answered 4/1 at 5:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.