How to show a GIF animation image in Tkinter – Python

In this blog, we will see how we can insert a GIF into a GUI using Python.
We will be using the Tkinter library for this purpose.
Tkinter library is the de-facto library of Python for GUI creation.
The use of GIF makes our content more eye-catching and creative.

Python program to show GIF image animation in Tkinter

#importing library
from tkinter import *
#creating window
window = Tk()
window.title('Grand Canyon')
#display attributes
canvas = Canvas(window, width = 500, height = 500)
#GIF in my_image variable
#Give the entire file address along with the file name and gif extension
#Use \\ in the address
#The image given by me is C:\\UserAdmin\\Device\\Desktop2\\canyon.gif
my_image = PhotoImage(file='FIle Location\\Filename.gif')
canvas.create_image(0, 0, anchor = NW, image-my_image)

Code Explained

After importing the library Tkiner which we intend to use, we create a window and give a title to it.
The window and title commands are a part of the Tkinter library which is meant for GUI creation.
Then the code orients the display and formatting of size and structure are done.
A variable is chosen and a GIF is loaded onto it using the PhotoImage function. This is done by proving the complete path to the image along with the name and extension.
\\ are used as per Python format.


How to show a GIF animation image in Tkinter - Python

The above is the screenshot only. So it is not showing the animation output.


GIF stands for Graphics Interchange Format.
It was developed in mid-1987.
Once the use of GIF was considered unprofessional and not of much use.
Today it is widely used in creating a lot of moving images in the world of graphic content creation.
This is because the quickly catch the eye and are easily sharable.
Unlike the JPEG format, the gif format does not lose its quality. It uses lossless compression to preserve the image quality and keep it intact.
It is usually used when there are larger areas of uniform color and the total color number is lesser than the figure of 256.
They can be played by a mere click on them.

Also read: How to create drop-down menu in Tkinter in Python

4 responses to “How to show a GIF animation image in Tkinter – Python”

  1. aa says:

    The reason to load a GIF image is usually to have it animated.

  2. asdfasf says:

    small error = image-my_image last line of the code
    another error
    window is not showing the gif image

  3. Saumitra Deshpande says:

    frame2 = PhotoImage(file=imagefilename, format=”gif -index 2″)
    Try using this.

  4. Pushek says:

    Not sure you even tested your code bro. Error on line 15. Window doesn’t show. Animation doesn’t show.

Leave a Reply

Your email address will not be published. Required fields are marked *