PyQt5 how to update QLabel as an animation












0














I want to use my qlabel as a countdown. Basically when countdown is called the label changes " 3 2 1 begin", with 1 second gap in-between .



however, if I do this:



def nextSound(self):

self.mainLabel.setText("3")

sleep(1)

self.mainLabel.setText("2")

sleep(1)
self.mainLabel.setText("1")


it simply just wait until the end without updating the label. So I try to use QPropertyAnimation:



  def nextSound(self):

self.animate = QPropertyAnimation(self.mainLabel,"setText")

self.animate.setDuration(1000)
self.animate.startValue("3")
self.animate.setEndValue("2")
self.animate.start()


But received this error:



self.animate = QPropertyAnimation(self.mainLabel,"setText")
TypeError: arguments did not match any overloaded call:
QPropertyAnimation(parent: QObject = None): too many arguments
QPropertyAnimation(QObject, Union[QByteArray, bytes, bytearray], parent: QObject = None): argument 2 has unexpected type 'str'


Any suggestion? Thanks










share|improve this question





























    0














    I want to use my qlabel as a countdown. Basically when countdown is called the label changes " 3 2 1 begin", with 1 second gap in-between .



    however, if I do this:



    def nextSound(self):

    self.mainLabel.setText("3")

    sleep(1)

    self.mainLabel.setText("2")

    sleep(1)
    self.mainLabel.setText("1")


    it simply just wait until the end without updating the label. So I try to use QPropertyAnimation:



      def nextSound(self):

    self.animate = QPropertyAnimation(self.mainLabel,"setText")

    self.animate.setDuration(1000)
    self.animate.startValue("3")
    self.animate.setEndValue("2")
    self.animate.start()


    But received this error:



    self.animate = QPropertyAnimation(self.mainLabel,"setText")
    TypeError: arguments did not match any overloaded call:
    QPropertyAnimation(parent: QObject = None): too many arguments
    QPropertyAnimation(QObject, Union[QByteArray, bytes, bytearray], parent: QObject = None): argument 2 has unexpected type 'str'


    Any suggestion? Thanks










    share|improve this question



























      0












      0








      0


      1





      I want to use my qlabel as a countdown. Basically when countdown is called the label changes " 3 2 1 begin", with 1 second gap in-between .



      however, if I do this:



      def nextSound(self):

      self.mainLabel.setText("3")

      sleep(1)

      self.mainLabel.setText("2")

      sleep(1)
      self.mainLabel.setText("1")


      it simply just wait until the end without updating the label. So I try to use QPropertyAnimation:



        def nextSound(self):

      self.animate = QPropertyAnimation(self.mainLabel,"setText")

      self.animate.setDuration(1000)
      self.animate.startValue("3")
      self.animate.setEndValue("2")
      self.animate.start()


      But received this error:



      self.animate = QPropertyAnimation(self.mainLabel,"setText")
      TypeError: arguments did not match any overloaded call:
      QPropertyAnimation(parent: QObject = None): too many arguments
      QPropertyAnimation(QObject, Union[QByteArray, bytes, bytearray], parent: QObject = None): argument 2 has unexpected type 'str'


      Any suggestion? Thanks










      share|improve this question















      I want to use my qlabel as a countdown. Basically when countdown is called the label changes " 3 2 1 begin", with 1 second gap in-between .



      however, if I do this:



      def nextSound(self):

      self.mainLabel.setText("3")

      sleep(1)

      self.mainLabel.setText("2")

      sleep(1)
      self.mainLabel.setText("1")


      it simply just wait until the end without updating the label. So I try to use QPropertyAnimation:



        def nextSound(self):

      self.animate = QPropertyAnimation(self.mainLabel,"setText")

      self.animate.setDuration(1000)
      self.animate.startValue("3")
      self.animate.setEndValue("2")
      self.animate.start()


      But received this error:



      self.animate = QPropertyAnimation(self.mainLabel,"setText")
      TypeError: arguments did not match any overloaded call:
      QPropertyAnimation(parent: QObject = None): too many arguments
      QPropertyAnimation(QObject, Union[QByteArray, bytes, bytearray], parent: QObject = None): argument 2 has unexpected type 'str'


      Any suggestion? Thanks







      python pyqt pyqt5 qlabel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 8:36









      eyllanesc

      72.7k93055




      72.7k93055










      asked Nov 20 at 21:44









      Jiajun Yang

      5543824




      5543824
























          1 Answer
          1






          active

          oldest

          votes


















          2














          QPropertyAnimation is based on interpolating the values that a q-property takes, when wanting to use setText I think that the closest thing is the q-property text but the texts can not be interpolated so a solution would be to create a q-property that takes numerical value.



          from PyQt5 import QtCore, QtWidgets

          class NumLabel(QtWidgets.QLabel):
          def number(self):
          try:
          return int(self.text())
          except:
          return 0
          def setNumber(self, number):
          self.setNum(number)
          number = QtCore.pyqtProperty(int, fget=number, fset=setNumber)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = NumLabel(alignment=QtCore.Qt.AlignCenter)
          w.resize(640, 480)
          animation = QtCore.QPropertyAnimation(w, b'number')
          animation.setStartValue(3)
          animation.setEndValue(0)
          animation.setDuration(1000*(abs(animation.endValue() - animation.startValue())))
          animation.start()
          w.show()
          sys.exit(app.exec_())


          Another best option is to use QTimeLine:



          from PyQt5 import QtCore, QtWidgets

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
          w.resize(640, 480)
          start = 3
          end = 0
          timeLine = QtCore.QTimeLine(abs(end - start)*1000, w)
          timeLine.setFrameRange(start, end)
          timeLine.frameChanged.connect(w.setNum)

          # set start value
          w.setNum(start)
          # start timer
          timeLine.start()

          w.show()
          sys.exit(app.exec_())





          share|improve this answer





















          • thank you! I used the QTimeLine and set the curve shape to Linear. It works.
            – Jiajun Yang
            Nov 21 at 11:34











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402046%2fpyqt5-how-to-update-qlabel-as-an-animation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          QPropertyAnimation is based on interpolating the values that a q-property takes, when wanting to use setText I think that the closest thing is the q-property text but the texts can not be interpolated so a solution would be to create a q-property that takes numerical value.



          from PyQt5 import QtCore, QtWidgets

          class NumLabel(QtWidgets.QLabel):
          def number(self):
          try:
          return int(self.text())
          except:
          return 0
          def setNumber(self, number):
          self.setNum(number)
          number = QtCore.pyqtProperty(int, fget=number, fset=setNumber)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = NumLabel(alignment=QtCore.Qt.AlignCenter)
          w.resize(640, 480)
          animation = QtCore.QPropertyAnimation(w, b'number')
          animation.setStartValue(3)
          animation.setEndValue(0)
          animation.setDuration(1000*(abs(animation.endValue() - animation.startValue())))
          animation.start()
          w.show()
          sys.exit(app.exec_())


          Another best option is to use QTimeLine:



          from PyQt5 import QtCore, QtWidgets

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
          w.resize(640, 480)
          start = 3
          end = 0
          timeLine = QtCore.QTimeLine(abs(end - start)*1000, w)
          timeLine.setFrameRange(start, end)
          timeLine.frameChanged.connect(w.setNum)

          # set start value
          w.setNum(start)
          # start timer
          timeLine.start()

          w.show()
          sys.exit(app.exec_())





          share|improve this answer





















          • thank you! I used the QTimeLine and set the curve shape to Linear. It works.
            – Jiajun Yang
            Nov 21 at 11:34
















          2














          QPropertyAnimation is based on interpolating the values that a q-property takes, when wanting to use setText I think that the closest thing is the q-property text but the texts can not be interpolated so a solution would be to create a q-property that takes numerical value.



          from PyQt5 import QtCore, QtWidgets

          class NumLabel(QtWidgets.QLabel):
          def number(self):
          try:
          return int(self.text())
          except:
          return 0
          def setNumber(self, number):
          self.setNum(number)
          number = QtCore.pyqtProperty(int, fget=number, fset=setNumber)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = NumLabel(alignment=QtCore.Qt.AlignCenter)
          w.resize(640, 480)
          animation = QtCore.QPropertyAnimation(w, b'number')
          animation.setStartValue(3)
          animation.setEndValue(0)
          animation.setDuration(1000*(abs(animation.endValue() - animation.startValue())))
          animation.start()
          w.show()
          sys.exit(app.exec_())


          Another best option is to use QTimeLine:



          from PyQt5 import QtCore, QtWidgets

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
          w.resize(640, 480)
          start = 3
          end = 0
          timeLine = QtCore.QTimeLine(abs(end - start)*1000, w)
          timeLine.setFrameRange(start, end)
          timeLine.frameChanged.connect(w.setNum)

          # set start value
          w.setNum(start)
          # start timer
          timeLine.start()

          w.show()
          sys.exit(app.exec_())





          share|improve this answer





















          • thank you! I used the QTimeLine and set the curve shape to Linear. It works.
            – Jiajun Yang
            Nov 21 at 11:34














          2












          2








          2






          QPropertyAnimation is based on interpolating the values that a q-property takes, when wanting to use setText I think that the closest thing is the q-property text but the texts can not be interpolated so a solution would be to create a q-property that takes numerical value.



          from PyQt5 import QtCore, QtWidgets

          class NumLabel(QtWidgets.QLabel):
          def number(self):
          try:
          return int(self.text())
          except:
          return 0
          def setNumber(self, number):
          self.setNum(number)
          number = QtCore.pyqtProperty(int, fget=number, fset=setNumber)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = NumLabel(alignment=QtCore.Qt.AlignCenter)
          w.resize(640, 480)
          animation = QtCore.QPropertyAnimation(w, b'number')
          animation.setStartValue(3)
          animation.setEndValue(0)
          animation.setDuration(1000*(abs(animation.endValue() - animation.startValue())))
          animation.start()
          w.show()
          sys.exit(app.exec_())


          Another best option is to use QTimeLine:



          from PyQt5 import QtCore, QtWidgets

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
          w.resize(640, 480)
          start = 3
          end = 0
          timeLine = QtCore.QTimeLine(abs(end - start)*1000, w)
          timeLine.setFrameRange(start, end)
          timeLine.frameChanged.connect(w.setNum)

          # set start value
          w.setNum(start)
          # start timer
          timeLine.start()

          w.show()
          sys.exit(app.exec_())





          share|improve this answer












          QPropertyAnimation is based on interpolating the values that a q-property takes, when wanting to use setText I think that the closest thing is the q-property text but the texts can not be interpolated so a solution would be to create a q-property that takes numerical value.



          from PyQt5 import QtCore, QtWidgets

          class NumLabel(QtWidgets.QLabel):
          def number(self):
          try:
          return int(self.text())
          except:
          return 0
          def setNumber(self, number):
          self.setNum(number)
          number = QtCore.pyqtProperty(int, fget=number, fset=setNumber)

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = NumLabel(alignment=QtCore.Qt.AlignCenter)
          w.resize(640, 480)
          animation = QtCore.QPropertyAnimation(w, b'number')
          animation.setStartValue(3)
          animation.setEndValue(0)
          animation.setDuration(1000*(abs(animation.endValue() - animation.startValue())))
          animation.start()
          w.show()
          sys.exit(app.exec_())


          Another best option is to use QTimeLine:



          from PyQt5 import QtCore, QtWidgets

          if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
          w.resize(640, 480)
          start = 3
          end = 0
          timeLine = QtCore.QTimeLine(abs(end - start)*1000, w)
          timeLine.setFrameRange(start, end)
          timeLine.frameChanged.connect(w.setNum)

          # set start value
          w.setNum(start)
          # start timer
          timeLine.start()

          w.show()
          sys.exit(app.exec_())






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 23:11









          eyllanesc

          72.7k93055




          72.7k93055












          • thank you! I used the QTimeLine and set the curve shape to Linear. It works.
            – Jiajun Yang
            Nov 21 at 11:34


















          • thank you! I used the QTimeLine and set the curve shape to Linear. It works.
            – Jiajun Yang
            Nov 21 at 11:34
















          thank you! I used the QTimeLine and set the curve shape to Linear. It works.
          – Jiajun Yang
          Nov 21 at 11:34




          thank you! I used the QTimeLine and set the curve shape to Linear. It works.
          – Jiajun Yang
          Nov 21 at 11:34


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402046%2fpyqt5-how-to-update-qlabel-as-an-animation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python