How to get background color of a dialog/window in RGB format?
Asked
Active
Viewed 1.7k times
3 Answers
13
Use QWidget::palette
to access widget's palette and QPalette::color
to obtain the background color:
color = widget.palette().color(QPalette.Background)
print color.red(), color.green(), color.blue()

Pavel Strakhov
- 39,123
- 5
- 88
- 127
-
1typo: `QPalette::Background` actually – thiagowfx Apr 15 '14 at 01:00
-
6No, it's `.` in Python. – Pavel Strakhov Apr 15 '14 at 15:33
2
http://qt-project.org/doc/qt-4.8/qwidget.html#palette-prop
http://qt-project.org/doc/qt-4.8/qpalette.html
http://qt-project.org/doc/qt-4.8/qpalette.html#ColorRole-enum
QPalette::Window - 10 - A general background color.
QPixmap::grabWindow();
can get you any part of the desktop.
Hope that helps.

phyatt
- 18,472
- 5
- 61
- 80
-1
import sys
from PyQt4 import QtGui, QtCore
class MainWin(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
pal=QtGui.QPalette()
role = QtGui.QPalette.Background
pal.setColor(role, QtGui.QColor(0, 0, 255))
self.setPalette(pal)
or with stylesheet
self.setStyleSheet("background:blue)

raton
- 418
- 5
- 14