PyQt5 – How to add padding to a Label ?

In this article, we will see how to add padding to our Label. Padding is just the space between the border and the content. Below is image of label this will helps in better understanding of the padding.

In order to add padding to our label, we will use setStyleSheet() method, below is how without padding label vs padded label looks like.

Syntax : label.setStyleSheet(“padding :15px”)

Argument : It takes string as argument.



Action performed : It adds padding to the label.

Code :

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the required libraries
  
from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
  
        # set the title
        self.setWindowTitle("Python")
  
        # setting  the geometry of window
        self.setGeometry(60, 60, 600, 400)
  
  
        # creating a label widget
        self.label_1 = QLabel("===============", self)
  
        # moving position
        self.label_1.move(100, 100)
  
        # setting up the border
        self.label_1.setStyleSheet("border :3px solid blue;")
  
        # resizing label
        self.label_1.resize(100, 100)
        # creating a label widget
        self.label_2 = QLabel("==================", self)
  
        # setting up the border and adding padding
        self.label_2.setStyleSheet("border :3px solid blue;padding :15px")
  
        # moving position
        self.label_2.move(230, 200)
  
        # resizing the label
        self.label_2.resize(100, 100)
  
        # 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())

chevron_right


Output :




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.