PyQt5 – 在标签的不同边缘添加不同大小的填充物
在这篇文章中,我们将看到如何在不同的边缘添加不同的padding大小。下面是一个有正常填充的标签和一个在不同边缘有不同填充大小的标签的图片。
为了做到这一点,我们将使用setStyleSheet()方法并描述每个边缘的填充长度。
语法:
label.setStyleSheet("border :3px solid black;"
"padding-top : 20px;"
"padding-left:30px;"
"padding-right:10;"
"padding-bottom :5px;")
Python
参数: 它接受字符串作为参数。
执行的动作: 它将在各自的边上添加各自长度的填充物。
代码。
# 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())
Python
输出 :