3

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?

2
  • See the answers to this question stackoverflow.com/questions/6531207/wxpython-listctrl-help Commented 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. Commented Jul 27, 2011 at 14:18

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.