1

So far I am only able to customize the button text color:

button = QtGui.QPushButton()
palette = QtGui.QPalette(button.palette())
palette.setColor(QtGui.QPalette.ButtonText, QtGui.QColor('blue'))
button.setPalette(palette)

But how to change the button background color?

None of this will change the button background color:

    palette.setColor(QtGui.QPalette.Foreground, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Button, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Light, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Midlight, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Dark, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Mid, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Text, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.BrightText, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.ButtonText, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Base, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Background, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Midlight, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Shadow, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.Highlight, QtGui.QColor('red'))
    palette.setColor(QtGui.QPalette.HighlightedText, QtGui.QColor('red'))
alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • how about this? : `self.pushButton.setStyleSheet("background-color: red")` – Mazdak Jul 30 '14 at 20:52
  • This is CSS. The question is how to do the same without using it. – alphanumeric Jul 30 '14 at 20:53
  • looks like `pallet.setColorGroup`? (http://pyqt.sourceforge.net/Docs/PyQt4/qpalette.html#setColorGroup) – Gerrat Jul 30 '14 at 21:00
  • How to use `setColorGroup`? I can't find any example... :( – alphanumeric Jul 30 '14 at 21:03
  • Not sure...there's also an old thread on a mailing list that gives an example of setting a button's background color: http://www.riverbankcomputing.com/pipermail/pyqt/2006-February/012279.html – Gerrat Jul 30 '14 at 21:21
  • Another possibly related question here: http://stackoverflow.com/questions/12655538/how-to-set-qwidget-background-color – Gerrat Jul 30 '14 at 21:24
  • Doesn's work with QPushButton or any other QButtons. I can't believe the most popular widget can't be customized with the colors without using CSS (which does more harm then good) – alphanumeric Jul 30 '14 at 21:28

2 Answers2

2

you need to set the right "role" and append "setAutoFillBackground()":

button = QtGui.QPushButton()
palette = self.button.palette()
role = self.button.backgroundRole() #choose whatever you like
palette.setColor(role, QColor('red'))
button.setPalette(palette)
self.button.setAutoFillBackground(True)
Thomas
  • 1,468
  • 4
  • 14
  • 20
  • You may also need to apply setFlat() depending on the theme (I did!). See https://stackoverflow.com/questions/21685414/qt5-setting-background-color-to-qpushbutton-and-qcheckbox answers and comments – rolinger Jun 01 '22 at 11:58
0

Some QStyles, specifically Windows Vista, takes certain aspects of the gui from the operating system, so you can't change them directly with a QPalette.

You can query available QStyles with

QStyleFactory.keys()

['windowsvista', 'Windows', 'Fusion']

Change the qstyle to some other Style, with

app.setStyle("Fusion")

Now, it will respect a QPalette's

QPalette.Button