BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
If you need to build a GUI system in Python, PyQt5 is probably the best choice, but don't take our word for it - read this blog and see what you think.
- Should you be using PyQt?
- QT Designer
- Widgets
- Layouts
- Forms and code
- Slots and Signals
- CSS and Formatting (this blog)
- Models and Data
Posted by Andy Brown on 21 June 2022
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
CSS and Formatting
By far the easiest way to attach formatting to the widgets of a form is using a cascading style sheet. Here we have a simple form with absolutely no formatting:

The form looks drab, but CSS will sort this out.
For the title at the top of the form saying that CSS is great, we've added this dynamic property:

I probably should have mentioned that you can add your own properties to widgets earlier!
You could now create a style sheet - here's mine:

This style sheet sets a default Verdana 14 point font for all widgets, then sets specific formatting for QT labels, push buttons and any widgets to which we've attached the class h1_title.
You can then apply this in your code:
# opens style sheet and returns string of text containing CSS
with open(r"temp.css") as css_file:
css = css_file.read()
# apply CSS
self.setStyleSheet(css)
This is what would appear when you run your form and click on the button:

In retrospect I should have put a bit more effort into making the styles look nice! Notice that the title appears in a different font size, because it has its dynamic property class set to h1_title.
This ability to attach CSS style sheets to GUI forms is a big selling-point to me for PyQt5.
- Should you be using PyQt?
- QT Designer
- Widgets
- Layouts
- Forms and code
- Slots and Signals
- CSS and Formatting (this blog)
- Models and Data