I’m new to wxPython, so please be gentle.
I’m attempting to make virtual list control that is controlled via a context menu popup when the user presses the right mouse button.
From my little bit of experience, it seems that the virtual list control likes to operate with “item” (read: ‘row’) and “column” numbers. Fair enough.
When I receive a right-click event, I can get the row (item) number easy enough by calling event.GetIndex(). But how do I get the column number of the object that was clicked?
import wx
import wx.lib.agw.ultimatelistctrl as ULC
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Right-click example")
self.list = MyListCtrl(parent=self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list, 1, wx.EXPAND)
self.SetSizer(sizer)
class MyListCtrl(ULC.UltimateListCtrl):
def __init__(self, parent, *args, **kwargs):
ULC.UltimateListCtrl.__init__(self, parent, 1, agwStyle=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES)
self.InsertColumn(0, "Column0")
self.InsertColumn(1, "Column1")
self.SetItemCount(5)
# Bindings
self.Bind(ULC.EVT_LIST_ITEM_RIGHT_CLICK, self.OnRightClick)
def OnGetItemText(self, item, column):
return "%d, %d" % (item, column)
def OnGetItemToolTip(self, item, column):
pass
def OnGetItemTextColour(self, item, column):
pass
def OnRightClick(self, event):
# Get the index (i.e. which row was clicked)
print("OnColRightClick: GetIndex = %r\n" %(event.GetIndex()))
# How can I get which column was clicked?
if __name__ == "__main__":
# Start the GUI
app = wx.App()
frame = MyFrame()
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
Sadly there is not a good way to get the column information. I did find this discussion on the matter though: http://wxpython-users.1045709.n5.nabble.com/Getting-row-col-of-selected-cell-in-ListCtrl-td2360831.html
It sounds like you’d have to calculate it yourself. According to Robin, there’s some code in wx.lib.mixins.listctrl.TextEditMixin that might help. I also found the recipe for ObjectListView (a wrapper for ListCtrl) that also might help: http://code.activestate.com/recipes/577543-objectlistview-getcolumnclickedevent-handler/