iconphoto() method in Tkinter | Python

iconphoto() method is used to set the titlebar icon of any tkinter/toplevel window. But to set any image as the icon of titlebar, image should be the object of PhotoImage class.

Syntax:

iconphoto(self, default = False, *args)

Steps to set icon image –



from tkinter import Tk
master = Tk()

photo = PhotoImage(file = "Any image file")
master.iconphoto(False, photo)

Set 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 in the titlebar icons. The function also scales provided icons to an appropriate size.

Code #1: When PhotoImage is provided.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Tkinter module
from tkinter import * from tkinter.ttk import *
  
# Creating master Tkinter window
master = Tk()
  
# Creating object of photoimage class
# Image should be in the same folder
# in which script is saved
p1 = PhotoImage(file = 'info.png')
  
# Setting icon of master window
master.iconphoto(False, 'info.png')
  
# Creating button
b = Button(master, text = 'Click me !')
b.pack(side = TOP)
  
# Infinite loop can be terminated by
# keyboard or mouse interrupt
# or by any predefined function (destroy())
mainloop()

chevron_right


Output:

Exception: If you provide an image directly instead of PhotoImage object then it will show the following error.

Code #2: When PhotoImage object is not provided.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing Tkinter module
from tkinter import * from tkinter.ttk import *
  
# Creating master Tkinter window
master = Tk()
  
# Setting icon of master window
master.iconphoto(False, 'info.png')
  
# Creating button
b = Button(master, text = 'Click me !')
b.pack(side = TOP)
  
# Infinite loop can be terminated by
# keyboard or mouse interrupt
# or by any predefined function (destroy())
mainloop()

chevron_right


Output:

Traceback (most recent call last):
  File "C:\Users\Admin\Documents\GUI_python\geeks.py", line 14, in 
    master.iconphoto(False, 'info.png')
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1910, in wm_iconphoto
    self.tk.call('wm', 'iconphoto', self._w, *args)
_tkinter.TclError: can't use "info.png" as iconphoto: not a photo image


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.