I find that every time I bind an event, it is bound to all cells of a
grid.
For example, if I bind EVT_GRID_CELL_LEFT_CLICK, then the
corresponding function is called every time I left click any cell.
Is there a way to have only a specific cell bound? For example only
cell (0, 0)?
The only way I can think of is write the function like this:
def event_handler(self, evt):
row = evt.GetRow()
col = evt.GetCol()
if row == 0 and col == 0:
do stuff....
but that seems very inelegant, as any changes to the format of the
grid could necessitate changes to the function (i.e. if I insert a row
above 0, 0, then I would want 1, 0 to be the cell with the event).
It would be best if I could have a fixed function and just be able to
bind the event that calls it to specific cells.
Something like self.grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK,
event_handler, cellThatIWantToBind)
Is there a way to do this?
I find that every time I bind an event, it is bound to all cells of a
grid.
For example, if I bind EVT_GRID_CELL_LEFT_CLICK, then the
corresponding function is called every time I left click any cell.
Is there a way to have only a specific cell bound? For example only
cell (0, 0)?
No. Since the grid can potentially have millions and millions of cells it seems rather absurd to have to support having millions of event bindings to go along with it. Also the event model in wx is events can be bound to (or in behalf of when using the source/id params) a window, and cells in a grid are not individual windows. So in other words there would be no way to do what you want in the current design.
The only way I can think of is write the function like this:
def event_handler(self, evt):
row = evt.GetRow()
col = evt.GetCol()
if row == 0 and col == 0:
do stuff....
but that seems very inelegant, as any changes to the format of the
grid could necessitate changes to the function (i.e. if I insert a row
above 0, 0, then I would want 1, 0 to be the cell with the event).
Just don't hard code the digits in your handler function, for example you could use a list of cell positions that need to react a certain way and then check it in the handler like this:
def handler(self, evt):
if (evt.GetRow(), evt.GetCol()) in self.someList:
do stuff...
It would be best if I could have a fixed function and just be able to
bind the event that calls it to specific cells.
Something like self.grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK,
event_handler, cellThatIWantToBind)
Is there a way to do this?
Cells are not independent objects, so even if you did something like that (with a lambda or functools.partial for example) it would still need to compare row,col values.
Ah, I see. That clarifies the motive behind this design. It's good
to know for sure that such a scheme is not possible/practical so I can
consider the best alternative. I like your list idea. I will try
that.