Open In App

PyQt5 – How to set skin of Label ?

Last Updated : 26 Mar, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Unlike setting background images, skins adjust themselves according to the size of Label. In background image, if the image size is large and label size is small only that part of image is displayable that comprises the size of label. Below is the difference between background image and skin of label.

For this image :

Background image of label and skin of label will look like this :

In order to do this we will use setStyleSheet() method.

Syntax : label.setStyleSheet(“border-image : url(image.png);”)

Argument : It takes string as argument.

Action performed : It sets the image as skin of 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("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())


Output :
pyqt-label-image-skin



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads