Interesting thing - wxGrid, and Mouse Events

Hi !

See this code:

#Boa:Frame:Frame1

import wx
import wx.grid

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1GRID1,
] = [wx.NewId() for _init_ctrls in range(2)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(346, 180), size=wx.Size(400, 479),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(392, 445))

        self.grid1 = wx.grid.Grid(id=wxID_FRAME1GRID1, name='grid1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(392, 445), style=0)
        self.grid1.Bind(wx.EVT_MOUSE_EVENTS, self.OnGrid1MouseEvents)
        self.grid1.Bind(wx.EVT_LEFT_UP, self.OnGrid1LeftUp)
        self.grid1.Bind(wx.EVT_MOTION, self.OnGrid1Motion)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.grid1.CreateGrid(3,4)

    def OnGrid1MouseEvents(self, event):
        print "a"
        event.Skip()

    def OnGrid1LeftUp(self, event):
        print "b"
        event.Skip()

    def OnGrid1Motion(self, event):
        print "c"
        event.Skip()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    wx.InitAllImageHandlers()
    frame = create(None)
    frame.Show()

    app.MainLoop()

When I started it, and do anything in grid (click, move the mouse), no one of the events fired.
I don't understand it, because I thinking as Delphi developer: in the inherited classes I can asscess, and redefine mouse clicks and moves.

But it seems to be that the click events are missed somewhere, only the grid click events have been remaining.

Or I thinking wrong ?

Thanx for help: ft

Hi,

       self.grid1.Bind(wx.EVT_MOUSE_EVENTS, self.OnGrid1MouseEvents)

I believe you should binds such sort of events to the grid's window ( grid.GetGridWindow() ), not to grid object itself.

    Vladimir Ignatov

Hi,

> self.grid1.Bind(wx.EVT_MOUSE_EVENTS, self.OnGrid1MouseEvents)

I believe you should binds such sort of events to the grid's window (
grid.GetGridWindow() ), not to grid object itself.

    Vladimir Ignatov

This is correct - I just wanted to respond to make it more clear,
because the docs don't mention this. The wx.Grid window is a wrapper
around several other windows, and those other windows do not forward
"regular" mouse events. You must bind to the internal grid windows
(like the one you can retrieve with GetGridWindow()) to catch these.

There are 4 internal windows:
GetGridWindow() - this is the client area where the cells are
GetGridRowLabelWindow() - this is the area on the left where the row labels are
GetGridColLabelWindow() - the area at the top where the column labels are
GetGridCornerLabelWindow() - top left corner

Working effectively with these does require a fair amount of knowlegde
about the internals of wxGrid, but for mouse events the main thing to
remember is that you need to call evt.Skip() or selection, focus, etc
will break.

···

On 7/6/05, Vladimir Ignatov <100xcd@100xcd.com> wrote:

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org