How do I get current background color for QlineEdit widget in Pyqt5?
Question:
How do I get the background color of a QlineEdit widget in pyqt5?
Answers:
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). You can improve it to fit your needs.
The captured group should be strip()
ed to get rid of spaces.
By default, QLineEdit uses the Base
palette role for its background and also uses that role for its 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:
background = label.palette().color(label.backgroundRole())
print(background.name())
[1] See this answer to know how QSS colors can alter a widget palette.
How do I get the background color of a QlineEdit widget in pyqt5?
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). You can improve it to fit your needs.
The captured group should be strip()
ed to get rid of spaces.
By default, QLineEdit uses the Base
palette role for its background and also uses that role for its 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:
background = label.palette().color(label.backgroundRole())
print(background.name())
[1] See this answer to know how QSS colors can alter a widget palette.