blunty6363
blunty6363

Reputation: 29

How to change the opacity of a PyQt5 window

I have just been experimenting with some animation in PyQt5 and am looking to animate the opacity of a window. I have had success with animating the opacity of buttons and QWidgets withink the window, however when I try to apply the same concept to the main QWidget class it doesn't seem to work. Below is my code for trying to get the animation to work on the window (I am aware this isn't the best looking code but I'm just trying to experiment, but also feel free to tell me about any big errors unrelated to the problem as well as I am also quite new to PyQt5):

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

class Test(QWidget):
    def __init__(self):
        super().__init__()

        self.setStyleSheet("background: green")
        self.setGeometry(100, 100, 400, 400)
        
        self.b = QPushButton("Reduce", self, clicked=self.reduce)
        self.b.show()
        self.b.setStyleSheet("color: yellow")
        
        self.show()

    def reduce(self):
        self.eff = QGraphicsOpacityEffect(self)
        self.eff.setOpacity(1)
        self.setGraphicsEffect(self.eff)

        self.anim = QPropertyAnimation(self.eff, b"opacity")
        self.anim.setDuration(500)
        self.anim.setStartValue(1)
        self.anim.setEndValue(0)
        self.anim.start()
        self.t = QTimer()
        self.t.timeout.connect(self.loop)
        self.t.start(10)
        print(self.eff.opacity())

    def loop(self):
        self.update()
        print(self.eff.opacity())
        if self.anim.currentValue() == 0:
            self.t.stop()
            self.update()

if __name__ == "__main__":
    window = QApplication(sys.argv)
    app = Test()
    window.exec_()

I thought maybe I am getting odd results as it isn't possible to do this when the widget is made to be the main widget in the window, however I also don't see why that would be a thing. What seems to happen for me is that the window does nothing and nothing changes, other than the button seems to half freeze, as in I am able to click it and get a result (run the function it is connected too) however no pressing down animation occurs as normally would. However, when I resize the window it will show a change, however this has to be manual as I added a basic loop to constantly resize and it did nothing. And finally when I resize the window, or change the opacity just by changing the opacity of self.eff it changes, but it just makes it all black even at values of 0.8, etc which is what leads me to believe it just isn't possible and some script is defaulting it to black, as the button will work fine while the rest goes black. Any help is appreciated.

Upvotes: 1

Views: 2174

Answers (1)

S. Nick
S. Nick

Reputation: 13651

Try like this:

import sys
from PyQt5.Qt import *


class Test(QWidget):
    def __init__(self):
        super().__init__()
        self.setStyleSheet("background: green;")
        self.resize(400, 400)
        
        self.label = QLabel()
        self.pixmap = QPixmap('Ok.png')
        self.label.setPixmap(self.pixmap)
        self.label.setAlignment(Qt.AlignCenter)
        self.label.setStyleSheet("background: #CD113B;")
        
        self.b = QPushButton("Reduce", self, clicked=self.reduce)
        self.b.setStyleSheet("background: blue; color: yellow;")
        
        layout = QVBoxLayout(self)
        layout.addWidget(self.label)
        layout.addWidget(self.b)

    def reduce(self):
        self.anim = QPropertyAnimation(self, b"opacity")
        self.anim.setDuration(3000)        
        self.anim.setLoopCount(3)
        self.anim.setStartValue(0.0)
        self.anim.setEndValue(1.0)        
        self.anim.start()
            
    def windowOpacity(self):
        return super().windowOpacity()    
    
    def setWindowOpacity(self, opacity):
        super().setWindowOpacity(opacity)    
    
    opacity = pyqtProperty(float, windowOpacity, setWindowOpacity)    
    

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Test()
    w.show()
    sys.exit(app.exec_())

enter image description here



or so:

import sys
from PyQt5.Qt import *


class Test(QWidget):
    def __init__(self):
        super().__init__()
        self.setStyleSheet("background: green;")
        self.resize(400, 400)
        
        self.label = QLabel()
        self.pixmap = QPixmap('Ok.png')
        self.label.setPixmap(self.pixmap)
        self.label.setAlignment(Qt.AlignCenter)
        self.label.setStyleSheet("background: #CD113B;")
        
        self.b = QPushButton("Reduce", self, clicked=self.reduce)
        self.b.setStyleSheet("background: blue; color: yellow;")
        
        layout = QVBoxLayout(self)
        layout.addWidget(self.label)
        layout.addWidget(self.b)

    def reduce(self):
        self.eff = QGraphicsOpacityEffect()
        self.eff.setOpacity(1.0)        
        self.label.setGraphicsEffect(self.eff)
        
        self.anim = QPropertyAnimation(self.eff, b"opacity")
        self.anim.setDuration(3000)        
        self.anim.setLoopCount(3)
        self.anim.setStartValue(0.0)
        self.anim.setEndValue(1.0)        
        self.anim.start()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Test()
    w.show()
    sys.exit(app.exec_())

enter image description here


Ok.png

enter image description here

Upvotes: 1

Related Questions