如何在 PyQt 中获取按钮或标签(QPushButton、QLabel)的背景颜色

Die*_*inh 3 python qt pyqt uibackgroundcolor

我对 PyQt 很陌生。有没有人告诉我如何在 PyQt 中获取按钮或标签(QPushButton、QLabel)的背景颜色。

AB *_*lim 7

这是一个示例代码。这会帮助你。

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() +";");
Run Code Online (Sandbox Code Playgroud)


Ole*_*ber 2

我没有使用过PyQt,但我认为API应该和C++非常相似。要获取基于 QWidget 的类的背景颜色,首先获取其调色板,然后使用QPalette::Window角色调用QPalette::color()