[Tutor] update a ws.ListBox
John Gerdeman
johnpatrick at gerdeman.de
Wed Nov 28 19:24:02 CET 2007
Hello,
I'm trying to create a gui using wxpython on ubuntu 6.10 (read, that
some functions of wx differ on different OS).
I have two Listbox items, Listbox1 and Listbox2. I want to update
Listbox2 according to the choice made in Listbox1.
I already looked at the wx API doc, but only found methods to insert
items or (de)select.
I know I can update a textctrl, but I need a way to make a choice based
on the first.
Here a Minimal example:
#!/usr/bin/python
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,
(550, 350))
self.listbox1_items = [ 'item1', 'item2']
self.listbox2_items = ['select a item from the right']
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
vbox = wx.BoxSizer(wx.VERTICAL)
panel = wx.Panel(self, -1)
self.listbox1 = wx.ListBox(panel, 1, wx.DefaultPosition, (170,
130), self.listbox1_items, wx.LB_SINGLE)
self.listbox1.SetSelection(0)
self.listbox2 = wx.ListBox(panel, 2, wx.DefaultPosition, (170,
130), self.listbox2_items, wx.LB_SINGLE)
self.listbox2.SetSelection(0)
hbox1.Add(self.listbox1, 0, wx.TOP, 40)
hbox1.Add(self.listbox2, 0, wx.TOP, 40)
vbox.Add(hbox1, 0, wx.ALIGN_CENTRE)
panel.SetSizer(vbox)
self.Bind(wx.EVT_LISTBOX, self.OnListBox1, id=1)
self.Bind(wx.EVT_LISTBOX, self.OnListBox2, id=2)
def OnListBox1(self, event):
index = event.GetSelection()
choice = self.listbox1_items[index]
print "You chose " + str(choice)
if choice == 'item1':
ret = ['choice1', 'choice2']
elif choice == 'item2':
ret = ['choice3', 'choice4']
else:
ret = ['Ooops']
print "Now I somehow need a way to pass to self.listbox2 I can set
self.listbox2_items to ret, but self.listbox2 doesn't get updated"
print ret
def OnListBox2(self, event):
return True
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'minexample.py')
frame.Centre()
frame.Show(True)
return True
app = MyApp(0)
app.MainLoop()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20071128/c2d0bf14/attachment.pgp
More information about the Tutor
mailing list