In this PyQt5 article we want to learn How to Add Graphic Effects in PyQt5 Label, so PyQt5 is a powerful Python GUI library that allows developers to create graphical user interfaces (GUIs) in a simple and easy way, and Labels are commonly used to display text or images in PyQt5 applications. when creating PyQt5 Label, maybe you will need to add effects in your application, you can do that easily in PyQt5.
First of all we need to install PyQt6 and you can use pip for that.
1 |
pip install PyQt5 |
To get started, we need to create a custom PyQt5 Label class that inherits from the QLabel widget. This class will provide the foundation for adding graphic effects to our labels.
1 2 3 4 5 6 |
from PyQt5.QtWidgets import QLabel, QGraphicsDropShadowEffect class GraphicLabel(QLabel): def __init__(self, text): super().__init__(text) self.setGraphicsEffect(QGraphicsDropShadowEffect()) |
In the above code snippet, we imported the necessary modules and created a GraphicLabel class that inherits from QLabel. after that we override __init__ method to set a default graphic effect using QGraphicsDropShadowEffect.
Let’s extend our GraphicLabel class to add methods for different graphic effects. For this example, we are going to focus on two commonly used effects, drop shadows and blurs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from PyQt5.QtWidgets import QLabel, QGraphicsDropShadowEffect, QGraphicsBlurEffect class GraphicLabel(QLabel): def __init__(self, text): super().__init__(text) self.setGraphicsEffect(QGraphicsDropShadowEffect()) def setDropShadowEffect(self): effect = QGraphicsDropShadowEffect() effect.setOffset(5, 5) self.setGraphicsEffect(effect) def setBlurEffect(self): effect = QGraphicsBlurEffect() effect.setBlurRadius(10) self.setGraphicsEffect(effect) |
In the above code we have added two new methods, setDropShadowEffect() and setBlurEffect(). These methods creates instances of QGraphicsDropShadowEffect and QGraphicsBlurEffect. also we can customize the effects by setting properties such as offsets and blur radius.
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QLabel, QGraphicsDropShadowEffect, QGraphicsBlurEffect, QWidget class GraphicLabel(QLabel): def __init__(self, text): super().__init__(text) self.setGraphicsEffect(QGraphicsDropShadowEffect()) # Default effect def setDropShadowEffect(self): effect = QGraphicsDropShadowEffect() effect.setOffset(5, 5) # Set shadow offset self.setGraphicsEffect(effect) def setBlurEffect(self): effect = QGraphicsBlurEffect() effect.setBlurRadius(10) # Set blur radius self.setGraphicsEffect(effect) if __name__ == '__main__': app = QApplication([]) # Create a main window widget window = QWidget() layout = QVBoxLayout() window.setLayout(layout) # Create graphic labels with different effects label1 = GraphicLabel("Codeloop.org - Drop Shadow Effect") label1.setDropShadowEffect() label2 = GraphicLabel("Codeloop.org - Blur Effect") label2.setBlurEffect() # Add labels to the layout layout.addWidget(label1) layout.addWidget(label2) window.show() app.exec() |
Run the complete code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email