Problem with Context Menus and Grids

Hi,

I am trying to use a context menu with a grid (without
much success).

Here is my first cut at getting the menu to appear

class MyGrid(wx.grid.Grid):
     def __init__(self,parent):
         wx.grid.Grid.__init__(self,parent)
         self.popupmenu = wx.Menu()
         for text in ["tom", "dick","harry"] :
             item = self.popupmenu.Append(-1,text)
             self.Bind(wx.EVT_MENU,self.OnPopupItemSelected,item)
         self.Bind(wx.EVT_CONTEXT_MENU,self.OnShowPopup)

     def OnShowPopup(self,event):
         wx.MessageBox("OnShowPopup")
         pos=event.GetPosition()
         pos = self.ScreenToClient(pos)
         self.PopupMenu(self.popupmenu,pos)

     def OnPopupItemSelected(self,event) :
         item=self.popupmenu.FindItemById(event.GetId())
         text = item.GetText()
         wx.MessageBox("You Selected %s" % text)

On my linux box the only way I can get the menu to popup
is if I have an editor going in the cell that I right click on.

On my mac box, the menu does not appear at all, even with
the editor working in a cell. In fact, on the mac, if I control-click
on a cell displaying an editor window I get different context menu
altogether ie /cut/paste/copy etc...

Is it possible to get contextual menus to work better with grids?

Thanks,

Jerry

Jerry LeVan wrote:

Hi,

I am trying to use a context menu with a grid (without
much success).

I was reminded the other day that the grid class eats most mouse events, so the context menu event isn't getting any chance to be created. You can instead intercept the raw mouse events by binding your handlers to self.GetGridWindow() or you can use EVT_GRID_CELL_RIGHT_CLICK.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thanks for the tip Robin :slight_smile: I could not get the first alternate to work but the
second one does the job ( I also needed to comment out the ScreenToClient call)...

class MyGrid(wx.grid.Grid):
     def __init__(self,parent):
         wx.grid.Grid.__init__(self,parent)
         self.popupmenu = wx.Menu()
         for text in ["tom", "dick","harry"] :
             item = self.popupmenu.Append(-1,text)
             self.Bind(wx.EVT_MENU,self.OnPopupItemSelected,item)
         #self.GetGridWindow().Bind(wx.EVT_CONTEXT_MENU,self.OnShowPopup)
         self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,self.OnShowPopup)

     def OnShowPopup(self,event):
         #wx.MessageBox("OnShowPopup")
         pos=event.GetPosition()
         #pos = self.ScreenToClient(pos)
         self.PopupMenu(self.popupmenu,pos)

     def OnPopupItemSelected(self,event) :
         item=self.popupmenu.FindItemById(event.GetId())
         text = item.GetText()
         wx.MessageBox("You Selected %s" % text)

Jerry

···

On May 23, 2007, at 8:44 PM, Robin Dunn wrote:

Jerry LeVan wrote:

Hi,
I am trying to use a context menu with a grid (without
much success).

I was reminded the other day that the grid class eats most mouse events, so the context menu event isn't getting any chance to be created. You can instead intercept the raw mouse events by binding your handlers to self.GetGridWindow() or you can use EVT_GRID_CELL_RIGHT_CLICK.

--
Robin Dunn