PyQt stylesheet

You can use css stylesheets with PyQt. The method call setStyleSheet() gives you the freedom to style the different components in your application.

As method argument you can specify plain css code, like so:

lbl_red.setStyleSheet("color: red;"
                             "border-style: solid;"
                             "border-width: 2px;"
                             "border-color: #FA8072;"
                             "border-radius: 3px")

Related course: Create Desktop Apps with Python PyQt5

PyQt css stylesheet

The program below adds 3 labels with different stylesheets applied to them. You can call the method setStyleSheet() on the widgets.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout

class MyApp(QWidget):

def __init__(self):
        super().__init__()
        self.initUI()

def initUI(self):

lbl_red = QLabel('Red')
        lbl_green = QLabel('Green')
        lbl_blue = QLabel('Blue')

        lbl_red.setStyleSheet("color: red;"
                             "border-style: solid;"
                             "border-width: 2px;"
                             "border-color: #FA8072;"
                             "border-radius: 3px")
        lbl_green.setStyleSheet("color: green;"
                               "background-color: #7FFFD4")
        lbl_blue.setStyleSheet("color: blue;"
                              "background-color: #87CEFA;"
                              "border-style: dashed;"
                              "border-width: 3px;"
                              "border-color: #1E90FF")

vbox = QVBoxLayout()
        vbox.addWidget(lbl_red)
        vbox.addWidget(lbl_green)
        vbox.addWidget(lbl_blue)

self.setLayout(vbox)

self.setWindowTitle('Stylesheet')
        self.setGeometry(300, 300, 300, 200)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

Decorate three label widgets in different styles.

pyqt stylesheet

Description

lbl_red = QLabel('Red')
lbl_green = QLabel('Green')
lbl_blue = QLabel('Blue')

Use the QLabel class to create three label widgets. The label text is set to 'Red', 'Green' and 'Blue', respectively.

lbl_red.setStyleSheet("color: red;"
                      "border-style: solid;"
                      "border-width: 2px;"
                      "border-color: #FA8072;"
                      "border-radius: 3px")

Use the setStyleSheet() method to set the letter color to red, the boundary line solid, the boundary thickness to 2px, the boundary color to #FA8072, and the edge of the boundary line rounded by 3px.

lbl_green.setStyleSheet("color: green;"
                        "background-color: #7FFFD4")

Similarly, lbl_green set the letter color of the label to green and the #7FFFD4 color.

lbl_blue.setStyleSheet("color: blue;"
                       "background-color: #87CEFA;"
                       "border-style: dashed;"
                       "border-width: 3px;"
                       "border-color: #1E90FF")

lbl_blue set the letter color of the label to blue, the background #87CEFA, the border to dash style, the boundary line thickness to 3px, and #1E90FF border color.

vbox = QVBoxLayout()
vbox.addWidget(lbl_red)
vbox.addWidget(lbl_green)
vbox.addWidget(lbl_blue)

self.setLayout(vbox)

Use the vertical box layout (QVBoxLayout()) to place the three label widgets vertically.