Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Loading Images in Tkinter using PIL
Next article icon

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 ofPhotoImage class.
Syntax: 
 

iconphoto(self, default = False, *args)


Steps to set icon image - 
 

from tkinter import Tkmaster = 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 isTrue, 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 thetitlebaricons. The function also scales provided icons to an appropriate size.
Code #1:When PhotoImage is provided. 
 

Python3
# Importing Tkinter modulefromtkinterimport*fromtkinter.ttkimport*# Creating master Tkinter windowmaster=Tk()# Creating object of photoimage class# Image should be in the same folder# in which script is savedp1=PhotoImage(file='info.png')# Setting icon of master windowmaster.iconphoto(False,p1)# Creating buttonb=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()

Output: 
 

iconphoto() method in Tkinter


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. 
 

Python3
# Importing Tkinter modulefromtkinterimport*fromtkinter.ttkimport*# Creating master Tkinter windowmaster=Tk()# Setting icon of master windowmaster.iconphoto(False,'info.png')# Creating buttonb=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()

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


 


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp