trying to change default icon in tkinter Tk() at the top of the "window"

1.8k Views Asked by At

Not sure why I'm getting an error I followed like 6 guides to make a .ico file and literally copied two tutorials trying all types of combinations of relative non relative file path

from tkinter import *
from PIL import ImageTk, Image


root = Tk()
root.title('Local Host Store')
root.geometry("500x500")

root.iconbitmap('pleasegod1.ico')

#mainloop

root.mainloop()

                        <ERROR CODE BELOW>
[Running] python -u "c:\Users\Eric\Desktop\LocalHostStore\main.py"
Traceback (most recent call last):
  File "c:\Users\Eric\Desktop\LocalHostStore\main.py", line 10, in <module>
    root.iconbitmap('pleasegod1.ico')
  File "C:\Users\Eric\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 2071, in 
wm_iconbitmap
    return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "pleasegod1.ico" not defined
2

There are 2 best solutions below

0
On

Try to type the entire image path or place the image in the same code file and make sure that the image type is .ico

0
On

Alternate solution is to use Tk.iconphoto() method and not Tk.iconbitmap(). Instead root.iconphoto(True,PhotoImage(file="C:/Users/Eric/Desktop/GuiFlowerIcon.png")) worked fine, it's just not using a .icon file I suppose. Not efficient to trouble shoot Tk.iconbitmap() for my use case. this just changes the icon at the top of the tkinter window from the default tkinter library "trademark" icon to a custom one of your own. iconphoto can be png.

Side note: I went through great lengths to follow tutorials to create .ico file, my file was 15x15 px grid in ms paint saved as bitmap 24 I think and then converted file type to .ico and windows recognized it as such, wheras if you try to change big png file to .ico it won't let you actually even change the file extension... super odd.

The problem still may be it doesn't recognize it as .ICO file if anyone have any tips to make a .ico file that python tkinter can recognize or how it even goes about this in.. c language? behind the scenes that would be super cool as I hope to unpack tkinter one day and how it works under the hood.