Hi folks,
from the PythonCard List, Ray Allen 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!
Hello, I have tried to implement a
"""
I tried to implement such a class (with the option, displaying the currently
typed in letters (the buffers) or not.
Any suggestions for improving the code or the design in common are highly welcome.
Also for bad coding.
thank you in advance!
#======= Begin Code
#!/usr/bin/env python
# Franz Steinhaeusler, 04.05.2006
# ChoiceAutoFind.py
import wx
class ChoiceAutoFind(wx.Choice):
"""ChoiceAutoFind: it will search for elements which fits a whole typed in word fragment."""
def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, choices=[], style=0, showbuffer=False):
"""Create the ChoiceAutoFind Control."""
wx.Choice.__init__(self, parent, id, pos, size, choices, style)
self.keybuffer = ""
self.showbuffer = showbuffer
if showbuffer:
# create a static text to show current keybuffer.
x, y = self.GetPosition()
w, h = self.GetSize()
self.StaticText = wx.StaticText(parent, pos=(x+w+5, y+3))
self.StaticText.SetBackgroundColour(wx.Colour(255, 255, 192))
self.Bind(wx.EVT_CHAR, self.EvtChar)
self.Bind(wx.EVT_SET_FOCUS, self.OnFocus)
#self.Bind(wx.EVT_CHOICE, self.EvtChoice)
def OnFocus(self, event):
"""ChoiceAutoFind gets the focus. => Clear the buffer."""
self.keybuffer = ""
self.ShowBuffer()
event.Skip()
def EvtChar(self, event):
"""Process current typed in key."""
if event.GetKeyCode() > 255 or event.GetKeyCode() == wx.WXK_RETURN:
# example: cursor keys should work as expected.
# and return should not fill the keybuffer list.
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 with Escape key.
self.keybuffer = ""
else:
# extend buffer with current pressed key.
self.keybuffer += chr (event.GetKeyCode())
self.ShowBuffer()
# try to find in the list
for i in range(self.GetCount()):
if self.GetString(i).find(self.keybuffer) == 0:
self.SetSelection(i)
return
#def EvtChoice(self, event):
# """This could be skipped."""
# print '1. EvtChoice: %s\n' % event.GetString()
# event.Skip()
def ShowBuffer(self):
"""Display Key Buffer."""
if self.showbuffer:
if self.keybuffer == "":
self.StaticText.SetLabel("")
else:
self.StaticText.SetLabel("'" + self.keybuffer + "'")
class TestChoiceAutoFindPanel(wx.Panel):
"""Test Panel for ChoiceAutoFind."""
def __init__(self, parent, showbuffer=False):
"""Create the TestChoiceAutoFind Panel."""
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 = ChoiceAutoFind(self, -1, (100, 50), choices=sampleList, showbuffer=showbuffer)
self.ch.Bind(wx.EVT_CHOICE, self.EvtChoice)
def EvtChoice(self, event):
"""Display currently selected entry.."""
#print '2. EvtChoice: %s\n' % event.GetString()
event.Skip()
if __name__ == "__main__":
app = wx.App(0)
frame = wx.Frame(None)
#TestChoiceAutoFindPanel(frame)
TestChoiceAutoFindPanel(frame, showbuffer=True)
frame.Show()
app.MainLoop()
#======= End Code
···
--
Franz Steinhaeusler