PyQt5 Label - Setting offset to the shadow

PyQt5 Label - Setting offset to the shadow

In PyQt5, if you want to apply a shadow effect to a QLabel and set an offset to the shadow, you would typically use the QGraphicsDropShadowEffect class.

Here's a step-by-step guide on how to achieve this:

  • First, ensure you've installed PyQt5:
pip install PyQt5 
  • Create a sample application to demonstrate the shadow effect:
import sys from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QGraphicsDropShadowEffect from PyQt5.QtCore import Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() # Create a label self.label = QLabel("Hello, PyQt5!", self) self.label.setAlignment(Qt.AlignCenter) self.label.setGeometry(50, 50, 200, 50) # Apply shadow effect to the label shadow_effect = QGraphicsDropShadowEffect(self.label) shadow_effect.setOffset(5, 5) # Set the offset of the shadow shadow_effect.setBlurRadius(20) self.label.setGraphicsEffect(shadow_effect) # Set window size and display it self.setGeometry(100, 100, 400, 300) self.setWindowTitle('PyQt5 QLabel Shadow') self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec_()) 

In this example, we've created a QMainWindow with a QLabel. A QGraphicsDropShadowEffect is applied to the QLabel, and we set an offset to the shadow using the setOffset() method. Adjust the values in setOffset(x, y) to change the shadow's position. Similarly, you can adjust the setBlurRadius() to increase or decrease the shadow's blur.


More Tags

tomcat7 spring-rabbit foreign-keys fongo aspbutton pdf-extraction large-title sql-server-2014 redirectstandardoutput lapply

More Programming Guides

Other Guides

More Programming Examples