简体   繁体   English

如何获取 Pyqt5 中 QlineEdit 小部件的当前背景颜色?

[英]How do I get current background color for QlineEdit widget in Pyqt5?

How do I get the background color of a QlineEdit widget in pyqt5?如何在 pyqt5 中获取 QlineEdit 小部件的背景颜色?

To complete my comment, this regex "(?<=background-color:)(.*?)(?=[;\}])" can do the job for simple cases (see simulation I did here: https://regex101.com/r/SEB9hQ/1 ).为了完成我的评论,这个正则表达式"(?<=background-color:)(.*?)(?=[;\}])"可以完成简单案例的工作(参见我在这里所做的模拟: https:// regex101.com/r/SEB9hQ/1 )。 You can improve it to fit your needs.您可以对其进行改进以满足您的需求。 The captured group should be strip() ed to get rid of spaces.捕获的组应该被strip()编辑以去除空格。

By default, QLineEdit uses the Base palette role for its background and also uses that role for its backgroundRole() .默认情况下,QLineEdit 使用Base调色板角色作为其背景,也使用该角色作为其backgroundRole()

Since basic properties of stylesheets set some color roles for the local widget palette [1] , the solution is to use that along with the role:由于样式表的基本属性为本地小部件调色板[1]设置了一些颜色角色,因此解决方案是将其与角色一起使用:

background = label.palette().color(label.backgroundRole())
print(background.name())

[1] See this answer to know how QSS colors can alter a widget palette. [1] 请参阅此答案以了解 QSS colors 如何更改小部件调色板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM