PyQt5 – 为标签的每一面设置不同的边框大小
在这篇文章中,我们将看到如何为标签设置不同的边框尺寸,在默认情况下,标签没有设置边框,尽管我们可以通过使用样式表设置边框来为标签添加边框,但是边框是统一的,四边都有相同的尺寸。
下面是普通边框标签与不同边框尺寸的标签的对比。
语法:
label.setStyleSheet("border : solid black;"
"border-width : 10px 1px 10px 1px;")
解释: border-width用于设置厚度,第一个元素指上边的厚度,第二个元素指右边,第三个元素指下边,第四个元素指左边。
下面是实现方法。
# 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 geometry
self.setGeometry(100, 100, 600, 400)
# creating a label widget
self.label_1 = QLabel("Label ", self)
# setting up the border with different size
self.label_1.setStyleSheet("border : solid black;"
"border-width : 10px 1px 10px 1px;")
# resizing the label
self.label_1.resize(100, 40)
# moving the label
self.label_1.move(100, 100)
# show all the widgets
self.update()
self.show()
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :