Skip to content
Related Articles

Related Articles

PyQt5 – How to make semi-transparent label ?
  • Last Updated : 26 Mar, 2020
GeeksforGeeks - Summer Carnival Banner

While designing a GUI (Graphical User Interface) application we tend to make lot of labels, but sometimes some labels overlaps each other and only label which is on top is visible, that’s why semi transparent label is needed.

Normal label vs Semi-transparent label –
      

In order to create semi-transparent labels setStyleSheet() method is used.

Syntax : label.setStyleSheet(“background-color: rgba(255, 255, 255, 10);”)
Here we are setting color using RGBA i.e transparency factor, 255 is completely opaque, and an alpha of 0 is completely transparent.

Argument: It takes string as argument.



Action performed : It makes the color of the label transparent.

Code :




# importing the required libraries
  
from PyQt5.QtWidgets import * 
from PyQt5.QtGui import * 
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        # set the title
        self.setWindowTitle("Label")
  
        # setting  the geometry of window
        self.setGeometry(0, 0, 400, 300)
  
        # creating a label widget
        # by default label will display at top left corner
        self.label_1 = QLabel('back', self)
  
        # moving position
        self.label_1.move(100, 100)
  
        # setting up border and background color
        self.label_1.setStyleSheet("background-color: lightgreen;
                                    border: 3px solid green")
  
        # creating a label widget
        # by default label will display at top left corner
        self.label_2 = QLabel('front', self)
  
  
        # moving position
        self.label_2.move(140, 100)
  
        # setting up border and background
        # color with transparency factor 
        self.label_2.setStyleSheet("border: 3px solid blue; 
                  background-color: rgba(0, 255, 255, 90);")
          
        # show all the widgets
        self.show()
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())

Output :
pyqt-semi-transparent-label

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :