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'))
7
  • how about this? : self.pushButton.setStyleSheet("background-color: red") Commented Jul 30, 2014 at 20:52
  • This is CSS. The question is how to do the same without using it. Commented Jul 30, 2014 at 20:53
  • looks like pallet.setColorGroup? (pyqt.sourceforge.net/Docs/PyQt4/qpalette.html#setColorGroup) Commented Jul 30, 2014 at 21:00
  • How to use setColorGroup? I can't find any example... :( Commented Jul 30, 2014 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: riverbankcomputing.com/pipermail/pyqt/2006-February/012279.html Commented Jul 30, 2014 at 21:21

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

You may also need to apply setFlat() depending on the theme (I did!). See stackoverflow.com/questions/21685414/… answers and comments
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.