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

雨燕双飞 提交于 2019-12-11 06:43:35

问题


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.


回答1:


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.




回答2:


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() +";");


来源:https://stackoverflow.com/questions/17271366/how-to-get-the-background-color-of-a-button-or-label-qpushbutton-qlabel-in-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!