Pyside Combobox Color Selector

Updated vegaseat 2 Tallied Votes 2K Views Share

I am exploring some of the PySide (PyQT public) widgets like the combo box, and how colors are applied ...

'''ps_combobox_colors1.py
explore the PySide QComboBox widget selecting colors

for Python33 use the Windows self-extracting installer
PySide-1.1.2qt483.win32-py3.3.exe
from:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
'''

from PySide.QtCore import *
from PySide.QtGui import *

class ComboBox(QWidget):
    def __init__(self, color_dict, parent=None):
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(320, 200, 340, 220)
        self.setWindowTitle('Select a color from the combo box')

        self.combo = QComboBox(self)

        self.frame = QFrame(self)
        self.frame.setFrameStyle(QFrame.Box|QFrame.Sunken)

        grid = QGridLayout()
        # addWidget(QWidget, row, column, rowSpan, columnSpan)
        grid.addWidget(self.combo, 1, 1, 1, 1)
        grid.addWidget(self.frame, 1, 2, 3, 2)
        self.setLayout(grid)

        self.color_dict = color_dict
        # create a sorted color list
        self.color_list = sorted(color_dict.keys())
        # load the combobox
        for color in self.color_list:
            self.combo.addItem(color)
        # bind/connect selection to an action method
        self.connect(self.combo, SIGNAL('currentIndexChanged(QString)'),
            self.changedIndex)

    def changedIndex(self, value):
        """
        item in the combox has been changed/selected
        """
        #print("value = %s" % value)  # test
        color = value
        # change color of widget self.frame
        style_str = "QWidget {background-color: %s}"
        self.frame.setStyleSheet(style_str % self.color_dict[color])


# colors are in HTML '#RRGGBB' format --> Red, Green, Blue hex 00 to ff
color_dict = {
'red': '#ff0000',
'green': '#00ff00',
'blue': '#0000ff',
'yellow': '#ffff00',
'gold': '#ffd700',
'pink': '#ffc0cb',
'bisque': '#ffe4c4',
'ivory': '#fffff0',
'black': '#000000',
'white': '#ffffff',
'violet': '#ee82ee',
'silver': '#c0c0c0',
'forestgreen': '#228b22',
'brown': '#a52a3a',
'chocolate': '#d2691e',
'azure': '#fffff0',
'orange': '#ffa500'
}

app = QApplication([])
combo = ComboBox(color_dict)
combo.show()
app.exec_()