PyQt5 – 如何设置Label的皮肤
与设置背景图像不同,皮肤会根据标签的大小进行自我调整。在背景图片中,如果图片的尺寸很大,而标签的尺寸很小,那么只有图片的那一部分可以显示,而这一部分包括标签的尺寸。以下是背景图片和标签皮肤的区别。
对于这个图像:
标签的背景图像和标签的皮肤将看起来像这样:
为了做到这一点,我们将使用setStyleSheet()方法。
语法: label.setStyleSheet(“border-image : url(image.png);”)
参数: 它使用字符串作为参数。
执行的操作: 它将图像设置为标签的皮肤。
代码。
# 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("background", self) # moving position self.label_1.move(100, 100) # setting up the border self.label_1.setStyleSheet("border :5px solid blue;") # setting background image self.label_1.setStyleSheet("background-image :url(image.png);") # resizing label self.label_1.resize(80, 100) # creating a label widget self.label_2 = QLabel("== Skin ==", self) # moving position self.label_2.move(200, 200) # setting skin for label self.label_2.setStyleSheet("border-image : url(image.png);") # resizing the label self.label_2.resize(80, 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())
输出 :
版权声明:本页面内容旨在传播知识,为用户自行发布,若有侵权等问题请及时与本网联系,我们将第一时间处理。E-mail:284563525@qq.com