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 –
# 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( 'It is Label 1' , self ) # moving position self .label_1.move( 100 , 100 ) # setting up border self .label_1.setStyleSheet( "border: 1px solid black;" ) # creating a label widget # by default label will display at top left corner self .label_2 = QLabel( 'It is Label 2' , self ) # moving position self .label_2.move( 100 , 200 ) # setting up border self .label_2.setStyleSheet( "border: 3px solid blue;" ) # 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 :