How to create an animation for a Graphicseffect property in Pyqt5?

Asked

Viewed 61 times

-1

I created a Label to make a slideshow and want to create a transition effect fading between one image and another.

To do this effect, I want to define an opacity of 1.0 à 0.0 in my Label. So far, I am using the code below to change the opacity:

opacity_effect = QtWidgets.QGraphicsOpacityEffect()
opacity_effect.setOpacity(0.3)
label.setGraphicsEffect(opacity_effect)

Searching the internet about animations in Pyqt5, I discovered only the class QtCore.QPropertyAnimation, but that does not seem to suit this case. My question is: how can I create an animation for the properties of GraphicsEffect?

1 answer

1


Qpropertyanimation can be used in this case yes, see the example:

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class Teste(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(100,100,700,500)
        self.vbox = QVBoxLayout()
        self.btn = QPushButton('start',self)
        self.btn.clicked.connect(self._efeito)
        self.vbox.addWidget(self.btn)
        self.label = QLabel()
        self.efeito = QGraphicsOpacityEffect(self.label)
        self.efeito.setOpacity(1)
        self.label.setGraphicsEffect(self.efeito)
        self.label.setStyleSheet("background:red")
        self.label.setMaximumWidth(100)
        self.vbox.addWidget(self.label)
        self.setLayout(self.vbox)
        self.efeito = QPropertyAnimation(self.efeito,b'opacity')
    
    def _efeito(self):
    
        self.efeito.setDuration(1000)
        self.efeito.setStartValue(1)
        self.efeito.setEndValue(0)
    
        self.efeito.start()


app = QApplication([])
w = Teste()
w.show()
sys.exit(app.exec_())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.