determining the cell that a user right-clicked in a wx grid

I want to popup a context menu for a right-click in a wx grid. The contents of the menu will depend on which cell the user right clicks. How do I determine the cell where the right-click occurred? The grid itself does not seem to have any selected cells when the event occurs.

For now, I just have this to bind the event:

self.params_grid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,
self.show_popup_menu)

and this little shell of an event handler:

def show_popup_menu(self, event):
    print('in show_popup_menu')
    pdb.set_trace()

Does the event instance contain any information on which cell was right clicked? The GetEventObject method seems to just return the wx.grid.Grid instance. If the information isn’t in the event and the grid doesn’t have any selected cells, am I out of luck?

Thanks,

Ryan

P.S. My full script is attached, but it won’t run without other modules of mine.

block_diagram_gui.py (11.5 KB)

Τη Τετάρτη, 25 Σεπτεμβρίου 2013 12:32:54 π.μ. UTC+3, ο χρήστης Ryan Krauss έγραψε:

I want to popup a context menu for a right-click in a wx grid. The contents of the menu will depend on which cell the user right clicks. How do I determine the cell where the right-click occurred? The grid itself does not seem to have any selected cells when the event occurs.

For now, I just have this to bind the event:

self.params_grid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,
self.show_popup_menu)

and this little shell of an event handler:

def show_popup_menu(self, event):
    print('in show_popup_menu')
    pdb.set_trace()

Does the event instance contain any information on which cell was right clicked? The GetEventObject method seems to just return the wx.grid.Grid instance. If the information isn’t in the event and the grid doesn’t have any selected cells, am I out of luck?

Thanks,

Ryan

P.S. My full script is attached, but it won’t run without other modules of mine.

Taken from one of the 9872364590827645 Robins answers and tips:

x, y = [YOUR GRID OBJECT].CalcUnscrolledPosition(event.GetX(),event.GetY())

coords = [YOUR GRID OBJECT].XYToCell(x, y)

col = coords[1]

row = coords[0]

Dont thank me , i just copied it…

Elias

That seems like the perfect answer, but doesn’t work. I put it in my code like this:

def show_popup_menu(self, event):
    print('in show_popup_menu')
    x, y = self.params_grid.CalcUnscrolledPosition(event.GetX(), \

                                                   event.GetY())
    coords = self.params_grid.XYToCell(x, y)
    col = coords[1]
    row = coords[0]
    print('col = %s' % col)

    print('row = %s' % row)

and on the event I get this traceback:

in show_popup_menu
Traceback (most recent call last):
File “block_diagram_gui.py”, line 161, in show_popup_menu

x, y = self.params_grid.CalcUnscrolledPosition(event.GetX(), \

AttributeError: ‘GridEvent’ object has no attribute ‘GetX’

So, it seems like a wx.grid.EVT_GRID_CELL_RIGHT_CLICK doesn’t have a GetX method…

···

On Tue, Sep 24, 2013 at 6:36 PM, Elias Alhanatis ealhanatis@gmail.com wrote:

Τη Τετάρτη, 25 Σεπτεμβρίου 2013 12:32:54 π.μ. UTC+3, ο χρήστης Ryan Krauss έγραψε:

I want to popup a context menu for a right-click in a wx grid. The contents of the menu will depend on which cell the user right clicks. How do I determine the cell where the right-click occurred? The grid itself does not seem to have any selected cells when the event occurs.

For now, I just have this to bind the event:

self.params_grid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,
self.show_popup_menu)

and this little shell of an event handler:

def show_popup_menu(self, event):
    print('in show_popup_menu')
    pdb.set_trace()

Does the event instance contain any information on which cell was right clicked? The GetEventObject method seems to just return the wx.grid.Grid instance. If the information isn’t in the event and the grid doesn’t have any selected cells, am I out of luck?

Thanks,

Ryan

P.S. My full script is attached, but it won’t run without other modules of mine.

Taken from one of the 9872364590827645 Robins answers and tips:

x, y = [YOUR GRID OBJECT].CalcUnscrolledPosition(event.GetX(),event.GetY())

coords = [YOUR GRID OBJECT].XYToCell(x, y)

col = coords[1]

row = coords[0]

Dont thank me , i just copied it…

Elias

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

My bad! The binder is missing:

[YOUR GRID OBJECT].GetGridWindow().Bind(wx.EVT_[WHATEVER], [HANDLER])

Place this after creating the grid. I think it will be fine…

I answerd a couple of times but i dont know where the message went , this is why i repost it now.

Elias

Τη Τετάρτη, 25 Σεπτεμβρίου 2013 5:04:33 π.μ. UTC+3, ο χρήστης Ryan Krauss έγραψε:

···

That seems like the perfect answer, but doesn’t work. I put it in my code like this:

def show_popup_menu(self, event):
    print('in show_popup_menu')
    x, y = self.params_grid.CalcUnscrolledPosition(event.GetX(), \

                                                   event.GetY())
    coords = self.params_grid.XYToCell(x, y)
    col = coords[1]
    row = coords[0]
    print('col = %s' % col)

    print('row = %s' % row)

and on the event I get this traceback:

in show_popup_menu
Traceback (most recent call last):
File “block_diagram_gui.py”, line 161, in show_popup_menu

x, y = self.params_grid.CalcUnscrolledPosition(event.GetX(), \

AttributeError: ‘GridEvent’ object has no attribute ‘GetX’

So, it seems like a wx.grid.EVT_GRID_CELL_RIGHT_CLICK doesn’t have a GetX method…

On Tue, Sep 24, 2013 at 6:36 PM, Elias Alhanatis ealha...@gmail.com wrote:

Τη Τετάρτη, 25 Σεπτεμβρίου 2013 12:32:54 π.μ. UTC+3, ο χρήστης Ryan Krauss έγραψε:

I want to popup a context menu for a right-click in a wx grid. The contents of the menu will depend on which cell the user right clicks. How do I determine the cell where the right-click occurred? The grid itself does not seem to have any selected cells when the event occurs.

For now, I just have this to bind the event:

self.params_grid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,
self.show_popup_menu)

and this little shell of an event handler:

def show_popup_menu(self, event):
    print('in show_popup_menu')
    pdb.set_trace()

Does the event instance contain any information on which cell was right clicked? The GetEventObject method seems to just return the wx.grid.Grid instance. If the information isn’t in the event and the grid doesn’t have any selected cells, am I out of luck?

Thanks,

Ryan

P.S. My full script is attached, but it won’t run without other modules of mine.

Taken from one of the 9872364590827645 Robins answers and tips:

x, y = [YOUR GRID OBJECT].CalcUnscrolledPosition(event.GetX(),event.GetY())

coords = [YOUR GRID OBJECT].XYToCell(x, y)

col = coords[1]

row = coords[0]

Dont thank me , i just copied it…

Elias

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

To add to what Elias said, I have attached an example app that demonstrates his solution.

  • Mike

grid_rclick.py (1.09 KB)

Thank you Mike and Elias. This is now working exactly like I would want it to.

Mike, I had originally grabbed my code off of your blog. Though I miss used it and caused my own problem, it would be great to put this short example (grid_rclick.py) on this page:

http://www.blog.pythonlibrary.org/2010/04/04/wxpython-grid-tips-and-tricks/

Your blog has been a great resource for me on wx.grid. Thanks for making what you have learned available to the rest of us.

Ryan

···

On Wed, Sep 25, 2013 at 8:23 AM, Mike Driscoll kyosohma@gmail.com wrote:

To add to what Elias said, I have attached an example app that demonstrates his solution.

  • Mike

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Glad you got it figured out. I went ahead and updated my blog article too. Hopefully someone else will find it useful in the future. Thank you for your readership!

Mike

···

On Wednesday, September 25, 2013 9:09:02 AM UTC-5, Ryan Krauss wrote:

Thank you Mike and Elias. This is now working exactly like I would want it to.

Mike, I had originally grabbed my code off of your blog. Though I miss used it and caused my own problem, it would be great to put this short example (grid_rclick.py) on this page:

http://www.blog.pythonlibrary.org/2010/04/04/wxpython-grid-tips-and-tricks/

Your blog has been a great resource for me on wx.grid. Thanks for making what you have learned available to the rest of us.

Ryan

Elias Alhanatis wrote:

Τη Τετάρτη, 25 Σεπτεμβρίου 2013 12:32:54 π.μ. UTC+3, ο χρήστης Ryan
Krauss έγραψε:

    I want to popup a context menu for a right-click in a wx grid. The
    contents of the menu will depend on which cell the user right
    clicks. How do I determine the cell where the right-click occurred?
    The grid itself does not seem to have any selected cells when the
    event occurs.

    For now, I just have this to bind the event:

    self.params_grid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,
    self.show_popup_menu)

    and this little shell of an event handler:

    def show_popup_menu(self, event):
    print('in show_popup_menu')
    pdb.set_trace()

    Does the event instance contain any information on which cell was
    right clicked? The GetEventObject method seems to just return the
    wx.grid.Grid instance. If the information isn't in the event and the
    grid doesn't have any selected cells, am I out of luck?

    Thanks,

    Ryan

    P.S. My full script is attached, but it won't run without other
    modules of mine.

Taken from one of the 9872364590827645 Robins answers and tips:

That's pretty amazing considering I've only been alive somewhere around 1486895020 seconds. :wink:

x, y = [YOUR GRID OBJECT].CalcUnscrolledPosition(event.GetX(),event.GetY())
coords = [YOUR GRID OBJECT].XYToCell(x, y)
col = coords[1]
row = coords[0]

Dont thank me , i just copied it.....

That approach is good if you need to translate from an arbitrary pixel position to a cell, but in this case EVT_GRID_CELL_RIGHT_CLICK will do all of that for you. The event object passed to the handler has GetRow and GetCol methods.

···

--
Robin Dunn
Software Craftsman

The event object passed to the handler has GetRow and GetCol methods.

That makes sense. I really did try and google for documentation on EVT_GRID_CELL_RIGHT_CLICK. I say that but googling “wxpython EVT_GRID_CELL_RIGHT_CLICK” lead me to these two links as results #1 and 3:

http://wiki.wxpython.org/wxGrid*Event
http://xoomer.virgilio.it/infinity77/wxPython/Events/wx.grid.GridEvent.html#GetCol

I feel silly now.

Thanks for treating my request kindly any ways…

···

On Wed, Sep 25, 2013 at 2:25 PM, Robin Dunn robin@alldunn.com wrote:

Elias Alhanatis wrote:

Τη Τετάρτη, 25 Σεπτεμβρίου 2013 12:32:54 π.μ. UTC+3, ο χρήστης Ryan

Krauss έγραψε:

I want to popup a context menu for a right-click in a wx grid. The

contents of the menu will depend on which cell the user right

clicks. How do I determine the cell where the right-click occurred?

The grid itself does not seem to have any selected cells when the

event occurs.



For now, I just have this to bind the event:



self.params_grid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,

self.show_popup_menu)



and this little shell of an event handler:



def show_popup_menu(self, event):

print('in show_popup_menu')

pdb.set_trace()





Does the event instance contain any information on which cell was

right clicked? The GetEventObject method seems to just return the

wx.grid.Grid instance. If the information isn't in the event and the

grid doesn't have any selected cells, am I out of luck?



Thanks,



Ryan



P.S. My full script is attached, but it won't run without other

modules of mine.

Taken from one of the 9872364590827645 Robins answers and tips:

That’s pretty amazing considering I’ve only been alive somewhere around 1486895020 seconds. :wink:

x, y = [YOUR GRID OBJECT].CalcUnscrolledPosition(event.GetX(),event.GetY())

coords = [YOUR GRID OBJECT].XYToCell(x, y)

col = coords[1]

row = coords[0]

Dont thank me , i just copied it…

That approach is good if you need to translate from an arbitrary pixel position to a cell, but in this case EVT_GRID_CELL_RIGHT_CLICK will do all of that for you. The event object passed to the handler has GetRow and GetCol methods.

Robin Dunn

Software Craftsman

http://wxPython.org

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Taken from one of the 9872364590827645 Robins answers and tips:

That’s pretty amazing considering I’ve only been alive somewhere around
1486895020 seconds. :wink:

Well you are the one with the time machine, you know!

x, y = [YOUR GRID OBJECT].CalcUnscrolledPosition(event.GetX(),event.GetY())

coords = [YOUR GRID OBJECT].XYToCell(x, y)

col = coords[1]

row = coords[0]

Dont thank me , i just copied it…

That approach is good if you need to translate from an arbitrary pixel
position to a cell, but in this case EVT_GRID_CELL_RIGHT_CLICK will do
all of that for you. The event object passed to the handler has GetRow
and GetCol methods.


Robin Dunn

Software Craftsman

http://wxPython.org

Oh…somehow I missed that. So I updated my update to my grid tips article. At least I got a little blogging in today. Thanks for the tip!

Mike