Tkinter, how to show a gif

In my 100 days of code journey, I'm learning so many new things. But I'm still searching for my path. In the past few years, I worked a lot with desktop applications linked to a database. So, I'd like to learn something like that. After a few searches, I find in PostgreSQL my object-relational database system. Now I have to find something to build the GUI for a final user. I like Python, so I'm searching for something that let me satisfy these requirements. On the web, I found many tutorials and resources and my choice is between Tkinter and PyQT. I tried to connect a Tkinter form to a PostgreSQL db and I found it pretty easy. Waiting for an idea for a desktop app to build, I want to try to build a splash screen. Maybe the result will be not good for a production app, but it was funny to try. So, let's start.
First of all, you need to install a package that adds image processing capabilities to your Python interpreter. This package is Pillow. Pillow is a fork of PIL (Python Imaging Library). Here the link.
So from your terminal, you can type: pip install Pillow. I had a VirtualEnv so I install Pillow in it.
Then I found on Stackoverflow this code:


# import the necessary packages
from itertools import count
from PIL import Image, ImageTk
import tkinter as tk
class ImageLabel(tk.Label):
    """this label displays images and if they are gifs plays them """
    def load(self, img):
        if isinstance(img, str):
            img = Image.open(img)
        self.loc = 0
        self.frames = []
    #next loop is to iterate frames
        try:
            for i in count(1):
                self.frames.append(ImageTk.PhotoImage(img.copy()))
                img.seek(i)
        except EOFError:
            pass
        try:
            self.delay = img.info['duration']
        except:
            self.delay = 100
        if len(self.frames) == 1:
            self.config(image=self.frames[0])
        else:
            self.next_frame()
    def unload(self):
        self.config(image=None)
        self.frames = None
    def next_frame(self):
        if self.frames:
            self.loc += 1
            self.loc %= len(self.frames)
            self.config(image=self.frames[self.loc])
            self.after(self.delay, self.next_frame)
root = tk.Tk()
lbl = ImageLabel(root)
lbl.pack()
lbl.load('Logo.gif')
root.geometry("500x400+600+300")
root.mainloop()


As I understand, the problem to show a gif is to iterate the frames in it but
we can loop over them until we get the EOF error.
I add also the geometry method to the root widget before the mainloop.
The geometry method is used to set the dimensions of the window and
to set the position of the main window on the user's desktop.
In my code
root.geometry("500x400+600+300")
I set 500 width x 400 height + 600 position right + 300 position down.
For this exercise, I built also a gif on Giphy.com... here the link.

That's all. If you have any suggestions, or if you found any kind of errors,
please feel free to leave a comment.

Commenti

AddToAny

Post popolari in questo blog

Something about me

PyQt5 and PyCharm