How to Use Stylesheets in Python PyQt6

In this Python PyQt6 lesson we want to learn How to Use Stylesheets in Python PyQt6, so Python PyQt6 is powerful graphical user interface (GUI) toolkit that allows developers to build interactive and nice desktop applications. PyQt6 offers different builtin widgets that can be used to create standard GUI components, such as buttons, labels and text fields. however sometimes you need to create custom widgets to meet specific design requirements or to add new functionality to your application. in this article we are going to talk about the process of building custom widgets using PyQt6, so first of all we need to install this library.

 

You can use pip for installation

 

 

What are Stylesheets?

Stylesheets are a way of defining the visual appearance of widgets in PyQt6. they are similar to CSS (Cascading Style Sheets) used in web development. with stylesheets you can define color, font, background, padding and other visual properties of a widget.

 

 

 

For using stylesheets in Python PyQt6, you need to set stylesheet property of a widget to valid CSS style string. for example suppose you want to change the background color of QPushButton to red. you can do this by setting the stylesheet property of the button as follows:

In the above code we have created new QPushButton called Click me. after that we set stylesheet property of the button to background-color: red. this changes the background color of the button to red.

 

 

 

Cascading Stylesheets

Similar to CSS stylesheets in PyQt6 support cascading. this means that you can apply styles to multiple widgets by using the same stylesheet. for example suppose you want to change the font size of all the labels in your application to 16 pixels. you can do this by setting the stylesheet property of the QApplication instance as follows:

In the above code we have created new QApplication instance and set its stylesheet property to QLabel { font-size: 16px }. this applies the style to all the QLabel widgets in the application.

 

 

 

Use Externally Stylesheets in Python PyQt6

PyQt6 also allows you to use external stylesheets. this is useful when you have large application with complex user interface and you want to separate the styling from the application code. to use an external stylesheet you can create separate CSS file and load it using the setStyleSheet() method. for example:

 

 

 

This is the complete for external stylesheet

In the above code we have imported necessary PyQt6 classes: QApplication, QLabel, QPushButton, QVBoxLayout and QWidget. after that we have created new QApplication instance and set its stylesheet to the contents of CSS file called style.css.

After that we have created new QWidget instance called widget and set its layout to QVBoxLayout instance called layout, also we have QLabel and QPushButton and we want to change the stylesheet of that.

and finally we show widget using widget.show() method and start the application event loop using the app.exec() method. 

 

Also you need to create style.css in your working directory

 

 

 

 

Run the code and this is the result

How to Use Stylesheets in Python PyQt6
How to Use Stylesheets in Python PyQt6

 

 

 

 

Now let’s use the style of the widgets directly

 

 

 

 

Run your code and this is the output

How to Use Stylesheets in Python PyQt6
How to Use Stylesheets in Python PyQt6

 

 

Learn More on Python GUI

 

 

 

Leave a Comment