Open In App
Related Articles

PyQt5 – How to set skin of Label ?

Improve Article
Save Article
Like Article

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):
        # set the title
        # 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
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 26 Mar, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials