[wxPython] - Grid with ComboBox behavior

Hi All,

i’m having a wierd behavior with a ComboBox cell in a grid,

when i press a key (“2”) when on the cell, the combobox is opened but doesn’t search “2”

only after clicking again “2” the comboBox changes to “2”.

i have this small example:

import wx

import wx.grid as gridlib

···

#---------------------------------------------------------------------------

class CustomDataTable(gridlib.PyGridTableBase):

def init(self):

gridlib.PyGridTableBase.init(self)

self.colLabels = [‘number’]

self.dataTypes = [gridlib.GRID_VALUE_CHOICE+":1,2,3",

]

self.data = [

[‘1’],[‘2’],

]

required methods for the wxPyGridTableBase interface

def GetNumberRows(self):

return len(self.data)

def GetNumberCols(self):

return len(self.colLabels)

def IsEmptyCell(self, row, col):

try:

return not self.data[row][col]

except IndexError:

return True

def GetValue(self,row,col):

return self.data[row][col]

def SetValue(self, row, col, value):

self.data[row][col] = value

def GetColLabelValue(self, col):

return self.colLabels[col]

def GetTypeName(self, row, col):

return self.dataTypes[col]

if name == ‘main’:

app = wx.PySimpleApp()

frame = wx.Frame(None,-1)

grid = wx.grid.Grid(frame)

grid.SetTable(CustomDataTable())

frame.Show(True)

app.MainLoop()

Thanks for any ideas on how to solve this.

This is an unfortunate side effect due to how the cell editors are implemented. Since the combo widget doesn't get the key event for the first "2" in a natural way, then it doesn't behave normally. Basically the grid is what gets the first event, and it then shows the editor widget and sets the value to a string corresponding to that first key event. When the next key event happens the combobox is where the focus is at so it then does what you expect.

···

On 12/29/09 7:35 AM, yoav glazner wrote:

Hi All,

i'm having a wierd behavior with a ComboBox cell in a grid,
when i press a key ("2") when on the cell, the combobox is opened but
doesn't search "2"
only after clicking again "2" the comboBox changes to "2".

--
Robin Dunn
Software Craftsman

OK, so how can i make up for this unfortunate side affect?

maybe if i catch the EVT_KEY_DOWN i have a chance?

···

On Tue, Dec 29, 2009 at 9:52 PM, Robin Dunn robin@alldunn.com wrote:

On 12/29/09 7:35 AM, yoav glazner wrote:

Hi All,

i’m having a wierd behavior with a ComboBox cell in a grid,

when i press a key (“2”) when on the cell, the combobox is opened but

doesn’t search “2”

only after clicking again “2” the comboBox changes to “2”.

This is an unfortunate side effect due to how the cell editors are

implemented. Since the combo widget doesn’t get the key event for the

first “2” in a natural way, then it doesn’t behave normally. Basically

the grid is what gets the first event, and it then shows the editor

widget and sets the value to a string corresponding to that first key

event. When the next key event happens the combobox is where the focus

is at so it then does what you expect.

If you have your own custom cell editor then you can override the StartingKey method and react to that key however you want.

···

On 12/29/09 1:07 PM, yoav glazner wrote:

On Tue, Dec 29, 2009 at 9:52 PM, Robin Dunn <robin@alldunn.com > <mailto:robin@alldunn.com>> wrote:

    On 12/29/09 7:35 AM, yoav glazner wrote:
     > Hi All,
     >
     > i'm having a wierd behavior with a ComboBox cell in a grid,
     > when i press a key ("2") when on the cell, the combobox is opened but
     > doesn't search "2"
     > only after clicking again "2" the comboBox changes to "2".

    This is an unfortunate side effect due to how the cell editors are
    implemented. Since the combo widget doesn't get the key event for the
    first "2" in a natural way, then it doesn't behave normally. Basically
    the grid is what gets the first event, and it then shows the editor
    widget and sets the value to a string corresponding to that first key
    event. When the next key event happens the combobox is where the focus
    is at so it then does what you expect.

OK, so how can i make up for this unfortunate side affect?
maybe if i catch the EVT_KEY_DOWN i have a chance?

--
Robin Dunn
Software Craftsman