[Tkinter] LONG POST ALERT: Setting application icon on Linux

Skip to first unread message

Tim Jarman

Mar 27, 2005, 7:27:25 AM3/27/05
Apologies in advance for the long post - I wanted to be sure I included all
the relevant details. The answer is probably very, very simple.

I am doing something stupid here, but I don't know what it is. I'm writing
an application with a Tkinter GUI (Python 2.4, Tcl/Tk 8.4.) and I want to
put a custom icon on the main window.

I've followed (so far as I understand it) the recipe in the eff-bot's
splendid Introduction to Tkinter - see:
- but it isn't working for me. Clearly there#s something I didn't get.

I need a solution for Linux initially (Mandrake 10.1/KDE 3.2 if it makes a
difference) and maybe OS/X in the future. The only solutions I've founds on
Google are Windows-specific.

Here's what I have at the moment, reduced to its essentials:

# Display an application icon.

import os.path
import Tkinter as tk

APP_NAME = "Icon Test"
HOME = "/home/tim/Projects/tkDev/playwright"

def main():
"Main entry point for the application."
# Create the root window.
root = tk.Tk()

# Set the icon.
print "current icon name = ", root.iconname()
icon_path = os.path.join(HOME, "icon.gif")
print "icon_path =", icon_path
icon_image = tk.PhotoImage(file=icon_path)
print "icon_image =", icon_image
icon_label = tk.Label(image=icon_image)
print "icon_label =", icon_label
assert icon_label.master is root
print "about to fail.."
print "success??!"
except IOError:

# Create and show the main window.

# Bootstrap code.
if __name__ == "__main__":

And here's the output I get when I run it:

current icon name =
icon_path = /home/tim/Projects/tkDev/playwright/icon.gif
icon_image = pyimage1
icon_label = .1076669484
about to fail..
Traceback (most recent call last):
File "test_icon.py", line 38, in ?
File "test_icon.py", line 27, in main
File "/usr/local/lib/python2.4/lib-tk/Tkinter.py", line 1473, in
return self.tk.call('wm', 'iconwindow', self._w, pathName)
_tkinter.TclError: can't use .1076669484 as icon window: not at top level

Obviously the root window doesn't even get displayed.

I don't understand the error message. How can I make the Label "top level"
enough to do the job? It's a child of root. Calling .pack() doesn't seem to
help (although if I comment out the iconwindow() call so that the window
actually appears, I can see that the GIF file has loaded correctly).

Do I need to hide the root window and create a new Toplevel for my app's
main window? I thought root would be an instance of Toplevel. As you can
tell, I'm a bit confused here!


Tim J

Website: www DOT jarmania FULLSTOP com

Jeff Epler

Mar 27, 2005, 11:21:51 AM3/27/05
to SPAML...@jarmania.com, pytho...@python.org
Here is a short program that sets Tk's window icon on Linux. My window
manager is icewm, and it uses a scaled version of the "flagup" image
both at the upper-left corner of the window and on the task bar entry
for the window.

import Tkinter
app = Tkinter.Tk()

As often happens, the Tkinter documentation doesn't tell the whole
story---you have to dig into the Tk documentation. I started with "man
n wm", and read the following:
If bitmap is specified, then it names a bitmap in the standard
forms accepted by Tk (see the Tk_GetBitmap manual entry for
OK, on to Tk_GetBitmap...
@fileName FileName must be the name of a file containing a
bitmap description in the standard X11 or X10 format.
and I happened to know that some bitmaps in this format exist in the
directory I mentioned above. Note that the "standard X11 format" is
monochrome, so you will not be able to use color images with
"iconbitmap" on Linux. Tk doesn't support _NET_WM_ICON for setting
full-color icons.


Tim Jarman

Mar 27, 2005, 12:01:41 PM3/27/05
Jeff Epler wrote:

Thanks for this, Jeff - I'll do some digging in the Tk docs. My problem is
that I'm trying to use iconwindow() to use a colour image, as opposed to
iconbitmap(), although if push comes to shove I suppose I could use that.

Thanks again for the quick response - on Easter weekend too!

Jeff Epler

Mar 30, 2005, 10:05:58 PM3/30/05
to pytho...@python.org
I have written a rather hackish extension to use NET_WM_ICON to set
full-color icons in Tkinter apps. You can read about it here:
you'll probably need to take a look at the EWMH spec, too. If KDE
supports NET_WM_ICON, this may work for you (but you'll have to convert
your image manually to the format required for NET_WM_ICON)

Best of luck! Unfortunately, the code is not supported.


Tim Jarman

Mar 31, 2005, 6:04:11 AM3/31/05
Jeff Epler wrote:

Thanks very much for the link! I'll take a look.

Reply all
Reply to author
0 new messages