I have a selection widget part of which is a wx.ListBox that should accept a double click event for selection, equivalent to selecting and the clicking OK. What should I do in the double click handler to close the list box and return the equivalent of OK? Sample below, am I on the right path or is there a better way?
import wx
class SimpleSelectDialog(wx.Dialog):
def init(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_DIALOG_STYLE
super(SimpleSelectDialog, self).__init__(*args, **kwds)
choices=['aaaa','bbbb','cccc']
lb_style = wx.LB_SINGLE | wx.LB_NEEDED_SB
self.list_box = wx.ListBox(self, -1, style=lb_style,
size=(200,200), choices=choices)
self.list_box.Bind(wx.EVT_LISTBOX_DCLICK, self.item_picked)
ok_btn = wx.Button(self, wx.ID_OK, "")
cancel_btn = wx.Button(self, wx.ID_CANCEL, "")
ok_btn.SetDefault()
dialog_sizer = wx.BoxSizer(wx.VERTICAL)
dialog_sizer.Add(self.list_box, 0, 0, 0)
btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
btn_sizer.Add(ok_btn, 0, wx.ALL, 5)
btn_sizer.Add(cancel_btn, 0, wx.ALL, 5)
dialog_sizer.Add(btn_sizer, 1, wx.EXPAND|wx.ALL, 5)
self.SetSizerAndFit(dialog_sizer)
self.Layout()
def item_picked(self, event):
print 'item picked',self.list_box.GetStringSelection()
def get_selection(self):
sel = self.list_box.GetStringSelection()
return sel
class TestFrame(wx.Frame):
def init(self, *args, **kwds):
kwds[“style”] = wx.DEFAULT_FRAME_STYLE
wx.Frame.init(self, *args, **kwds)
panel = wx.Panel(self, wx.ID_ANY)
test_btn = wx.Button(panel, wx.ID_ANY, "Go")
self.Bind(wx.EVT_BUTTON, self.DoTest, test_btn)
self.SetSize((200, 200))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(test_btn)
panel.SetSizer(sizer)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel, 1, wx.EXPAND)
self.SetSizerAndFit(sizer)
self.Layout()
def DoTest(self, event):
dlg = SimpleSelectDialog(self, wx.ID_ANY, 'Pick One')
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_OK:
print 'OK: result="%s"' % dlg.get_selection()
elif result == wx.ID_CANCEL:
print 'Cancel'
else:
print 'Unknown', result
app = wx.PySimpleApp()
TestFrame(None, -1, “”).Show()
app.MainLoop()
···
–
Mike Conley