Tkinter, iconbitmap and Windows XP

J

John Fouhy

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: wm
iconbitmap 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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top