wxgrid and GridCellChoiceEditor with colourpicker?

Hello,

I have a python application which uses wx and wx.grid to show a dialog which can edit values in a grid. I use several editors for the different options:

    #Create an editor for the protocol selection
    Editor_OnOff = wx.grid.GridCellChoiceEditor(['On.','Off.'], allowOthers=False)
    Editor_YesNo = wx.grid.GridCellChoiceEditor(['Yes.','No.'], allowOthers=False)
    Editor_EnabledDisabled = wx.grid.GridCellChoiceEditor(['Enabled.','Disabled.'], allowOthers=False)
    Editor_HighLow = wx.grid.GridCellChoiceEditor(['High.','Low.'], allowOthers=False)

    for l in list:
      self.locgrid.SetCellValue(i, 0, str(l))
      self.locgrid.SetReadOnly(i, 0, True)
      self.locgrid.SetCellValue(i, 1, list[l])
      if (l == 'Turnout DCC Only') or (l == 'XPressNet') or (l == 'Turnout auto off'):
        self.locgrid.SetCellEditor(i, 1, Editor_OnOff)
      elif (l == 'Touch calibrated') or (l == 'MM locs only'):
        self.locgrid.SetCellEditor(i, 1, Editor_YesNo)
      elif (l == 'Booster S/C enable') or (l == 'Acknowlegde enable') or (l == 'I2C manual control'):
        self.locgrid.SetCellEditor(i, 1, Editor_EnabledDisabled)
      elif (l == 'Booster S/C change'):
        self.locgrid.SetCellEditor(i, 1, Editor_HighLow)
      i += 1

For one of the values I need to be able to choose a colour value instead of a list of values. Is it possible to use a ColourDialog as an editor or have an editor trigger a ColourDialog to let the user choose from this dialog? The following function works to give a dialog and return the value I want (a string representation of the HTML colour value).

def OnCC(self, event):
        data = wx.ColourData()
        data.SetChooseFull(True)

        global configList

        # set the first custom color (index 0)
        data.SetCustomColour(0, (255, 170, 128))
        # set indexes 1-N here if you like.

        # set the default color in the chooser
        data.SetColour(wx.Colour(128, 255, 170))

        # construct the chooser
        dlg = wx.ColourDialog(self, data)

        if dlg.ShowModal() == wx.ID_OK:
            color = dlg.GetColourData().Colour
            NewValue = str(hex(color[2]+256*color[1]+256*256*color[0]))
            return NewValue
        dlg.Destroy()

I could pose the full application or make a sample application as an example of how I use this, but I hope the question is general enough to warrant an answer by itself.
The application runs on both Linux and Windows, I am trying to keep the code as portable as possible. I am using python 2.7 and python-wxgtk 2.8 on Linux, this being my main platform.

Peter Mansvelder

Hello Peter,

you could write your own editor and renderer for a colourpicker. I’ve made a small sample app which you can use as a base for your own editor/renderer. When you run the app, double click the top left cell.

GridColourPicker.py (2.53 KB)