Change of line label during animation



  • as in QPropertyAnimation You can change the label line at a certain point.

    import sys
    

    from PyQt5.QtCore import QPropertyAnimation
    from PyQt5.QtWidgets import QWidget, QApplication, QLabel
    from PyQt5.Qt import *

    class Example(QWidget):
    def init(self):
    super().init()
    self.initUI()

    def initUI(self):
        self.setGeometry(700, 300, 500, 500)
        self.setWindowTitle('Тест')
    
        self.label = QLabel(self)
        self.label.setText("Надпись")
        self.label.move(200, 200)
    
        self.animation()
    
    def animation(self):
        self.animation_group = QSequentialAnimationGroup()
    
        # центр --> низ
        self.anim = QPropertyAnimation(self.label, b"pos")
        self.anim.setStartValue(QPoint(200, 200))
        self.anim.setEndValue(QPoint(200, 350))
        self.anim.setDuration(1000)
        self.animation_group.addAnimation(self.anim)
    
        # верх --> центр
        self.anim = QPropertyAnimation(self.label, b"pos")
        self.anim.setStartValue(QPoint(200, 50))
        self.anim.setEndValue(QPoint(200, 200))
        self.anim.setDuration(1000)
        self.animation_group.addAnimation(self.anim)
    
        self.animation_group.setLoopCount(3)
    
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Space:
            self.animation_group.start()
    

    if name == 'main':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec())

    Let me get the label, every time I get upstairs, I've changed my text to another one. How can you do that?



  • You need to track the change of current animation (with QPropertyAnimation issuing the currentAnimationChanged signal)

    import sys
    

    from PyQt5.QtCore import QPropertyAnimation
    from PyQt5.QtWidgets import QWidget, QApplication, QLabel
    from PyQt5.Qt import *

    class Example(QWidget):
    def init(self):
    super().init()
    self.initUI()

    def initUI(self):
        self.setGeometry(700, 300, 500, 500)
        self.setWindowTitle('Тест')
    
        self.label = QLabel(self)
        self.label.setText("Надпись")
        self.label.move(200, 200)
        self.animation()
    
    def animation(self):
        self.colors = ['#0000FF', '#00FF00', '#FF0000', '#FF0000']
        self.animation_group = QSequentialAnimationGroup()
    
        # центр --> низ
        self.anim = QPropertyAnimation(self.label, b"pos")
        self.anim.setStartValue(QPoint(200, 200))
        self.anim.setEndValue(QPoint(200, 350))
        self.anim.setDuration(1000)
        self.animation_group.addAnimation(self.anim)
    
        # верх --> центр
        self.anim2 = QPropertyAnimation(self.label, b"pos")
        self.anim2.setStartValue(QPoint(200, 50))
        self.anim2.setEndValue(QPoint(200, 200))
        self.anim2.setDuration(1000)
        self.animation_group.addAnimation(self.anim2)
    
        self.animation_group.setLoopCount(3)
        self.animation_group.currentAnimationChanged.connect(self.changeColor)
    
    
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Space:
            self.animation_group.start()
    def changeColor(self,anim):
        if anim == self.anim2:
            c = self.colors.pop(0)
            self.colors.append(c)
            self.label.setStyleSheet(f'color: {c}')
    

    if name == 'main':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec())



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2