PyQt5 QLabel - Setting blur radius to the blur effect

PyQt5 QLabel - Setting blur radius to the blur effect

In PyQt5, to apply a blur effect with a specific blur radius to a QLabel, you can use the QGraphicsBlurEffect class. This class provides the functionality to add a blur effect to widgets. Here's a step-by-step guide on how to apply a blur effect with a specified blur radius to a QLabel:

Step 1: Import Necessary Modules

First, you need to import the required modules from PyQt5.

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QGraphicsBlurEffect from PyQt5.QtGui import QPixmap import sys 

Step 2: Create the Main Window and QLabel

Set up the main window and add a QLabel to it. You can set an image or text to the label depending on your requirement.

class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): # Create a QLabel self.label = QLabel(self) self.label.setPixmap(QPixmap("path_to_your_image.jpg")) # Set an image # self.label.setText("Your Text Here") # Or set text # Layout layout = QVBoxLayout(self) layout.addWidget(self.label) # Window settings self.setGeometry(300, 300, 350, 250) self.setWindowTitle('QLabel Blur Effect') self.show() 

Step 3: Apply the Blur Effect

Create a QGraphicsBlurEffect and set its blur radius, then apply it to the QLabel.

 # Create a blur effect blur_effect = QGraphicsBlurEffect() blur_effect.setBlurRadius(10) # Set the desired blur radius # Apply the blur effect to the label self.label.setGraphicsEffect(blur_effect) 

Add this part inside the initUI method of the Example class.

Step 4: Run the Application

Finally, create an instance of your application and run it.

if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) 

Notes:

  • Replace "path_to_your_image.jpg" with the path to your image file if you're using an image for the label.
  • The setBlurRadius method sets the blur radius for the effect. You can adjust this value to increase or decrease the amount of blur.
  • This example demonstrates how to apply a blur effect to a QLabel, but you can apply similar effects to other widgets as well.
  • The blur effect is more noticeable on images than on text. If you're applying it to text, you might need a higher blur radius to see a significant effect.

More Tags

laravel-datatables compose-db bootstrap-vue screensaver textwatcher rxjs-pipeable-operators aws-secrets-manager xml.etree clock azure-aks

More Programming Guides

Other Guides

More Programming Examples