Understanding handling wx.grid.Grid events

1,898 views
Skip to first unread message

Neven Goršić

unread,
Feb 28, 2010, 5:04:30 PM2/28/10
to wxpytho...@googlegroups.com
Dear all!

I want to highlight specific row when I click on any cell in that row.
Just like selection background colour shows up over row when one clicks on the row label cell.
I setup binder for left mouse click:

    self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.OnCellLeftClick)

and wrote the handler:

    def OnCellLeftClick(self, event):
        self.grid.SelectRow(event.GetRow(),False)
        event.Skip()

And it doesn't work, but if I comment out line event.Skip(), it works fine.
Well, not quite, because it disables editing the cell.

I understand that if I remove command event.Skip(), left mouse click event are erased from the event
stack and therefore will be impossible to "gather" two left click in the row, needed for detecting Dclick.

But I must admit that I don't understand the behavior of the self.grid.SelectRow(event.GetRow(),False)
in that event handling setup. The same self.grid.SelectRow() command works fine out of the event
handler method.

Can you, please, help me to understand what is going on here?

Thank you very much,

Regards,

Neven

Robin Dunn

unread,
Mar 1, 2010, 4:04:09 PM3/1/10
to wxpytho...@googlegroups.com
On 2/28/10 2:04 PM, Neven Goršić wrote:
> Dear all!
>
> I want to highlight specific row when I click on any cell in that row.
> Just like selection background colour shows up over row when one clicks
> on the row label cell.
> I setup binder for left mouse click:
>
> self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.OnCellLeftClick)
>
> and wrote the handler:
>
> def OnCellLeftClick(self, event):
> self.grid.SelectRow(event.GetRow(),False)
> event.Skip()
>

Did you also put the grid in row selection mode? Something like this:

self.SetSelectionMode(wx.grid.Grid.SelectRows)

> And it doesn't work, but if I comment out line event.Skip(), it works fine.
> Well, not quite, because it disables editing the cell.
>

Correct. You'll also disable anything else that is triggered by mouse
clicks. So instead of hooking in at the mouse event level I would let
the grid do all it wants with the mouse and do the row selection from
the EVT_GRID_SELECT_CELL higher-level event instead.

self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.onSelectCell, self)


def onSelectCell(self, evt):
self.SelectRow(evt.GetRow())
evt.Skip()

--
Robin Dunn
Software Craftsman
http://wxPython.org

Neven Goršić

unread,
Mar 3, 2010, 5:22:09 AM3/3/10
to wxpytho...@googlegroups.com
Thank you - it WORKS!  :-)

I would still like to know were I did wrong, or why .SelectRow() doesn't work with mouse event.
Where I can find a good (but not too expert) text about wxPython events
as a next step from the wxPython in Action (event part)?

Thanks again!

Neven

-----------------------------------------


Mike Driscoll

unread,
Mar 3, 2010, 10:58:39 AM3/3/10
to wxPython-users
Hi Neven,

On Mar 3, 4:22 am, Neven Goršić <neven.gor...@gmail.com> wrote:
> Thank you - it WORKS!  :-)
>
> I would still like to know were I did wrong, or why .SelectRow() doesn't
> work with mouse event.
> Where I can find a good (but not too expert) text about wxPython events
> as a next step from the wxPython in Action (event part)?
>
> Thanks again!
>
> Neven
>

Do a search for "event" or "events" on the wxPython wiki. There are a
bunch of pages on the subject.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Neven Goršić

unread,
Mar 7, 2010, 4:33:05 AM3/7/10
to wxpytho...@googlegroups.com
Hi!

I want to do 2 simple actions on a grid, but choosing appropriate events are still out of my rich :-)

1. I want to move grid cursor to the right instead of down after editing cell.
I tried with:

self.grid.Bind(wx.grid.EVT_GRID_CELL_CHANGE,self.onCellChange)

and

    def onCellChange(self,event):
            . . .
        self.grid.MoveCursorRight(False)
        self.grid.MoveCursorUp(False)

I can see blinking on these cells (right and up) for a second and than the cursor is set down instead of right by the grid automatism.

 2. I want to use Ctrl key for deselecting rows from already selected group of rows. I see that grid provides Ctrl mechanism for appending selected new row to the existing group of selected rows. But holding Ctrl key and left klicking on some ALREADY selected row doesn' deselect it as it should.
I tried:

self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.OnLabelLeftClick)

and

    def OnLabelLeftClick(self, evt):
        if evt.ControlDown() and evt.GetRow() in self.grid.GetSelectedRows():
            self.grid.DeselectRow(evt.GetRow())

But it doesn't work ....
Can you help me?

Regards,

Neven

---------------------------------------

Robin Dunn

unread,
Mar 9, 2010, 7:05:42 PM3/9/10
to wxpytho...@googlegroups.com
On 3/7/10 1:33 AM, Neven Goršić wrote:
> Hi!
>
> I want to do 2 simple actions on a grid, but choosing appropriate events
> are still out of my rich :-)
>
> 1. I want to move grid cursor to the right instead of down after editing
> cell.
> I tried with:
>
> self.grid.Bind(wx.grid.EVT_GRID_CELL_CHANGE,self.onCellChange)
>
> and
>
> def onCellChange(self,event):
> . . .
> self.grid.MoveCursorRight(False)
> self.grid.MoveCursorUp(False)
>
> I can see blinking on these cells (right and up) for a second and than
> the cursor is set down instead of right by the grid automatism.

Look at the GridEnterHandler.py in the demo for an example of this.

Neven Goršić

unread,
Mar 11, 2010, 1:45:33 AM3/11/10
to wxpytho...@googlegroups.com
Thank you for showing me example for positioning grid cursor to the right after editing.

But I still doesn't know how to solve second problem: deselecting already selected rows by holding Ctrl key. Can you help me?

Thanks!

Kind regards,

Neven

--------------------------

Robin Dunn

unread,
Mar 11, 2010, 1:31:29 PM3/11/10
to wxpytho...@googlegroups.com
On 3/10/10 10:45 PM, Neven Goršić wrote:
> Thank you for showing me example for positioning grid cursor to the
> right after editing.
>
> But I still doesn't know how to solve second problem: deselecting
> already selected rows by holding Ctrl key. Can you help me?

Sorry, I forgot to mention that we'll need more information to help with
that. What exactly does "it doesn't work" mean? Can you provide a
small runnable sample for people to experiment with?

Reply all
Reply to author
Forward
0 new messages