16

I have the follow code:

self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(0, 550, 150, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))

How do I get the background color of this button to change. I have tried using Palette and I am having no success. I would like the color to be red. I can't seem to call it correctly. Any help would be great.

Trying_hard
  • 8,931
  • 29
  • 62
  • 85
  • 1
    That code looks like Autogenerated code from pyuic? Is that right? –  Dec 18 '13 at 22:55

2 Answers2

46

You can change the style of the button:

self.pushButton.setStyleSheet("background-color: red")

It's like CSS.

Alvaro Fuentes
  • 16,937
  • 4
  • 56
  • 68
  • 2
    This doesn't work for me on PyQt 5.14. I have to set `border: none` to change color, according to [this answer](https://stackoverflow.com/a/55585279/3926429). – cges30901 Feb 24 '20 at 14:12
  • Hum... It's been ages since I used PyQt, but certainly it seems bizarre that one needs now to remove the border in order to change the background color... maybe a bug? :[ – Alvaro Fuentes Feb 25 '20 at 12:45
15

Here is a sample code. You can choose any style to set background color.

QPushButton button1, button2, button3;

button1.setStyleSheet("background-color: red");

button2.setStyleSheet("background-color:#ff0000;");

button3.setStyleSheet("background-color:rgb(255,0,0)");
AB Bolim
  • 1,997
  • 2
  • 23
  • 47