Open In App
Related Articles

PyQt5 – How to add border on QLabel ?

Improve Article
Improve
Save Article
Save
Like Article
Like

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 :
pyqt-add-border-qlabel


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
Previous
Next
Similar Reads
Complete Tutorials