PyQt5 – How to change color of the label ?
While creating a Label in PyQt5, we can see that there is no background color. In this article we will see how to add background color to the Label.
In order to add border to the Label we will use label.setStyleSheet()
method, this will add the background color to the label, it is same like designing the CSS style sheet.
Syntax : label.setStyleSheet(“background-color: cyan”)
Argument : It takes string as argument.
Action performed: It changes the background color of the label.
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( 'Light green' , self )
self .label_1.move( 100 , 100 )
self .label_1.setStyleSheet( "background-color: lightgreen" )
self .label_2 = QLabel( 'Yellow' , self )
self .label_2.move( 100 , 150 )
self .label_2.setStyleSheet("background - color: yellow;
border: 1px solid black;")
self .show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output :
Last Updated :
26 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...