Pyqt5: add loading gift while function is running

Hi all, I’m writing a pyqt project and I have a single class for main windows:

class Ui(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi('./ui/smipack40.ui', self)
        self.logoImageMainWindows.setScaledContents(True)
        self.setWindowIcon(QtGui.QIcon('./ui/logo.png'))
    def function1():
        '''

app = QApplication([])
window = Ui()
window.show()
app.exec()

I would like create a dialog screen with a gif which runs while function1 is loading (writing files etc)
and maybe animate a gif file in the main windows (maybe in a angle of the screen or in the right side of the button which run the function1 when clicked) during function1 is running.

this is possible?

Yes, you would use QMovie to load a GIF file, and perhaps use a QLabel to display it.

If function1 (which should be a method containing a self parameter) takes a long time to run, you will need to make sure that the application’s event loop keeps running. This will ensure that the user interface does not freeze. There are a few different approaches to doing this.

First, verify that you can display a GIF file in a label, and that it animates as you expect. Then, look at ways to implement function1.

thank you

I tried with the following code:

class Ui(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi('./ui/logo.ui', self)
        self.logoImageMainWindows.setScaledContents(True)
        self.setWindowIcon(QtGui.QIcon('./ui/logo2.png'))
        self.logoImageMainWindows.resize(113,70)
        self.logoImageMainWindows.setPixmap(QPixmap('./ui/logo2.png'))
        self.loadVideo.setText('')
        self.movie = QMovie(self)
        self.movie.setFileName("./ui/loadingVideo.gif")
        
    def startAnimation(self):
        self.movie.start()

    def stopAnimation(self):
        self.movie.stop()
        
    def onConnectionClicked(self):
        self.startAnimation()
        while true:
             ....
        self.stopAnimation()
     

the windows opens but when I click button the movie not starts

Do you have any idea?
many thanks

I am guessing that the onConnectionClicked method is called when the button is clicked. It may be useful for you to verify that.

To begin with, remove the loop from the onConnectionClicked method and verify that the animation starts.