PySide没有开箱即用的色卡控件,需要扩充QComboBox控件,来进行使用,直接上代码
其中exec_标识模态Dialog
import sys
from PySide2.QtWidgets import QApplication, QMainWindow, QComboBox, QLabel
from PySide2.QtGui import QColor, QPixmap
app = QApplication(sys.argv)
window = QMainWindow()
window.resize(300, 150)
combo_box = QComboBox(window)
colors = [QColor(255, 0, 0), QColor(0, 255, 0), QColor(0, 0, 255)]
for color in colors:
pixmap = QPixmap(16, 16)
pixmap.fill(color)
combo_box.addItem(pixmap, "", color)
label = QLabel(window)
combo_box.activated[int].connect(lambda index: update_label(label, combo_box, index))
window.show()
sys.exit(app.exec_())
def update_label(label, combo_box, index):
color = combo_box.itemData(index, role=Qt.UserRole)
label.setAutoFillBackground(True)
label.setPalette(QPalette(color))