PyQt5 QLabel change font and set background image

Use the setStyleSheet method to modify the font, size, and color you want

self.lab = QLabel("标签字体大小颜色", self)
self.lab.setGeometry(50,50,300,200)        
self.setStyleSheet("QLabel{color:rgb(225,22,173,255);font-size:50px;font-weight:normal;font-family:Arial;}")

color: four parameters in rgb(), the first three control the color, the fourth controls the transparency
font-size: set the font size
font-weight: bold can set the font to be bold
font-family: choose the color you want
setStyleSheet can also set the label background image, but cannot make the image match the label size
I use QPixmap to set the label background image

self.lab = QLabel('标签背景图片', self)
self.lab.setGeometry(50,50,300,200)
pixmap = QPixmap('F:\A_code\PyQT_Demo\\1.png')
self.lab.setPixmap(pixmap)

write picture description here
But at this time, the background image we added still cannot match
the We need to modify our instantiated QPixmap part

pixmap=QPixmap('F:\A_code\PyQT_Demo\\1.png').scaled
       (self.lab.width(), self.lab.height())

Or modify the properties of the lab:self.lab.setScaledContents(True)
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325568751&siteId=291194637