Hi, I'm trying to add an icon to my Tkinter GUI window and have had no luck. After some searching, I came up with the same piece of code which does not work.

Here is a snippet of my code:

from Tkinter import *

root = Tk()
root.minsize(290, 700)
root.title("My Window")
root.wm_iconbitmap('MyIcon.ico')
root.mainloop()

The traceback:

File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1525, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "MyIcon.ico" not defined

What can I do to change that danged icon?

Thanks in advance.

Recommended Answers

All 11 Replies

Hi, I'm trying to add an icon to my Tkinter GUI window and have had no luck. After some searching, I came up with the same piece of code which does not work.

Here is a snippet of my code:

from Tkinter import *

root = Tk()
root.minsize(290, 700)
root.title("My Window")
root.wm_iconbitmap('MyIcon.ico')
root.mainloop()

The traceback:

What can I do to change that danged icon?

Thanks in advance.

---------

Are you sure the file you're trying to display is indeed an .ico file?
Also, double check that the file is in the same directory as your Python script.

Either

root.iconbitmap("file.ico")

or the code you have should work.

You need to make sure that the ICO file is in the same directory where you working or you have to specify the correct location of the icon.

Dan08

Well, I am positive the file is in the correct directory. (The icon is "CAKE.ico" and the script is "CAKE.py"):

~/Documents/pyscripts/CAKE/Dev/trunk$ ls
CAKE.ico CAKE.py

Also, I am saving the .ico file via GIMP; I am positive it is an .ico file, but it also gives me specific options:

1 bpp, 1-bit alpha, 2-slow palette
4 bpp, 1-bit alpha, 16-slow palette
8 bpp, 1-bit alpha, 256-slow palette
24 bpp, 1-bit alpha, no palette
32 bpp, 8-bit alpha, no palette

Does it matter which one I choose?

@Dan: It is both in the same working directory AND I am also using the full path.

Thanks again.

EDIT: Also, it must not be the icon's problem, as I downloaded some .ico file from the web and still nothing.
EDIT 2: Well, I just tried this out on my Windows VM and have come to the conclusion that it works on Windows and not on Linux (using the same icon). Any ideas?

Dos it give you any error message?
They code is working fine here.

Test with this ico,to see if your ico file is the problem.
http://www.iconspedia.com/icon/snowman.html
Klikk on Download.ico.file

Can put in exception handling,it will give an TclError if ico-file not found.

from Tkinter import *

root = Tk()
root.minsize(290, 200)
root.title("My Window")
try:
    root.wm_iconbitmap('s.ico')
    root.mainloop()
except TclError:
    print 'No ico file found'

I would go with 4 bpp, 1-bit alpha, 16-slow palette

@snipp: It didn't find the icon. Here's an "ls" to prove that the icon is in fact there.

$ ls /home/foo/Desktop | grep snowman
snowman.ico

@vegaseat: Thanks, but that didn't work either.

Could this be a Tk bug? Because I know I am not screwing anything up that we have discussed so far.

EDIT: And if you didn't read the edit on my other post, this DID work on Windows but it does not on Linux.

Thanks again.

oh yea, I actually think this can be a bug because I've been trying to do it on Windows and it works, and then when I try it on ubuntu it doesn't, by the way I've tried it on Python 2.6.

Okay, thanks for backing up my idea. Could you comment on this oh great vegaseat? Moderators seem to have much more leverage.

I don't have access to a Linux machine right now, but it could be as simple as spelling the icon file name correctly, remember in Linux file names are case sensitive.

In Linux you have to use an "@" sign in front of the filename, but
root.wm_iconbitmap('@'+fname)
yields an "error reading bitmap file". I opened it in GIMP and it is fine, and tried ico, gif, and bmp with the same results. This is something that is not critical but we should know the solution, so I'll look some more tonight, and perhaps someone else has ideas as well.

Edit: Apparently it has to be an xbm image. I used ImageMagick's convert (convert ./fname.gif ./fname.xbm or do a 'man convert') to change a gif to an xbm an it worked fine with the '@' in front of the file name.

In Linux you have to use an "@" sign in front of the filename, but
root.wm_iconbitmap('@'+fname)
yields an "error reading bitmap file". I opened it in GIMP and it is fine, and tried ico, gif, and bmp with the same results. This is something that is not critical but we should know the solution, so I'll look some more tonight, and perhaps someone else has ideas as well.

Edit: Apparently it has to be an xbm image. I used ImageMagick's convert (convert ./fname.gif ./fname.xbm or do a 'man convert') to change a gif to an xbm an it worked fine with the '@' in front of the file name.

Well, what you said worked woooee. The xbm image did it. If you discover anything else, I'll make sure to keep watching this thread. But why does the "@" sign in front of the filename matter? I have never seen this before.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.