[英]How to get the background color of a button or label (QPushButton, QLabel) in PyQt
I am quite new to PyQt.我对 PyQt 很陌生。 Does anyone tell me how to get the background color of a button or label (QPushButton, QLabel) in PyQt.
有没有人告诉我如何在 PyQt 中获取按钮或标签(QPushButton、QLabel)的背景颜色。
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() +";");
I haven't used PyQt, but I think API should be very similar to C++.我没有使用过 PyQt,但我认为 API 应该与 C++ 非常相似。 To get background color of QWidget-based class, first get its palette and then call QPalette::color() with QPalette::Window role.
要获取基于 QWidget 的类的背景颜色,首先获取其调色板,然后使用QPalette::Window角色调用QPalette::color() 。
This worked for me这对我有用
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
import sys
app = QApplication(sys.argv)
window = QWidget()
window.show()
myLabel = QLabel()
colorOfmyLabel = myLabel.palette().window().color().name()
print(colorOfmyLabel)
sys.exit(app.exec_())
the output:输出:
#f0f0f0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.