Tendero
Tendero

Reputation: 1166

Change background color of ComboBox in PyQt

I'm trying to change the color of the combobox background. I want it to be white, but I can't make it have any color different from gray. The picture on top is what I'm getting, and the one on the bottom is what I'm after.

enter image description here

Here's my code:

from PyQt5 import QtWidgets, QtGui

class combodemo(QtWidgets.QWidget):
   def __init__(self, parent = None):
      super(combodemo, self).__init__(parent)

      layout = QtWidgets.QHBoxLayout()
      self.cb = QtWidgets.QComboBox()
      self.cb.addItem("1")
      self.cb.addItem("2")
      pal = self.cb.palette()
      pal.setColor(self.cb.backgroundRole(),QtGui.QColor(255,255,255))
      self.cb.setPalette(pal)
      self.cb.setAutoFillBackground(True)

      layout.addWidget(self.cb)
      self.setLayout(layout)

if __name__ == '__main__':
   app = QtWidgets.QApplication([])
   ex = combodemo()
   ex.show()
   app.exec_()

Upvotes: 2

Views: 8617

Answers (2)

mherzog
mherzog

Reputation: 1230

To avoid changing the style for the entire application, I used a style sheet, per How to set background color of QComboBox button?.

Note that, as stated in the post, this styles "the whole drop-down to mimic the native look, but it's not nice to do and not robust (and not portable)." However, since changing the style of the application has larger implications, this approach works for my purposes.

Here's my style sheet:

QComboBox QAbstractItemView {
  border: 1px solid grey;
  background: white;
  selection-background-color: blue;
}
QComboBox {
  background: red;
}

and code:

from PyQt5 import QtWidgets, QtGui

class combodemo(QtWidgets.QWidget):
   def __init__(self, parent = None):
      super(combodemo, self).__init__(parent)

      layout = QtWidgets.QHBoxLayout()
      self.cb = QtWidgets.QComboBox()
      self.cb.addItem("1")
      self.cb.addItem("2")

      cbstyle =  "QComboBox QAbstractItemView {"
      cbstyle += " border: 1px solid grey;"
      cbstyle += " background: white;"
      cbstyle += " selection-background-color: blue;"
      cbstyle += " }"
      cbstyle += " QComboBox {"
      cbstyle += " background: white;"
      cbstyle += "}"
      self.cb.setStyleSheet(cbstyle)

      layout.addWidget(self.cb)
      self.setLayout(layout)

if __name__ == '__main__':
   app = QtWidgets.QApplication([])
   ex = combodemo()
   ex.show()
   app.exec_()

Upvotes: 3

eyllanesc
eyllanesc

Reputation: 244311

You have to change the color of the QPalette::Button role:

from PyQt5 import QtWidgets, QtGui

class combodemo(QtWidgets.QWidget):
    def __init__(self, parent = None):
        super(combodemo, self).__init__(parent)

        layout = QtWidgets.QHBoxLayout(self)

        self.cb = QtWidgets.QComboBox()
        self.cb.addItems(["1", "2"])

        pal = self.cb.palette()
        pal.setColor(QtGui.QPalette.Button, QtGui.QColor(255,255,255))
        self.cb.setPalette(pal)

        layout.addWidget(self.cb)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ex = combodemo()
    ex.show()
    sys.exit(app.exec_())

In Windows you need:

app.setStyle("fusion")

Upvotes: 4

Related Questions