Which file formats can I use for tkinter icons?
Asked Answered
C

2

5

I know this might be obvious, but in tkinter you can set an icon, but I have found it really hard to find one. I just wanted to know if you have to use the .ico format for the file or if there is a way to use .png or .jpeg files.

Currently I have

window = Tkinter.Tk()
window.title("Weclome!")
window.geometry("200x300")
window.wm_iconbitmap("Icon.ico")
window.configure(background = "Black")

That is the whole setup I have and I just want to know about line 4:

window.wm_iconbitmap("Icon.ico") 

Thanks for responding to my question, although i am sorry for not spending more time looking into the question rather than just asking here.

Chicalote answered 9/3, 2017 at 21:15 Comment(0)
E
7

Let's start by reading the documentation!

The documentation at effbot.org says the following regarding iconbitmap(bitmap=None)

Sets or gets the icon bitmap to use when this window is iconified. This method is ignored by some window managers (including Windows).

Note that this method can only be used to display monochrome icons. To display a color icon, put it in a Label widget and display it using the iconwindow method instead.

Same as wm_iconbitmap.

So here's the documentation about iconwindow(window=None):

Sets or gets the icon window to use as an icon when this window is iconified. This method is ignored by some window managers (including Windows).

Same as wm_iconwindow.

window

The new icon window. If omitted, the current window is returned.

According to this other documentation, which actually says the same things as the docstrings of the homonymous method for tkinter in (at least) Python 2.7, 3.5 and 3.6:

wm_iconbitmap(self, bitmap=None, default=None)

Set bitmap for the iconified widget to bitmap. Return the bitmap if None is given.

Under Windows, the default parameter can be used to set the icon for the widget and any descendents that don't have an icon set explicitly. default can be the relative path to a .ico file (example: root.iconbitmap(default='myicon.ico') ). See Tk documentation for more information.

So here's the original Tk documentation:

wm iconbitmap window ?bitmap?

If bitmap is specified, then it names a bitmap in the standard forms accepted by Tk (see the Tk_GetBitmap manual entry for details). This bitmap is passed to the window manager to be displayed in window's icon, and the command returns an empty string. If an empty string is specified for bitmap, then any current icon bitmap is canceled for window. If bitmap is specified then the command returns an empty string. Otherwise, it returns the name of the current icon bitmap associated with window, or an empty string if window has no icon bitmap.

From my understanding of Tcl, here window is your toplevel window (either an instance of Tk or Toplevel).

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.

Not very concrete and thus helpful answer so far.


My conclusion

The iconbitmap function (or method, depending on the programming language) should be used to set a bitmap image to the window when the window is iconified.

On Windows you're allowed to set 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.

So which images are bitmaps?

  1. xbm and xpm (for X Window System)

    According to the Wikipedia article to which I linked "bitmap" to above:

    The X Window System uses a similar xbm format for black-and-white images, and xpm for color images. ...

  2. BMP file format

  3. Netpbm format

  4. .wbmp

  5. ILBM

    ...

So most of the bitmap file formats are not cross-platform! In other words, if someone tells you to use a xbm image for the icon, it may not work on your platform because xbm are bitmaps for X Window System.

Note: even after this answer you may still have problems!


Other possible useful articles

Eglantine answered 9/3, 2017 at 22:7 Comment(0)
H
3

I was struggling a lot to find an answer too but finally I peeked into the source code of idle3.6 where I found the following piece of code:

# 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)
else:
    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(master=root, file=iconfile)
             for iconfile in iconfiles]
    root.wm_iconphoto(True, *icons)

I searched through all files in the idlelib folder for .ico and .png using the rummage software.

So finally I managed to get the window icon working (on GNU-linux with TkVersion>=8.6) with the following two lines:

icon = PhotoImage(master=root, file='icon.png')
root.wm_iconphoto(True, icon)

where I put the icon directly in my application folder.

From the idle code it seems to me that on Windows still only .ico files are supported.

Hydrant answered 21/10, 2019 at 0:2 Comment(1)
I accidentally answered the question I was looking for myself. However, despite the question asked here is slightly different, I think my answer is still quite useful.Hydrant

© 2022 - 2024 — McMap. All rights reserved.