PyQt5如何将QLabel更新为动画

时间:2018-11-20 21:44:32

标签: python pyqt pyqt5 qlabel

我想将我的qlabel用作倒计时。基本上,当倒数计时被调用时,标签将变为“ 3 2 1开始”,其间有1秒的间隔。

但是,如果我这样做:

from turtle import Screen, Turtle

print("Please select your conversion:")

invalid_input = True
def Converter() :
  conversion = input ("metric to metric type mm, metric to imperial type mi, units of water type w, for physics equations type p, for math equations type m and for quick facts type q:")
  print("Please select your conversion:")

  if conversion == "mm":
    #selection = "metric to metric conversions!"

  elif conversion == "mi":
    selection = "metric to imperial conversions!"
    invalid_input = False
  elif conversion == "w":
    selection = "water conversions!"
    invalid_input = False
  elif conversion == "p":
    selection = "physics equations!"
    invalid_input = False
  elif conversion == "m":
    selection = "maths equations!"
    invalid_input = False
  elif conversion == "q":
    selection = "quick facts!"
    invalid_input = False
  else:
    print("\n")
    print("Invalid! Please try again.\n \n \n")
while invalid_input : 


print("\n")
print("You have selected", selection + "!")

invalid_input = True
def start() :
  decision = input ("Is this correct? If correct type y, if incorrect type n.")
  if decision == "y":
        #stuff
        invalid_input = False
while invalid_input : # this will loop until invalid_input is set to be True
    start()

它只是等到最后而无需更新标签。因此,我尝试使用def nextSound(self): self.mainLabel.setText("3") sleep(1) self.mainLabel.setText("2") sleep(1) self.mainLabel.setText("1")

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

有什么建议吗?谢谢

1 个答案:

答案 0 :(得分:3)

QPropertyAnimation是基于对q属性所取值的插值,当想使用setText时,我认为最接近的是q属性文本,但无法对这些文本进行插值,因此可以采用一种解决方案创建一个带有数值的q属性。

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

另一个最佳选择是使用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_())