How to add image/icon in tkinter window in python|How to add image in Tkinter|How to changle tkinter icon|how to use image in tkinter python

Introduction

 When We say about GUI based application, images play a vital role. We will add it to the root window or main window.

Note:-This code will not change the titlebar icon.Steps for that purpose are quite different .

Tkinter
Tkinter gives us the way in through which we can build a GUI in Python.It can be said as Python interface to the Tk GUI toolkit,which comes with Python.Tkinter is the standard library in Python. When Python and Tkinter are combined then they provide a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.

Basic steps to start your tkinter-python program are:-
  • Import Tkinter module.
  • Create main window of GUI generally known as Root window.
  • Add widgets you want.
  • Enter the main event loop using root.mainloop() to take action against each operation defined by you.
Widgets
Tkinter provides user to create different objects in their window which are known as Widgets.Example of such Widgets are; buttonslabels and text boxes used in a GUI application.

Supported image formats for tkinter:-

Currently, the "photoimage" widget supports several image file formats such as PGM, PPM, GIF, and PNG as of Tkinter 8.6.

You can use Libraries like Pillow to use more image formats like JPG,JPEG etc.we can also display images using Label widget.

Source code-

from tkinter import Tk
root=Tk()
photo = PhotoImage(file = "Any image file")
root.iconphoto(False, photo)
#---------------CODE ENDS--


For Tutorial Watch My YouTube Video:-


About the code
To use the above code you have to paste the path of image file as directed in the first line of the code.
  • Library used - tkinter 
  • Functions and operators used in the program are:
  • PhotoImage()
  • root.iconphoto()

About the functions used:-

photoimage():-
To display images in labels, buttons, text widgets, and canvases , the PhotoImage class is used, which we get in tkinter package.

root.iconphoto():-
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.

"Hope it helped"
Have anything in mind? Let me 
know in the comment section👇

Next Post Previous Post
No Comment
Add Comment
comment url