Open In App
Related Articles

PyQt5 – How to add border on QLabel ?

Improve Article
Save Article
Like Article

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):
        # set the title
        # 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
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

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