When we create Label in PyQt5, we can see that there are no borders like there exist in Push Buttons, in this article we will see how to add border to the Label.
In order to add border to the Label we will use label.setStyleSheet()
method, this will add the border to the label, also we can set the thickness as well as color of the border.
Syntax : label.setStyleSheet(“border: 1px solid black;”)
Argument : It takes string as a argument.
Action performed : This will create a border on label with thickness of 1px and color will be black.
Below is the Python implementation –
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( 'It is Label 1' , self )
self .label_1.move( 100 , 100 )
self .label_1.setStyleSheet( "border: 1px solid black;" )
self .label_2 = QLabel( 'It is Label 2' , self )
self .label_2.move( 100 , 200 )
self .label_2.setStyleSheet( "border: 3px solid blue;" )
self .show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output :

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Mar, 2020
Like Article
Save Article