Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. why my pyqt5 gif animation is getting slower and slower…?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved Mobile and Embedded
6 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jonus
    wrote on last edited by
    #1

    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_())

    JonBJ 1 Reply Last reply
    0
    • J jonus

      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_())

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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?

      J 1 Reply Last reply
      3
      • J Offline
        J Offline
        jonus
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • JonBJ JonB

          @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?

          J Offline
          J Offline
          jonus
          wrote on last edited by
          #4

          @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)
          
          JonBJ 1 Reply Last reply
          0
          • J jonus

            @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)
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @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.

            1 Reply Last reply
            3
            • J Offline
              J Offline
              jonus
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved