Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

why my pyqt5 gif animation is getting slower and slower…?



  • this codes get current status from status.txt and change gif which you want to show it detect and change your gif 1sec term and when i activate this code the gif file get slower and slower eventhough i don't change the status why this is happening and how can i prevent this??

    import sys, datetime
    from PyQt5.QtCore import Qt, QByteArray, QSettings, QTimer, pyqtSlot
    from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QSizePolicy, QVBoxLayout,
    QAction, QPushButton
    from PyQt5.QtGui import QMovie
    import random
    import time

    class GifPlayer(QWidget):
    def init(self, title, parent=None):
    QWidget.init(self, parent)
    self.emotion='0'
    self.read_file()
    self.gifFile="14.gif"

        self.movie = QMovie(self.gifFile, QByteArray(), self)
        size = self.movie.scaledSize()
        self.setGeometry(400, 400, 400, 400)
        self.setWindowTitle(title)
        self.movie_screen = QLabel()
        self.movie_screen.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
        self.movie_screen.setAlignment(Qt.AlignCenter)
        main_layout = QVBoxLayout()
        main_layout.addWidget(self.movie_screen)
        self.setLayout(main_layout)
        self.movie.setCacheMode(QMovie.CacheAll)
        self.movie_screen.setMovie(self.movie)
        self.movie.start()
        self.movie.loopCount()
    
        self.timer=QTimer(self)
        self.timer_one = QTimer(self)
        self.timer_one.start(1000)
        self.timer_one.timeout.connect(self.timeout_run)
    
    
    def read_file(self):
        with open("status.txt") as file_object:
                self.emotion=file_object.read()
                #print(self.emotion)
    
    @pyqtSlot()
    def change_gif(self):
        self.read_file()
        if self.emotion=='0':
                self.gifFile='5.gif'
        elif self.emotion=='1':
                self.gifFile='6.gif'
        elif self.emotion=='2':
                self.gifFile='7.gif'
        elif self.emotion=='3':
                self.gifFile='8.gif'
        elif self.emotion=='4':
                self.gifFile='9.gif'
        elif self.emotion=='5':
                self.gifFile='10.gif'
        elif self.emotion=='6':
                self.gifFile='11.gif'
        elif self.emotion=='7':
                self.gifFile='12.gif'
        elif self.emotion=='8':
                self.gifFile='13.gif'
        elif self.emotion=='9':
                self.gifFile='14.gif'
        elif self.emotion=='10':
                self.gifFile='5.gif'
        elif self.emotion=='11':
                self.gifFile='6.gif'
        elif self.emotion=='12':
                self.gifFile='7.gif'
        elif self.emotion=='13':
                self.gifFile='8.gif'
        elif self.emotion=='14':
                self.gifFile='9.gif'
        elif self.emotion=='15':
                self.gifFile='10.gif'
        elif self.emotion=='16':
                self.gifFile='11.gif'
        self.movie = QMovie(self.gifFile,QByteArray(), self)
        self.movie_screen.setMovie(self.movie)
        self.movie.start()
    
    
    def speed(self):
        self.movie.setSpeed(100)
    
    def stopAnimation(self):
        self.movie.stop()
    
    def timeout_run(self):
    
        sender = self.sender();
        current_time = datetime.datetime.now()
    
        if id(sender) == id(self.timer_one):
            self.change_gif()
    

    if name == "main":
    app = QApplication(sys.argv)
    player = GifPlayer("PI FACE")
    player.show()
    sys.exit(app.exec_())



  • @jonus said in why my pyqt5 gif animation is getting slower and slower…?:

    Purely at a guess: your old movies keep playing, somewhere?

    self.movie.loopCount()
    

    This statement has no effect, what's the point?

    self.timer=QTimer(self)
    

    This timer is never used/has no slot, what's the point?

    def stopAnimation(self):
        self.movie.stop()
    

    This method is never called, what's the point? The movie is never stopped. Maybe you should be calling it?

    def change_gif(self):
        self.movie = QMovie(self.gifFile,QByteArray(), self)
        self.movie_screen.setMovie(self.movie)
        self.movie.start()
    

    You already have a QMovie created, running, and shown in the label. Why do you create a new one, instead of just changing the file on the existing one? I would try sticking to one QMovie instance and see if that resolves the issue?



  • @jonus said in why my pyqt5 gif animation is getting slower and slower…?:

    Purely at a guess: your old movies keep playing, somewhere?

    self.movie.loopCount()
    

    This statement has no effect, what's the point?

    self.timer=QTimer(self)
    

    This timer is never used/has no slot, what's the point?

    def stopAnimation(self):
        self.movie.stop()
    

    This method is never called, what's the point? The movie is never stopped. Maybe you should be calling it?

    def change_gif(self):
        self.movie = QMovie(self.gifFile,QByteArray(), self)
        self.movie_screen.setMovie(self.movie)
        self.movie.start()
    

    You already have a QMovie created, running, and shown in the label. Why do you create a new one, instead of just changing the file on the existing one? I would try sticking to one QMovie instance and see if that resolves the issue?



  • This post is deleted!


  • @JonB yes those codes i forgot to earase it before posting this sorry;;
    so making new movie over and over again is slowing the program down??
    and do you know how to change gif without this code??

    self.movie = QMovie(self.gifFile,QByteArray(), self)


  • @jonus said in why my pyqt5 gif animation is getting slower and slower…?:

    so making new movie over and over again is slowing the program down??

    Like I said, I don't know, but maybe it is, if something keeps the original QMovie around and it's still playing. Which is why I suggested trying sticking with one instance, and see? :)

    and do you know how to change gif without this code??

    https://doc.qt.io/qt-5/qmovie.html#setFileName ?

    Also, by keep creating new QMovie instances you will make it harder to connect signals/slots as you'll have to redo those each time you create a new instance. Sticking to one instance and just changing the file it is playing won't have that issue.



  • Thank you i finally figured it out

    self.movie = QMovie(self.gifFile,QByteArray(), self)
    self.movie_screen.setMovie(self.movie)
    self.movie.start()
    

    ->
    self.movie.stop()
    self.movie.setFileName(self.gifFile)
    self.movie.start()
    and works really well!! thank you


Log in to reply