Custom grid cell editor: GridCellComboBoxEditor

ioprst <[email protected]>
Newsgroups gmane.comp.python.wxpython
Message-ID <[email protected]>
I am writing a grid cell editor with a ComboBox. When I open (activate) the 
editor, I see strange behavior. 

   1. If the text in the widget is not selected and I open the list with an 
   element, then this list closes immediately.
   2. If the text in the widget is selected, then when you open the list, 
   it remains. 

What could be the problem?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
import wx.grid


class GridCellComboBoxEditor(wx.grid.GridCellEditor):

    def __init__(self, choices):
        super().__init__()
        self.choices = choices

    def Create(self, parent, id, evtHandler):
        self.control = wx.ComboBox(parent, id, choices=self.choices)
        self.SetControl(self.control)

        if evtHandler:
            self.control.PushEventHandler(evtHandler)

    def Clone(self):
        return GridCellComboBoxEditor(self.choices)

    def BeginEdit(self, row, col, grid):
        self.startValue = grid.GetTable().GetValue(row, col)
        pos = self.control.FindString(self.startValue)
        if pos == wx.NOT_FOUND:
            pos = 0
        self.control.SetSelection(pos)

    def EndEdit(self, row, col, grid, oldval):
        self.endValue = self.control.GetValue()
        if self.endValue != oldval:
            return self.endValue
        else:
            return None

    def ApplyEdit(self, row, col, grid):
        grid.GetTable().SetValue(row, col, self.endValue)

    def Reset(self):
        self.control.SetStringSelection(self.startValue)
        self.control.SetInsertionPointEnd()


if __name__ == '__main__':
    class Frame(wx.Frame):

        def __init__(self, parent):
            super().__init__(parent, title='Test GridCellComboBoxEditor')

            vbox = wx.BoxSizer(wx.VERTICAL)

            grid = wx.grid.Grid(self, size=(256, 128))
            vbox.Add(grid, flag=wx.ALL, border=5)

            self.SetSizer(vbox)
            self.Layout()

            grid.CreateGrid(4, 2)
            table = grid.GetTable()  # type: wx.grid.GridTableBase

            table.SetValue(0, 0, "Choice1")
            table.SetValue(1, 0, "Choice2")

            choices = ['Choice1', 'Choice2', 'Choice3', 'Choice4', 
'Choice5']
            grid.SetCellEditor(0, 0, GridCellComboBoxEditor(choices))
            grid.SetCellEditor(1, 0, GridCellComboBoxEditor(choices))
            grid.SetCellEditor(2, 0, GridCellComboBoxEditor(choices))
            grid.SetCellEditor(3, 0, GridCellComboBoxEditor(choices))

    app = wx.App()
    frame = Frame(None)
    frame.Show()
    app.MainLoop()


-- 
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/80858e90-2a21-4e51-be29-10c592e5aa86%40googlegroups.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.