Personal tools
You are here: Home How To Icons Tkinter Title Bar Icon
Document Actions

Tkinter Title Bar Icon

by James Stroud last modified 2008-09-13 15:07

Change the default Tkinter title bar icon in windows.

This is how to change the default Tkinter title bar icon in windows. It comes from a hint on line by John Fouhy. I'm summarizing here and providing the complete text of his message below so (1) I can find it in the future, (2) it doesn't disappear off the net permanently some day. Here is the summary:

root.tk.call('wm', 'iconbitmap', self._w, '-default', 'iconfile.ico')

Here, "root" would have been a Tkinter.Tk instance, e.g.:

from Tkinter import *
tk = Tk()
root.tk.call('wm', 'iconbitmap', self._w, '-default', 'iconfile.ico')

Of course the 'iconfile.ico' is a proper windows icon file. See this page for how to make those.

Below is the original message.

John Fouhy's Original Message

Hi all,

I have been attempting to change the title bar icon of my Tkinter applications in Windows XP.  Chasing round in google, I discovered:
 - This is a common difficulty.
 - There aren't any good answers.

Eventually, I stumbled across a link to this:
http://www.tcl.tk/man/tcl8.4/TkCmd/wm.htm#M18

Here is the significant information:
 "On the Windows operating system, an additional flag is supported: wmiconbitmap window ?-default? ?image?. If the -default flag is given, the icon is applied to all toplevel windows (existing and future) to     which no other specific icon has yet been applied. In addition to bitmap image types, a full path specification to any file which contains a  valid Windows icon is also accepted (usually .ico or .icr files), or any file for which the shell has assigned an icon. Tcl will first test   if the file contains an icon, then if it has an assigned icon, and finally, if that fails, test for a bitmap."

The Tkinter source (Python 2.3.4) includes this:

    def wm_iconbitmap(self, bitmap=None):
        """Set bitmap for the iconified widget to BITMAP. Return
        the bitmap if None is given."""
        return self.tk.call('wm', 'iconbitmap', self._w, bitmap)

I modified this, to look like:

    def wm_iconbitmap(self, bitmap=None, default=None):
        """Set bitmap for the iconified widget to BITMAP. Return
        the bitmap if None is given."""
        if default:
            return self.tk.call('wm', 'iconbitmap', self._w, '-default', default)
        else:
            return self.tk.call('wm', 'iconbitmap', self._w, bitmap)

The following code now does exactly what you would like it to:

#####
from Tkinter import *

tk = Tk()
tk.iconbitmap(default='foo.ico')
Label(tk, text='This window now has a custom icon.').pack()

t = Toplevel(tk)
Label(t, text='This one has the same custom icon.').pack()

tk.mainloop()
#####

I hope this is helpful to people...

(now to see if I can figure out how to submit a patch in Sourceforge)

--
John.

« January 2009 »
Su Mo Tu We Th Fr Sa
123
45678910
11121314151617
18192021222324
25262728293031
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: