How to make a loading gif in PyQT5?

PyQt5 is a GUI tool stash that can be utilized to foster GUI applications in Python. It gives numerous modules that can assist with building different parts of the GUI application.

self.loading = QLabel() # create the QLabel
self.layout.addWidget(self.loading) # add it to our layout
movie = QMovie("myGifLoading.gif") # Create a QMovie from our gif
self.loading.setMovie(movie) # use setMovie function in our QLabel
self.loading.setFixedSize(60, 60) # set its size
self.loading.setMaximumWidth(50) # set Max width
movie.start() # now start the gif
# and to stop the gif
movie.stop()

# you use show() and hide() function to make it visible or not where you want it

0

6180efb1bfb4b3cb3f52774d

How to create a loading in pyqt5

self.loading = QLabel() # create the QLabel
self.layout.addWidget(self.loading) # add it to our layout
movie = QMovie("myGifLoading.gif") # Create a QMovie from our gif
self.loading.setMovie(movie) # use setMovie function in our QLabel
self.loading.setFixedSize(60, 60) # set its size
self.loading.setMaximumWidth(50) # set Max width
movie.start() # now start the gif
# and to stop the gif
movie.stop()

# you use show() and hide() function to make it visible or not where you want it

0

6180efb6bfb4b3cb3f5277d9

Loading in pyqt5

self.loading = QLabel() # create the QLabel
self.layout.addWidget(self.loading) # add it to our layout
movie = QMovie("myGifLoading.gif") # Create a QMovie from our gif
self.loading.setMovie(movie) # use setMovie function in our QLabel
self.loading.setFixedSize(60, 60) # set its size
self.loading.setMaximumWidth(50) # set Max width
movie.start() # now start the gif
# and to stop the gif
movie.stop()

# you use show() and hide() function to make it visible or not where you want it

0

6180efb6bfb4b3cb3f5277de

Gimp make a gif

// On gimp 2.10
1.	Select File Menu > Open as Layers > Select all images you want to be in
	the GIF > Open
2.	In layers tab order your images > The GIF sequence will start with your
	bottom layer and run through each layer bottom to top.
3.	Select Filters from main menu > Animation > Click Optimize for GIF
4.	To view GIF > Filter > Animation > Playback
5.	To save GIF > Select File > click Export as
6.	Name your GIF and choose folder  > Select File Type > Click GIF Image
7.	SelectAs Animation’ > Select ‘Loop Forever’.
	To change speed between each image change the delay
8.	Click Export

1

611c18343baea304b018d64b

Page loading gif bootstrap

<div class="spinner-border text-primary" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-secondary" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-success" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-danger" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-warning" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-info" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-light" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-dark" role="status">
  <span class="sr-only">Loading...</span>
</div>

Installation:

pip install PyQt5

Gif Link:  https://loading.io/

Approach:

  • Import module
  • Create window and labels
  • Load GIF
  • Start GIF using start()
  • Add mechanism to stop GIF using stop()
  • Execute code

Example:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import Qt
 
 
class LoadingGif(object):
 
    def mainUI(self, FrontWindow):
        FrontWindow.setObjectName("FTwindow")
        FrontWindow.resize(320, 300)
        self.centralwidget = QtWidgets.QWidget(FrontWindow)
        self.centralwidget.setObjectName("main-widget")
 
        # Label Create
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(25, 25, 200, 200))
        self.label.setMinimumSize(QtCore.QSize(250, 250))
        self.label.setMaximumSize(QtCore.QSize(250, 250))
        self.label.setObjectName("lb1")
        FrontWindow.setCentralWidget(self.centralwidget)
 
        # Loading the GIF
        self.movie = QMovie("loader.gif")
        self.label.setMovie(self.movie)
 
        self.startAnimation()
 
    # Start Animation
 
    def startAnimation(self):
        self.movie.start()
 
    # Stop Animation(According to need)
    def stopAnimation(self):
        self.movie.stop()
 
 
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
demo = LoadingGif()
demo.mainUI(window)
window.show()
sys.exit(app.exec_())

Output:

Also Read: Learn Arrays in C and C++

Leave a Reply

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