from PyQt5.QtWidgets import QApplication, QPushButton, QGraphicsOpacityEffect app = QApplication([]) button = QPushButton("Click me!") opacity_effect = QGraphicsOpacityEffect() opacity_effect.setOpacity(0.5) button.setGraphicsEffect(opacity_effect) button.show() app.exec_()
from PyQt5.QtWidgets import QApplication, QGraphicsOpacityEffect, QLabel from PyQt5.QtCore import Qt app = QApplication([]) label = QLabel("Hello, world!") opacity_effect = QGraphicsOpacityEffect() label.setGraphicsEffect(opacity_effect) label.show() for opacity in [0.2, 0.4, 0.6, 0.8]: opacity_effect.setOpacity(opacity) app.processEvents() label.setAlignment(Qt.AlignCenter) label.show() app.exec_()This code will create a QLabel widget with an initial opacity effect. When the script is run, the opacity value of the effect is changed four times, causing the label to gradually become more opaque. The label is also repositioned and re-shown with each change in opacity. This code demonstrates how to change the opacity of a QGraphicsOpacityEffect dynamically. Library: PyQt5 is the package library used for creating the opacity effect in these examples.