Updated 2015-11-02 01:58:44 by blackcrow

The easiest way is to use

wm iconphoto

because

wm iconbitmap

only works for with bitmap, no color.

( bll 2015-10-27. False. wm iconbitmap works with windows .ico files. You can put several different sizes of .png images in the .ico file and it works just fine. Tested and working on Windows XP, Vista, 7, 8, 10. e.g. wm iconbitmap . -default bdj_icon.ico. I use wm iconphoto on the unix variants and wm iconbitmap on windows. )

Below are a few other methods.

Use iconwindow

On the one hand, one has to understand that this is NOT possible to do successfully across all platforms and window managers.

For example, the method does not work for Microsoft Windows (tested with tk8.5).

There are just too many variations.

However, here is something to attempt. Note that out of the box, Tk accepts only three image file formats - GIF, PGM, and PPM.

If you want to use something other than that, take a look at Img, which adds additional photo image types.
 # Sample created by Larry W. Virden, with the kind assistance of
 #    Donal K. Fellows
 # Date: Nov 6, 2002

 package require Tcl
 package require Tk
 
 if { $argc != 1 } {
        puts stderr "$argv0 gif_pathname"
        exit
 }
 set icon_pathname [lindex $argv 0]
 
 set i [image create photo .i1 -format gif -file $icon_pathname ]
 
 set b [button .b1 -text hello]
 
 pack $b
 toplevel .icon;pack [label .icon.l -image $i]
 wm iconwindow . .icon

You may need to add the following to this code:
  wm withdraw .
  wm state . normal

to force the window manager to recognize the change in icon.

[Geoff Battye] mentions on comp.lang.tcl that the above code results in a color icon showing up on the desktop with window managers like fvwm (and lv did the above work under cde). However, it didn't result in KDE showing a color icon in its title bar, alt-switching panel or its taskbar. So, MGS developed the code documented in KDE window icons to solve that need.

GWM the above code crashes tcl/tk8.4.11

However... from ask and it shall be given page:

GWM I have changed the icon on my toplevel window to an xbm file using
  wm iconbitmap . @folder.xbm

and the icon is restricted to the 2 colours used by the wish feather icon. How can I change the colours to red & green or some other horrible colour scheme?

use .icon icon file for bitmap, for Windows

This method doesn't work as of tk8.5 - it gives error "cannot read bitmap" on Windows Vista.

MG If you're using Windows XP, you should be using a Windows .ico icon file, not a .xbm. Taking a Windows Bitmap and just renaming it from "foo.bmp" to "foo.ico" will usually (always?) give you a valid icon file, and the Img package can also write photo images in the ico format. There are also various free programs mentioned on the Wiki (though I don't have links to hand) which can create icons in many different sizes/color depths/etc. A Wiki search for "Windows Icons" will probably find some of those.

GWM Thanks - you don't need the @ needed for xbm:
 wm iconbitmap . /tcl/examples/tk8.4.5/win/rc/wish.ico

works nicely if you have the examples installed.

See also:


[blackcrow] - 2015-10-28 03:04:04

In current version of their distribution, ActiveState removed any regular possibility to replace their Company Logo in Windows taskbar by custom icons. Even in the wish dll. So, if you like to display an icon related to functionality of your script, better switch to a distribution at https://bitbucket.org/tombert/tcltk/downloads. Otherwise, "wm iconbitmap" and "wm iconphoto" would only set the titlebar icon, not the taskbar icon.

bll 2015-10-27 If you (re)start the program with tclsh, you can set the icon as you wish. See: google groups: Replace wish's taskbar icon with another?

[blackcrow] Yes, that worked under Windows up to 8.1 with prevoius distrib of ActiveState's Tcl. The trick is to force using the wish.dll instead of wish.exe. But in current version, 8.6.4.1, ActiveState killed even the possibility of that workaround. The later version avaliable at https://bitbucket.org/tombert/tcltk/downloads, 8.6.4.5, works perfect, tested with Win 7 , 8 & 8.1. ;)

bll 2015-10-27. Works for me. Looking at windows 10 with ActiveTcl 8.6.4.1 right now. I just tested recently with win 8 and it works there too.

[blackcrow] So it was with Win 8 and ActiveTcl 8.6.4.1, before I used teacup to update Tk and some other packages. After that, I had no more chance to change the ugly AcitveState taskbar icon. With the distrib mentioned in my last post, there are no hacks or tricks required: simply use: "wm iconbitmap . myicon.ico", and taskbar icon is changed correctly.