Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the background color of a button or label (QPushButton, QLabel) in PyQt

I am quite new to PyQt. Does anyone tell me how to get the background color of a button or label (QPushButton, QLabel) in PyQt.

like image 675
Linh Nguyen Avatar asked Dec 27 '22 01:12

Linh Nguyen


2 Answers

Here is a sample code. This will help you.

QPushButton button1, button2;
button1.setStyleSheet("background-color:#ff0000;");

//To get Background color
QColor color = button1.palette().button().color();

//To set fetched color
button2.setStyleSheet("background-color:" + color.name() +";");
like image 123
AB Bolim Avatar answered Dec 29 '22 07:12

AB Bolim


I haven't used PyQt, but I think API should be very similar to C++. To get background color of QWidget-based class, first get its palette and then call QPalette::color() with QPalette::Window role.

like image 44
Oleg Shparber Avatar answered Dec 29 '22 05:12

Oleg Shparber