Gopi Bollineni
Gopi Bollineni

Reputation: 43

How to insert custom icon on the toolbar using TKINTER in PYTHON specifically in LINUX?

I tried to do the same using the following code:

# A Python program to create root window with some options
from tkinter import *

# create top level window
root = Tk()

# set window title
root.title('Tkinter Demo')

# set window size
root.geometry('500x500')

# set window icon
root.wm_iconbitmap('PYTHON\Graphical User Interface[GUI]\pythonlogo.ico') # this line won't work for linux

# display window and wait for any events
root.mainloop()

and i also tried:

# A Python program to create root window with some options
from tkinter import *

# create top level window
root = Tk()

# set window title
root.title('Tkinter Demo')

# set window size
root.geometry('500x500')

# set window icon
img = PhotoImage(file='PYTHON/Graphical User Interface[GUI]/pythonlogo.xbm')
root.tk.call('wm', 'iconphoto', root._w, img)

# display window and wait for any events
root.mainloop()

The above code worked totally fine in Windows, but for linux i used the image file formats: .png,.jpeg,.gif,.ico,.xbm,.xpm but none of them worked.

I know the .png,.jpeg cannot be used. And, .gif,.ico formats only work on windows. I found from several forums that .xpm or .xbm formats worked fine. But, that was not true in my case.

I got the following error with .xpm as well as .xpm formats:

_tkinter.TclError: couldn't recognize data in image file "PYTHON/Graphical User Interface[GUI]/pythonlogo.xbm"

I've been struggling with this since quite a while. Any pointers or help towards the topic is well appreciated.

Thank You!

Upvotes: 3

Views: 3796

Answers (1)

j_4321
j_4321

Reputation: 16169

You can use root.wm_iconphoto(True, img), where img is a PhotoImage created from a .png or .gif image file.

Here is the docstring of the method:

wm_iconphoto(default=False, *args) method of tkinter.Tk instance Sets the titlebar icon for this window based on the named photo images passed through args. If default is True, this is applied to all future created toplevels as well.

The data in the images is taken as a snapshot at the time of invocation. If the images are later changed, this is not reflected to the titlebar icons. Multiple images are accepted to allow different images sizes to be provided. The window manager may scale provided icons to an appropriate size.

On Windows, the images are packed into a Windows icon structure. This will override an icon specified to wm_iconbitmap, and vice versa.

On X, the images are arranged into the _NET_WM_ICON X property, which most modern window managers support. An icon specified by wm_iconbitmap may exist simultaneously.

On Macintosh, this currently does nothing.

In your code:

from tkinter import *

# create top level window
root = Tk()

# set window title
root.title('Tkinter Demo')

# set window size
root.geometry('500x500')

# set window icon
img = PhotoImage(file='PYTHON/Graphical User Interface[GUI]/pythonlogo.png')
root.wm_iconphoto(True, img)

# display window and wait for any events
root.mainloop()

I use this in Linux and it works.

Upvotes: 3

Related Questions