So, I have a wxPython ListCtrl which contains rows of data. How can I make an event that calls a function, with the row contents, when one of the rows if clicked on?
-
See the answers to this question stackoverflow.com/questions/6531207/wxpython-listctrl-helpStephen Terry– Stephen Terry2011-07-26 15:03:53 +00:00Commented Jul 26, 2011 at 15:03
-
@Stephen-Terry Thanks, but that doesn't answer my question entirely; how can I call the function in the first place? Those replies are about how to get the information from the event, once the function has been called.user825286– user8252862011-07-27 14:18:21 +00:00Commented Jul 27, 2011 at 14:18
Add a comment
|
1 Answer
You can use the Bind function to bind a method to an event. For example,
import wx
class MainWidget(wx.Frame):
def __init__(self, parent, title):
super(MainWidget, self).__init__(parent, title=title)
self.list = wx.ListCtrl(parent=self)
for i,j in enumerate('abcdef'):
self.list.InsertStringItem(i,j)
self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnClick, self.list)
self.Layout()
def OnClick(self, event):
print event.GetText()
if __name__ == '__main__':
app = wx.App(redirect=False)
frame = MainWidget(None, "ListCtrl Test")
frame.Show(True)
app.MainLoop()
This app will print the item in the ListCtrl that is activated (by pressing enter or double-clicking). If you just want to catch a single click event, you could use wx.EVT_LIST_ITEM_SELECTED.
The important point is that the Bind function specifies the method to be called when a particular event happens. See the section in the wxPython Getting Started guide on event handling. Also see the docs on ListCtrl for the events that widget uses.