I have a class which is subclassed from wxTextCtrl which simply accept
integers and reformats the input upon exit. The problem with this code
is that the user couldn't use EVT_KILL_FOCUS with this class. Can
someone figure out the problem?
Version: wxPython 2.2.1-0 / Redhat 7.3
Thanks,
Thomas.
class IntegerCtrl(wxTextCtrl):
def __init__(self, parent, id, str, position, size,
style=wxTE_PROCESS_ENTER):
wxTextCtrl.__init__(self, parent, id, str, position, size, style)
self.Connect(-1, -1, wxEVT_CHAR, self.TextChange)
self.Connect(-1, -1, wxEVT_KILL_FOCUS, self.OnKillFocus)
self.Connect(-1, -1, wxEVT_SET_FOCUS, self.OnSetFocus)
def TextChange(self, event):
kc = event.KeyCode()
if (48 <= kc <= 57) or kc == 9 or kc == 8 or \
kc == 127 or (314 <= kc <= 319) or kc == 45:
event.Skip()
elif kc == 13: # ENTER key
event.Skip()
def OnSetFocus(self, event):
txt = string.strip(self.GetValue())
self.SetValue(txt)
self.SetInsertionPoint(0)
event.Skip()
def OnKillFocus(self, event):
txt = self.GetValue()
try:
sp = float(txt)
except ValueError:
sp = 0.00
num = "%9d" % sp
self.SetValue(num)
event.Skip()