Display Images in Tkinter Using Labels

Before understanding how to display images in Tkinter using labels? Let’s have a sneak peek about Python GUI first. In python there are various methods for creating graphical user interfaces (GUIs). Most Common used are:-

  1. Tkinter – It is Python Standard GUI Framework, open source and simple.
  2. Kivy – It is open source, supports multiple platforms such as Windows, Linux, etc. and contains over 20 widgets in its toolkit.
  3. wxPython – It is implemented as a Python extension module and for cross-platform GUI library wxwidgets support as an open source wrapper.

Tkinter is Python’s Standard GUI Framework.It is an open source platform and most commonly used because of its simplicity. Any beginner in python can create their very own GUI using tkinter by just using these simple steps:-

  1. Import Tkinter module.
  2. Create the GUI application main window.
  3. Add widgets to the GUI application.
  4. Create a main event loop to perform action for every event triggered by the user.
In Tkinter display images using label

In this blog, we are going to learn how to display images in python tkinter using labels for your GUI. We are going to understand how to display .png and .jpg image files.

As the Tkinter PhotoImage module currently does not support .jpg files, we are going to use a pillow library which will help us to display .jpg files in our GUI.

For .PNG Files

Step1.  Import tkinter library and all of it modules using the following command. ‘*’ is used to signify that we want to import all modules from the tkinter library.

from tkinter import *

Step2. Now, we need to create a root window which is the main application window of our program, it contains a title bar and borders.It should be created before any widgets.

root=  Tk()

Step3. With the help of the geometry method we will set the width X height of our window.

root.geometry=("500x500")

Step4. PhotoImage method is used to add user defined images in the application and pass the parameter file=”path_of_png_image_file”.

pic=PhotoImage(file="blog4-mainpage.png")

Step5. Now, with the help of Label widget we can display a box in our window that contain text or image. Here we are displaying image so will pass parameter image=pic. After creating label pack it.

label=Label(image=pic)
label.pack()

Step6. Create a main event loop to perform action for every event triggered by the user.

root.mainloop()

Output :-

PNG file output

For .JPG File

Step1. Install Pillow by running the below command in your terminal.

pip install pillow

Step2.  Import tkinter library and all of it modules using the following command. ‘*’ is used to signify that we want to import all modules from the tkinter library. Import Image,ImageTk from PIL library.

from tkinter import *
from PIL import Image,ImageTk

Step3. Now, we need to create a root window which is the main application window of our program, it contains a title bar and borders.It should be created before any widgets.

root=  Tk()

Step4. With the help of the geometry method we will set the width X height of our window.
root.geometry=("500x500")

Step5. Image module will help us to load images from file and ImageTk module contains support to create and modify Tkinter PhotoImage module.

image=Image.open('avatar.jpg')
pic=ImageTk.PhotoImage(image)

Step6. Now, with the help of Label widget we can display a box in our window that contain text or image. Here we are displaying image so will pass parameter image=pic. After creating label we need to pack it.

label=Label(image=pic)
label.pack()

Step7. Create a main event loop to perform action for every event triggered by the user.

root.mainloop()

Output :-

JPG file output

Quick Revision :-

  1. Import Tkinter module.
  2. Create the GUI application main window.
  3. Load the image
  4. Add Image to the Label Widget.
  5. Pack the Label.
  6. Create a main event loop to perform action for every event triggered by the user.