Python tkinter вставить gif

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() #Title window.title('Grand Canyon') #display attributes canvas = Canvas(window, width = 500, height = 500) canvas.pack() #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.

Output

How to show a GIF animation image in Tkinter - Python

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

Читайте также:  Мои альбомы

Background

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.

Источник

Оцените статью