5

I am studying Python GUI from Python GUI Programming Cookbook. A certain task requires me to change the window icon by adding the following code to my recipe:

# Change the main windows icon
win.iconbitmap(r'C:\Python34\DLLs\pyc.ico')

Since I am using Linux, I have to change my path to /home/user/test.ico. After reading similar questions, I got to know that .ico is Windows only. I tried .gif but that doesn't work either. Existing SO articles I tried: tkinter TclError: error reading bitmap file Setting Application icon in my python Tk base application (On Ubuntu) tkinter.TclError: image "pyimage3" doesn't exist

All three of these were unhelpful. I got the following error with each:

In [3]: runfile('/home/bhedia/untitled1.py', wdir='/home/bhedia')
Traceback (most recent call last):

  File "<ipython-input-3-17a671578909>", line 1, in <module>
    runfile('/home/bhedia/untitled1.py', wdir='/home/bhedia')

  File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/bhedia/untitled1.py", line 58, in <module>
    img = tk.PhotoImage(file='test.ico')

  File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/tkinter/__init__.py", line 3403, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)

  File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/tkinter/__init__.py", line 3359, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)

TclError: couldn't recognize data in image file "test.ico"

In [4]: runfile('/home/bhedia/untitled1.py', wdir='/home/bhedia')
Traceback (most recent call last):

  File "<ipython-input-4-17a671578909>", line 1, in <module>
    runfile('/home/bhedia/untitled1.py', wdir='/home/bhedia')

  File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "/home/bhedia/anaconda3/envs/mitx/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/bhedia/untitled1.py", line 59, in <module>
    root.tk.call('wm','iconphoto',root._w,img)

TclError: can't use "pyimage2" as iconphoto: not a photo image

So, I am not sure how to change my window's icon when using the Tkinter library on Linux.

  • 1
    The error on In [3] says, you are using ico file which you shouldn't. You should use .xbm fil as said in [first link](https://stackoverflow.com/questions/11176638/tkinter-tclerror-error-reading-bitmap-file) you wrote. – Lafexlos Jul 28 '17 at 06:18
  • Naively tkinter handles GIF& PNG files with no problem. ICO files it doesn't recognize. You'll need to pass in a PNG or GIF file. – Mike from PSG May 16 '19 at 15:24
  • Possible duplicate of [Which file formats can I use for tkinter icons?](https://stackoverflow.com/questions/42705547/which-file-formats-can-i-use-for-tkinter-icons) – dalanicolai Oct 23 '19 at 13:32

2 Answers2

3

I'm on Linux Ubuntu (18.04) and using .ico files didn't work. (xbm, xpm, png, gif did not either). Im also using tkinter, as PySimpleGUI is based on it.

A base64 encoded gif image should work. At least it did for me:

with open('./assets/icon.gif', 'rb') as icon_gif:
            icon_base64 = base64.b64encode(icon_gif.read())

# In PySimpleGUI:
sg.SetOptions(icon=icon_base64)

# In tkinter directly, probably:
win.iconbitmap(icon_base64)

I'm not sure about tkinter, as I'm not directly working with it. From the source code of PySimpleGUI (v4.19.0), I'd say this should do it. Please correct me, if necessary. I'll update this answer.

Wu Wei
  • 1,827
  • 1
  • 15
  • 27
  • Hi @anarchist912, your answer sounds convincing, but it does not have effect on my ubuntu with PySimpleGUI 4.34. Is there any subtle things that I'd need to be aware of? Say, does the file have to be in gif format (rather than ico or xbm)? Does the icon size has to be in a specific size? For example, I tried several icon format and size from [this sample image](https://www.iconsdb.com/guacamole-green-icons/emoticon-icon.html), but my GUI app still just shows the PySimpleGUI's default icon. – RayLuo Feb 01 '21 at 05:44
  • @RayLuo base64 encoded GIF still works with 4.19.0. Here's my base64 encoded string I pass in `sg.SetOptions(icon=icon_base64)`. Try this: https://pastebin.com/0jNFrnkL — Dimensions are 50x50. – Wu Wei Feb 01 '21 at 08:54
  • Thanks, Anarchist, for getting back to me quickly! Appreciate it. Your icon still does not show up on my computer, but I figure that serves as a confirmation that the issue is probably not about the icon data. I guess it is something else on my Linux distro's Desktop Environment. I still consider this as a decisive outcome. Thank you again! – RayLuo Feb 01 '21 at 09:46
2

Use this code:

10 icon = PhotoImage(file='yourfile.ico')   
20 root.tk.call('wm', 'iconphoto', root._w, icon)

and make sure yourfile.ico is in the same folder as yourfilename.py.

peaceout from Pakistan