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.