Tuesday, October 7, 2025
HomeLanguagesPyQt5 – Different padding size at different edge of Label

PyQt5 – Different padding size at different edge of Label

In this article, we will see how we can add different padding size at different edges. Below is a image of label with normal padding and a label with different padding size at different edge.

In order to do this we will use setStyleSheet() method and describe padding length for each edge.

Syntax :

label.setStyleSheet("border :3px solid black;"
                    "padding-top : 20px;"
                    "padding-left:30px;"
                    "padding-right:10;"
                    "padding-bottom :5px;")

Argument : It takes string as argument.

Action performed : It will add padding to respective edges with respective lengths.

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("padding", self)
  
        # moving position
        self.label_1.move(100, 100)
  
        # setting up the border and padding
        self.label_1.setStyleSheet("border :3px solid black; 
                                              padding :15px")
  
        # resizing label
        self.label_1.adjustSize()
  
        # creating a label widget
        self.label_2 = QLabel("padding", self)
  
        # setting up the border 
        # and adding padding of different length to different edges
        self.label_2.setStyleSheet("border :3px solid black;"
                                   "padding-top : 20px;"
                                   "padding-left:30px;"
                                   "padding-right:10;"
                                   "padding-bottom :5px;")
  
        # moving position
        self.label_2.move(230, 200)
  
        # resizing the label
        self.label_2.adjustSize()
  
        # 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-padding-size-label

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS