3
votes
    {btn = QtGui.QPushButton('Button', self)

    palettes = btn.palette()
    palettes.setColor(btn.backgroundRole(),QtCore.Qt.green)

    btn.setPalette(palettes)
    btn.setAutoFillBackground(True)}

Using btn.backgroundRole() only provides green border to the button.
Using btn.foregroundRole() changes the text color of the button
Using btn.windowRole() gives me error

TypeError: arguments did not match any overloaded call:
QPalette.setColor(QPalette.ColorGroup, QPalette.ColorRole, QColor): argument 1 has unexpected type 'str'
QPalette.setColor(QPalette.ColorRole, QColor): argument 1 has unexpected type 'str'

Tried QtGui.QPalette.Background in place of btn.windowRole() but does nothing
Also tried QtGui.QPalette.Base but does nothing
And also QtGui.QPalette.Window does nothing

How can I change button color using palettes?
I am unable to rectify the problem. I am using windows 7, PyQt4 and Python 3.4

1
Don't know about PyQt4, but these are enums at C++. Maybe a problem with PyQt? Could you try to update PyQt? (I'm not of much help beyond this since I'm not a Python programmer...)Vinícius Gobbo A. de Oliveira

1 Answers

2
votes

You can read in Qt documentation about QPalette :

Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows XP, Windows Vista, and the Mac OS X styles.

Windows 7 style also does not use palette colors for buttons. You should use stylesheets to give a color to your button. If you use some other theme than Windows 7 style, you can use QPalette.Button role to change the button color.