Изменение строки label во время анимации
как в QPropertyAnimation можно изменить строку label в определённом моменте.
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())
Допустим мне нужно чтобы label, каждый раз когда оказывался наверху, менял свой текст на другой. Как это можно сделать?
Ответы (1 шт):
Автор решения: Sergey Tatarincev
→ Ссылка
Вам нужно отследить момент смены текущей анимации (при этом QPropertyAnimation издает сигнал currentAnimationChanged)
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())