Adding Icon To Tkinter Window

Terry Reedy tjreedy at udel.edu
Sat Mar 5 16:38:08 EST 2016


On 3/5/2016 11:47 AM, Wildman via Python-list wrote:
> Anybody have the correct method of adding an icon to a
> window?  I have found several code examples on the web
> but they all result in an error.  Thanks.

No single simple statement work for all situations.  You should have 
specified OS, Python version, and tcl/tk version.  (IDLE's Help -> About 
IDLE displays full tk version).

Here is the current code from idlelib.PyShell, written by Serhiy 
Storchaka, our current tkinter maintainer.

     # set application icon
     icondir = os.path.join(os.path.dirname(__file__), 'Icons')
     if system() == 'Windows':
         iconfile = os.path.join(icondir, 'idle.ico')
         root.wm_iconbitmap(default=iconfile)
     elif TkVersion >= 8.5:
         ext = '.png' if TkVersion >= 8.6 else '.gif'
         iconfiles = [os.path.join(icondir, 'idle_%d%s' % (size, ext))
                      for size in (16, 32, 48)]
         icons = [PhotoImage(file=iconfile) for iconfile in iconfiles]
         root.wm_iconphoto(True, *icons)

Non-windows pre-8.5 systems remain stuck with the tk icon.

I just tried changing the 'if' line to 'if False:' on my 3.5.1 (8.6.4) 
windows install and the 2nd, wm_iconphoto block worked here also.  It 
may be that it did not work on windows with 8.5 and before.

-- 
Terry Jan Reedy




More information about the Python-list mailing list