PyQt5 – How to make semi-transparent label ?
Last Updated :
26 Mar, 2020
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 :
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys
class Window(QMainWindow):
def __init__( self ):
super ().__init__()
self .setWindowTitle( "Label" )
self .setGeometry( 0 , 0 , 400 , 300 )
self .label_1 = QLabel( 'back' , self )
self .label_1.move( 100 , 100 )
self .label_1.setStyleSheet("background - color: lightgreen;
border: 3px solid green")
self .label_2 = QLabel( 'front' , self )
self .label_2.move( 140 , 100 )
self .label_2.setStyleSheet("border: 3px solid blue;
background - color: rgba( 0 , 255 , 255 , 90 );")
self .show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output :
Share your thoughts in the comments
Please Login to comment...