How to insert into listbox using wxPython

Brian Kelley bkelley at wi.mit.edu
Thu Jan 8 14:03:12 EST 2004


Andrew wrote:

> Hi I just started learning wxPython
> 
> I wanted to know how I could do this in wxPython
> 
> 
> self.listbox.delete(0, END)
>         for item in self.results:
>             self.listbox.insert(END, item)
> 
> 
> I don't know but I think the insert and delete things here are specific of
> Tkinter which I have been studying for the last little while
> 

# make a list box with default choices
box = wxListBox(self, -1, choices=['a','b','c'])
# append a new entry
box.Append('d')
# reset the listbox to something else
box.Set(['1','2','3'])

# delete the second entry selection
box.Delete(1)

# get the index of currently selected entry
print box.GetSelection()

# get the string that is selected
print box.GetStringSelection()

# make a callback for when an entry is selected
def listboxhandler(evt):
     box = evt.GetEventObject()
     print box.GetSelection(), "selected index"
     print box.GetStringSelection(), "selected item"

EVT_LISTBOX(box, box.GetId(), listboxhandler)

full little program
###########################################################
from wxPython.wx import *

class T(wxFrame):
     def __init__(self, parent, id, title):
         print "calling init"
         wxFrame.__init__(self, parent, id, title)
         self.mainsizer = self.sizer = wxBoxSizer(wxVERTICAL)
         # make a list box with default choices
         box = wxListBox(self, -1, choices=['a','b','c'])
         # append a new entry
         box.Append('d')
         # reset the listbox to something else
         box.Set(['1','2','3'])
         box.Delete(0)
         print box.GetSelections()
         self.sizer.Add(box, 0, wxEXPAND)
         self.SetSizer(self.sizer)
         def listboxhandler(evt):
             box = evt.GetEventObject()
             print box.GetSelection(), "selected index"
             print box.GetStringSelection(), "selected item"

         EVT_LISTBOX(box, box.GetId(), listboxhandler)

app = wxPySimpleApp()
foo = T(None, -1, "hello")
print foo
foo.Show()
app.MainLoop()


> Anyhelp would be cool
> 
> 




More information about the Python-list mailing list