On Thu, 4 May 2006 09:40:33 +0100, "Ray Allen" <ray...@sa...> wrote:
>Is there a way to configure choice boxes so they autofill using more than
>the first letter?
>eg. List of countries available from choice box. If I want to select
>'United Kingdom', have to click U five times (after passing a number of
>other countries beginning with U) to get there. I would like to type 'un'
>and end up with countries beginning with 'un'. Currently, this would result
>in me seeing countries beginning in N!
>Thanks in advance,
>Ray Allen
>
>
I tried to implement this.
Here is the first prototype:
==================================================================
#!/usr/bin/env python
# TestChoiceAutoFind.py
#todo: make own ChoiceAutoFind class derived from Choice (also parent, pos, size, choices, styles, ...)
#todo: provide a better representation of the buffer. (A pop up or a tooltip beside the
# right edge of the choice).
#todo: make option in constructor, wether to show buffer or not.
#todo: docstrings, comments
#todo: shold alt-cursor down (open the choice list) delete the buffer?
import wx
class TestChoiceAutoFind(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
sampleList = ['zero', 'one', 'two', 'three', 'thirteen', 'twenty']
wx.StaticText(self, -1, "Choice Auto Find", (15, 10))
wx.StaticText(self, -1, "Select one:", (15, 50), (75, -1))
self.ch = wx.Choice(self, -1, (100, 50), choices = sampleList)
self.keybuffer = ""
self.ch.Bind(wx.EVT_CHAR, self.EvtChar)
self.ch.Bind(wx.EVT_SET_FOCUS, self.OnFocus)
self.ch.Bind(wx.EVT_CHOICE, self.EvtChoice)
def OnFocus(self, event):
#delete buffer
self.keybuffer = ""
self.ShowBuffer()
event.Skip()
def EvtChar(self, event):
if event.GetKeyCode() > 255:
#example: cursor keys should work as expected
event.Skip()
return
if event.GetKeyCode() == wx.WXK_BACK:
#delete last key in buffer
if len (self.keybuffer) > 0:
self.keybuffer = self.keybuffer[:-1]
elif event.GetKeyCode() == wx.WXK_ESCAPE:
#delete buffer
self.keybuffer = ""
else:
#extend buffer with current key
self.keybuffer += chr (event.GetKeyCode())
self.ShowBuffer()
#try to find in the list
for i in range(self.ch.GetCount()):
if self.ch.GetString(i).find(self.keybuffer) == 0:
self.ch.SetSelection(i)
return
def EvtChoice(self, event):
print 'EvtChoice: %s\n' % event.GetString()
def ShowBuffer(self):
self.GetParent().SetStatusText("'"+self.keybuffer+"'",0)
#show as tooltip beside combobox would be cool; or small popup window
app = wx.App(0)
f = wx.Frame(None)
f.CreateStatusBar()
p = TestChoiceAutoFind(f)
f.Show()
app.MainLoop()
==================================================================
I want to continue the discussion on the wxPython user mailing list.
--
Franz Steinhaeusler
|