在 PyQt 中更改 ComboBox 的背景颜色
Change background color of ComboBox in PyQt
我正在尝试更改组合框背景的颜色。我希望它是白色的,但我不能让它有任何不同于灰色的颜色。上图是我得到的,下图是我要的。
这是我的代码:
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_()
您必须更改 QPalette::Button
角色的颜色:
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_())
在Windows你需要:
app.setStyle("fusion")
为了避免更改整个应用程序的样式,我根据 使用了样式 sheet。
请注意,如 post 中所述,此样式 "the whole drop-down to mimic the native look, but it's not nice to do and not robust (and not portable)." 但是,由于更改应用程序的样式具有更大的影响,因此此方法适用于我的目的。
这是我的风格 sheet:
QComboBox QAbstractItemView {
border: 1px solid grey;
background: white;
selection-background-color: blue;
}
QComboBox {
background: red;
}
和代码:
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_()
我正在尝试更改组合框背景的颜色。我希望它是白色的,但我不能让它有任何不同于灰色的颜色。上图是我得到的,下图是我要的。
这是我的代码:
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_()
您必须更改 QPalette::Button
角色的颜色:
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_())
在Windows你需要:
app.setStyle("fusion")
为了避免更改整个应用程序的样式,我根据
请注意,如 post 中所述,此样式 "the whole drop-down to mimic the native look, but it's not nice to do and not robust (and not portable)." 但是,由于更改应用程序的样式具有更大的影响,因此此方法适用于我的目的。
这是我的风格 sheet:
QComboBox QAbstractItemView {
border: 1px solid grey;
background: white;
selection-background-color: blue;
}
QComboBox {
background: red;
}
和代码:
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_()