I have a listbox and what I basically want to achieve is as I click a value within the listbox I want the value clicked to be passed to a function.

I could easily do this using a button and listbox.GetValue() but I believe the quality of my finished code would benefit by having the listbox do this for me.

The listbox obviously knows its being clicked as it highlights the value in blue. I would like it to send this value to a function, or just call a function similar to the one below.

import wx

class Trial(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Trial', size = (300, 300))

        self.panel = wx.Panel(self)

        maincats = wx.StaticText(self.panel, label = "Main Catagory")

        self.mainselect = wx.ListBox(self.panel, style = wx.LB_SINGLE)

        y = (1,2,3,4,5,6,7,8)

        for x in y:

            self.mainselect.Insert(str(x), 0)

        self.grid = wx.GridSizer(2,2,10,5)
        self.grid.Add(maincats)
        self.grid.Add(self.mainselect)

        self.panel.SetSizer(self.grid)

    def OnListBoxClick(self, event):

        value = self.mainselect.GetValue()
        print value

app = wx.App(redirect = False)
frame = Trial(parent = None, id = -1)
frame.Show()
app.MainLoop()

I could easily call this function with a button, but I believe having the listbox call this function when a value is selected would be a better option.

Can anybody point me in the right direction here?

The parameters for the insert method are as follows.

Insert(self, item, pos, clientData=None)

"Insert an item into the control before the item at the pos index, optionally associating some data object with the item."

Can a data object be a function?

Thanks in advance.

Currently looking into the Bind method the same way I would Bind a button.

Bind it to the event

EVT_LISTBOX
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.