Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.6 tkinter window icon on Linux error

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.

like image 649
Sannidhya Sandheer Avatar asked Jul 27 '17 22:07

Sannidhya Sandheer


2 Answers

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.

like image 178
anarchist912 Avatar answered Nov 19 '22 21:11

anarchist912


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

like image 27
AbdulRehmanTareen Avatar answered Nov 19 '22 21:11

AbdulRehmanTareen