Learn Python PyQt

PyQt show gif

Using the QMovie class within PyQt, it becomes a seamless process to display (animated) GIFs. This guide walks you through the steps to achieve this, allowing for multiple GIF displays if desired.

Recommended Resource: Create Desktop Apps with Python PyQt5

Displaying Animated GIFs in PyQt

By leveraging the QMovie class, you can easily import a GIF file and present it on a label. Notably, while QMovie is typically for movies, it can handle (animated) GIFs effortlessly.

The example below demonstrates how to showcase an animated gif named “earth.gif”. Although our provided screenshot is static, the actual GIF animates and plays within the program window.
pyqt animated gif

For ease of access, you can either specify the complete path to the GIF or simply place it in the same directory as your program file.

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QMovie
import sys

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(250, 250)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        # Define a label for displaying GIF
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(25, 25, 200, 200))
        self.label.setMinimumSize(QtCore.QSize(200, 200))
        self.label.setMaximumSize(QtCore.QSize(200, 200))
        self.label.setObjectName("label")

        # Embed the label into the main window
        MainWindow.setCentralWidget(self.centralwidget)

        # Integrate QMovie to the label and initiate the GIF
        self.movie = QMovie("earth.gif")
        self.label.setMovie(self.movie)
        self.movie.start()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(window)
    window.show()
    sys.exit(app.exec_())

Code Breakdown

  1. Module Imports: Start by importing the necessary PyQt modules and other dependencies.

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtGui import QMovie
    
  2. Window Initialization: Configure the primary PyQt window attributes.

    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(250, 250)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    
  3. GIF Label Creation: Designate a label within which the GIF will be displayed.

    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(25, 25, 200, 200))
    self.label.setMinimumSize(QtCore.QSize(200, 200))
    self.label.setMaximumSize(QtCore.QSize(200, 200))
    self.label.setObjectName("label")
    
  4. Label Integration: Add the freshly created label to the primary window.

    MainWindow.setCentralWidget(self.centralwidget)
    
  5. GIF Loading and Display: Incorporate the GIF using QMovie, connect it to the label, and kickstart its playback.

    self.movie = QMovie("earth.gif")
    self.label.setMovie(self.movie)
    self.movie.start()