Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding Icon To Tkinter Window

4,018 views
Skip to first unread message

Wildman

unread,
Mar 5, 2016, 11:47:27 AM3/5/16
to
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.

--
<Wildman> GNU/Linux user #557453
The cow died so I don't need your bull!

Grant Edwards

unread,
Mar 5, 2016, 1:08:27 PM3/5/16
to
On 2016-03-05, Wildman <best...@yahoo.com> 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.

You'll have to be a lot more specific about what you mean by "add an
icon to a window". Do you just want to display an .ico file within a
window?

--
Grant






Wildman

unread,
Mar 5, 2016, 1:10:15 PM3/5/16
to
On Sat, 05 Mar 2016 10:47:09 -0600, Wildman 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.

I found this and it works in Linux but only with black
and white xbm images (I would prefer color):

root.wm_iconbitmap('@myicon.xbm')

I found some info in a forum that talked about putting
a color icon in a Label on a separate window and passing
that to the main window as the icon. But there were no
code examples or explanations. So far I have not been
able to figure it out. I would appreciate any help.

--
<Wildman> GNU/Linux user #557453
"Be at war with your vices, at peace with your neighbors,
and let every new year find you a better man."
-Benjamin Franklin

Christian Gollwitzer

unread,
Mar 5, 2016, 1:36:31 PM3/5/16
to
Am 05.03.16 um 19:10 schrieb Wildman:
> On Sat, 05 Mar 2016 10:47:09 -0600, Wildman 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.
>
> I found this and it works in Linux but only with black
> and white xbm images (I would prefer color):
>
> root.wm_iconbitmap('@myicon.xbm')

iconphoto is the newer API for color icons. I am a bit surprised that it
is not wrapped in Tkinter, which is odd. You can still call it via eval:

import Tkinter
from Tkinter import Tk
root = Tk()
img = Tkinter.Image("photo", file="appicon.gif")
root.call('wm','iconphoto',root._w,img)


If you Tk is recent enough (8.6, you can find out by doing
root.eval('info patchlevel')), you can use .png files in addition to
.gif. If you need to read other image files, look into PIL and ImageTk.

Christian

Mark Lawrence

unread,
Mar 5, 2016, 1:39:49 PM3/5/16
to
On 05/03/2016 16:47, 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.
>

Would you please be kind enough to read this
http://www.catb.org/esr/faqs/smart-questions.html. Then tell us exactly
what you are trying to achieve, how you are trying to achieve it and the
error that you get, including the complete traceback if there is one.

TIA.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Wildman

unread,
Mar 5, 2016, 1:47:38 PM3/5/16
to
Sorry. I am talking about the titlebar or system icon.

Wildman

unread,
Mar 5, 2016, 1:50:48 PM3/5/16
to
On Sat, 05 Mar 2016 18:38:57 +0000, Mark Lawrence wrote:

> On 05/03/2016 16:47, 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.
>>
>
> Would you please be kind enough to read this
> http://www.catb.org/esr/faqs/smart-questions.html. Then tell us exactly
> what you are trying to achieve, how you are trying to achieve it and the
> error that you get, including the complete traceback if there is one.
>
> TIA.

Thank you, I will do that. I am new to Python and this group
but ignorance is no excuse. In the future I will strive to
do better.

Serhiy Storchaka

unread,
Mar 5, 2016, 2:56:10 PM3/5/16
to
On 05.03.16 18:47, 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.

On Windows:

root.wm_iconbitmap(default='myapp.ico')

.ico-file can contain several icons of different size.

Otherwise if TkVersion >= 8.5:

root.wm_iconphoto(True, PhotoImage(file='myapp16.gif'),
PhotoImage(file='myapp32.gif'), ...)

You can specify several icons of different size.

If TkVersion >= 8.5 you can use .png-files.


Terry Reedy

unread,
Mar 5, 2016, 4:39:00 PM3/5/16
to
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

Wildman

unread,
Mar 6, 2016, 12:53:58 AM3/6/16
to
On Sat, 05 Mar 2016 19:36:19 +0100, Christian Gollwitzer wrote:

> Am 05.03.16 um 19:10 schrieb Wildman:
>> On Sat, 05 Mar 2016 10:47:09 -0600, Wildman 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.
>>
>> I found this and it works in Linux but only with black
>> and white xbm images (I would prefer color):
>>
>> root.wm_iconbitmap('@myicon.xbm')
>
> iconphoto is the newer API for color icons. I am a bit surprised that it
> is not wrapped in Tkinter, which is odd. You can still call it via eval:
>
> import Tkinter
> from Tkinter import Tk
> root = Tk()
> img = Tkinter.Image("photo", file="appicon.gif")
> root.call('wm','iconphoto',root._w,img)

The above worked perfectly. Thank you very much.

> If you Tk is recent enough (8.6, you can find out by doing
> root.eval('info patchlevel')), you can use .png files in addition to
> .gif. If you need to read other image files, look into PIL and ImageTk.
>
> Christian

According to "root.eval('info patchlevel')" I have version 8.6.2.
PNG worked. That is good because PNG is a common file type for
Linux icons.

--
<Wildman> GNU/Linux user #557453

Wildman

unread,
Mar 6, 2016, 12:56:21 AM3/6/16
to
Thanks. I will file away the snippets for later use. The
code provided by Mr. Gollwitzer is working for me on Linux.

Wildman

unread,
Mar 6, 2016, 1:04:31 AM3/6/16
to
I appreciate your reply. Your code looks interesting but it
is over my head right now. I will save it for further study
and experimentation. Right now my problem was fixed by using
the code posted by Mr. Gollwitzer.

Wildman

unread,
Mar 6, 2016, 1:10:01 AM3/6/16
to
I apologize to the group for my lack of information in
my original post. I will do better in the future. I
very much want to remain in good standing in this group
because of all the knowledgeable people here.

Thanks again to everyone that replied and helped me
solve my problem.

Christian Gollwitzer

unread,
Mar 6, 2016, 4:17:12 AM3/6/16
to
Am 06.03.16 um 06:53 schrieb Wildman:
> On Sat, 05 Mar 2016 19:36:19 +0100, Christian Gollwitzer wrote:
>> import Tkinter
>> from Tkinter import Tk
>> root = Tk()
>> img = Tkinter.Image("photo", file="appicon.gif")
>> root.call('wm','iconphoto',root._w,img)
>
> The above worked perfectly. Thank you very much.

Then you should try if it works also with the last line replaced as

root.wm_iconphoto(True, img)

(as Serhiy and Terry wrote). My system python which I used to test this
was just too old to wrap iconphoto, yours should be up to date.

Christian


> According to "root.eval('info patchlevel')" I have version 8.6.2.
> PNG worked. That is good because PNG is a common file type for
> Linux icons.

PS: 8.6.2 is from mid 2014, that's OK - though we've had a brand new
release (8.6.5) in February 2016

Wildman

unread,
Mar 6, 2016, 11:30:25 AM3/6/16
to
On Sun, 06 Mar 2016 10:16:55 +0100, Christian Gollwitzer wrote:

> Am 06.03.16 um 06:53 schrieb Wildman:
>> On Sat, 05 Mar 2016 19:36:19 +0100, Christian Gollwitzer wrote:
>>> import Tkinter
>>> from Tkinter import Tk
>>> root = Tk()
>>> img = Tkinter.Image("photo", file="appicon.gif")
>>> root.call('wm','iconphoto',root._w,img)
>>
>> The above worked perfectly. Thank you very much.
>
> Then you should try if it works also with the last line replaced as
>
> root.wm_iconphoto(True, img)

That does not work...

$ ./makexface.py
Traceback (most recent call last):
File "./makexface.py", line 236, in <module>
root.wm_iconphoto(True, img)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1897, in __getattr__
return getattr(self.tk, attr)
AttributeError: wm_iconphoto

> (as Serhiy and Terry wrote). My system python which I used to test this
> was just too old to wrap iconphoto, yours should be up to date.
>
> Christian
>
>
>> According to "root.eval('info patchlevel')" I have version 8.6.2.
>> PNG worked. That is good because PNG is a common file type for
>> Linux icons.
>
> PS: 8.6.2 is from mid 2014, that's OK - though we've had a brand new
> release (8.6.5) in February 2016

I am using the latest release that is offered in the Debian
repository but I will look into upgrading from other sources.

Thanks for the info.

Rick Johnson

unread,
Mar 9, 2016, 11:39:40 AM3/9/16
to
On Saturday, March 5, 2016 at 12:08:27 PM UTC-6, Grant Edwards wrote:
> You'll have to be a lot more specific about what you mean by "add an
> icon to a window". Do you just want to display an .ico file within a
> window?

I think he wants to use his *OWN* icon on a window's title bar, instead of the default icon that Tkinter uses. It would help if he posted the code he tried, along with any exceptions that were raised.

Rick Johnson

unread,
Mar 9, 2016, 11:41:19 AM3/9/16
to
On Saturday, March 5, 2016 at 3:39:00 PM UTC-6, Terry Reedy wrote:
> 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).

Yes, because Tkinter is not as cross-platform as it's hyped to be.

Serhiy Storchaka

unread,
Mar 9, 2016, 12:11:10 PM3/9/16
to
On 06.03.16 18:30, Wildman via Python-list wrote:
> That does not work...
>
> $ ./makexface.py
> Traceback (most recent call last):
> File "./makexface.py", line 236, in <module>
> root.wm_iconphoto(True, img)
> File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1897, in __getattr__
> return getattr(self.tk, attr)
> AttributeError: wm_iconphoto

This works in Python 3.x. In 2.7 you should use

root.tk.call('wm', 'iconphoto', str(root), '-default', img, ...)

Note that it is worth to specify images for several different sizes (up
to 128x128 for OS X).

Joaquin Alzola

unread,
Mar 9, 2016, 1:40:27 PM3/9/16
to
> root.wm_iconphoto(True, img)

What are the images format allowed?
This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.
0 new messages