英文:
How to run multiple two separate QGraphicsOpacityEffect on two separate QLabels simultaneously
问题
假设您有两个QLabel和一个QWindow,label1
和label2
,您想对它们同时运行QGraphicsOpacityEffect
。这两个效果必须同时开始,同时播放,但总持续时间可以不同。
我进行了很多谷歌搜索。找不到任何解释,只找到一个关于在同一个部件上执行两个单独动画的解释 - 这不是我要做的。
任何帮助将不胜感激 xoxo
我尝试使用了QThread、QThreadPool和QParallelAnimationGroup,但似乎都不能满足我的需求,也许我只是经验不足。
英文:
Say you have two QLabels a QWindow, label1
and label2
, and you want to run a QGraphicsOpacityEffect
on both of them. The two effects must start at the same time, play simultaneously, but last differing lengths of time in total.
I have done so many Google searches. Cannot find any explanation, only one on doing two separate animations on the SAME widget - NOT what I am trying to do.
Any help would be greatly appreciated xoxo
I tried using QThread, QThreadPool, and QParallelAnimationGroup, but none of these seemed to be able to suffice, but maybe I'm just inexperienced.
答案1
得分: 0
I'm a muppet. Certified Doo-Doo head. Anyway I figured it out.
Basically, instead of redefining a single self.animation
value each time I launch a new animation (which stops the old one, silly goose), I have an array of animations that I had new ones to. Here's the MRE I made to do it:
from PyQt5.QtCore import QPropertyAnimation, QAbstractAnimation
import sys
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setWindowTitle('Window')
self.animations = []
self.label1 = QLabel('Label 1!', self)
self.label1.move(10, 10)
self.label2 = QLabel('Label 2!', self)
self.label2 move(10, 110)
self.showMaximized()
self.fadeOutSequence()
def animateWidget(self, widget, duration):
effect = QGraphicsOpacityEffect()
self.animations.append(QPropertyAnimation(effect, b'opacity'))
widget.setGraphicsEffect(effect)
self.animations[len(self.animations)-1].setDuration(duration)
self.animations[len(self.animations)-1].setStartValue(0)
self.animations[len(self.animations)-1].setEndValue(1)
self.animations[len(self.animations)-1].start(QAbstractAnimation.DeleteWhenStopped)
def fadeOutSequence(self):
self.animateWidget(self.label1, 4000)
self.animateWidget(self.label2, 8000)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Window()
sys.exit(app.exec_())
英文:
I'm a muppet. Certified Doo-Doo head. Anyway I figured it out.
Basically, instead of redefining a single self.animation
value each time I launch a new animation (which stops the old one, silly goose), I have an array of animations that I had new ones to. Here's the MRE I made to do it:
from PyQt5.QtCore import QPropertyAnimation, QAbstractAnimation
import sys
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setWindowTitle('Window')
self.animations = []
self.label1 = QLabel('Label 1!', self)
self.label1.move(10, 10)
self.label2 = QLabel('Label 2!', self)
self.label2.move(10, 110)
self.showMaximized()
self.fadeOutSequence()
def animateWidget(self, widget, duration):
effect = QGraphicsOpacityEffect()
self.animations.append(QPropertyAnimation(effect, b'opacity'))
widget.setGraphicsEffect(effect)
self.animations[len(self.animations)-1].setDuration(duration)
self.animations[len(self.animations)-1].setStartValue(0)
self.animations[len(self.animations)-1].setEndValue(1)
self.animations[len(self.animations)-1].start(QAbstractAnimation.DeleteWhenStopped)
def fadeOutSequence(self):
self.animateWidget(self.label1, 4000)
self.animateWidget(self.label2, 8000)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Window()
sys.exit(app.exec_())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论