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 :
# 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 ())
|
Output :