Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt give color to a specific element

This might be an easy question, but I'm trying to give a color to a specific QLabel in my application and it doesn't work.

The code I tried is the following :

nom_plan_label = QtGui.QLabel()
nom_plan_label.setText(nom_plan_vignette)
nom_plan_label.setStyleSheet("QLabel#nom_plan_label {color: yellow}")

Any hint would be appreciated

like image 610
Johanna Avatar asked Dec 20 '11 15:12

Johanna


1 Answers

There are a few things wrong with the stylesheet syntax you are using.

Firstly, ID selectors (i.e. #nom_plan_label) must refer to the objectName of the widget.

Secondly, it is only necessary to use selectors when a stylesheet is applied to an ancestor widget and you want certain style rules to cascade down to particular descendant widgets. If you're applying the stylesheet directly to one widget, the selector (and braces) can be left out.

Given the above two points, your example code would become either:

nom_plan_label = QtGui.QLabel()
nom_plan_label.setText(nom_plan_vignette)
nom_plan_label.setObjectName('nom_plan_label')
nom_plan_label.setStyleSheet('QLabel#nom_plan_label {color: yellow}')

or, more simply:

nom_plan_label = QtGui.QLabel()
nom_plan_label.setText(nom_plan_vignette)
nom_plan_label.setStyleSheet('color: yellow')
like image 92
ekhumoro Avatar answered Sep 24 '22 06:09

ekhumoro