I personally used a listener that check every input from keyboard and enable it only if it's a numeric value or the "." of a floating point value.
This is the code you can build up from it (:
Usage:
self.__MyTxtCtrl.Bind(wx.EVT_CHAR, self.__on_change_text_check_is_int_value)
def __on_change_text_check_is_int_value(self, evt):
KeyboardEventUtils.on_change_text_check_is_int_value(self, evt)
Event handler in class KeyboardEventUtils.py:
from pyglet.window import key
FLOAT_POINT = "."
ENTRY_ID = "id"
ENTRY_KEY_EVENT = "keyEvent"
ENTRY_SELECTION_EVENT = "selectionEvent"
MASK_STARTED_RIGHT_SELECTION = 1
MASK_STARTED_LEFT_SELECTION = -1
class KeyboardEventUtils(object):
__mLastEvt = {}
def check_input_key_only_numeric_value(value, strng):
return value.isnumeric() or (value == FLOAT_POINT and not FLOAT_POINT in strng and len(strng) > 0)
def on_change_text_check_is_int_value(self, evt):
txt = evt.GetEventObject()
value = chr(evt.GetUnicodeKey())
if KeyboardEventUtils.__mLastEvt != None and len(KeyboardEventUtils.__mLastEvt) > 0 and KeyboardEventUtils.__mLastEvt[ENTRY_ID] != txt.GetId():
self._mLastEvt = {}
if check_input_key_only_numeric_value(value, txt.GetValue()):
pos = txt.GetInsertionPoint()
txt.SetValue(txt.GetValue()[:pos] + value + txt.GetValue()[pos:])
txt.SetInsertionPoint(pos + 1)
elif (evt.GetModifiers() == 4 or evt.GetModifiers() == key.MOD_SHIFT) and evt.GetRawKeyCode() == key.LEFT: # 4 (?) key.MOD_SHIFT (?) TO FIX
pos = txt.GetInsertionPoint()
rng = list(txt.GetSelection())
if rng[0] == rng[1] and ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt:
del KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT]
if ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt and KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] == MASK_STARTED_RIGHT_SELECTION:
rng[1] = rng[1] - 1
else:
KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] = MASK_STARTED_LEFT_SELECTION
rng[0] = rng[0] - 1
txt.SetSelection(rng[0], rng[1])
elif (evt.GetModifiers() == 4 or evt.GetModifiers() == key.MOD_SHIFT): # 4 (?) key.MOD_SHIFT (?) TO FIX
pos = txt.GetInsertionPoint()
rng = list(txt.GetSelection())
if rng[0] == rng[1] and ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt:
del KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT]
if ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt and KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] == MASK_STARTED_LEFT_SELECTION:
rng[0] = rng[0] + 1
else:
KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] = MASK_STARTED_RIGHT_SELECTION
rng[1] = rng[1] + 1
txt.SetSelection(rng[0], rng[1])
elif evt.GetRawKeyCode() == key.LEFT:
rng = list(txt.GetSelection())
if rng[0] != rng[1]:
txt.SetInsertionPoint(rng[0])
else:
txt.SetInsertionPoint(txt.GetInsertionPoint() - 1)
elif evt.GetRawKeyCode() == key.RIGHT:
rng = list(txt.GetSelection())
if rng[0] != rng[1]:
txt.SetInsertionPoint(rng[1])
else:
txt.SetInsertionPoint(txt.GetInsertionPoint() + 1)
elif evt.GetRawKeyCode() == key.UP or evt.GetRawKeyCode() == key.DOWN:
pass
elif evt.GetRawKeyCode() == key.BACKSPACE:
pos = txt.GetInsertionPoint()
if txt.GetStringSelection() == "":
txt.SetValue(txt.GetValue()[:pos-1] + txt.GetValue()[pos:])
txt.SetInsertionPoint(pos - 0x1)
else:
r = txt.GetSelection()
txt.SetValue(txt.GetValue()[:r[0]] + txt.GetValue()[r[1]:])
txt.SetInsertionPoint(pos)
elif evt.GetRawKeyCode() == key.DELETE:
pos = txt.GetInsertionPoint()
if txt.GetStringSelection() == "":
txt.SetValue(txt.GetValue()[:pos] + txt.GetValue()[pos+1:])
else:
r = txt.GetSelection()
txt.SetValue(txt.GetValue()[:r[0]] + txt.GetValue()[r[1]:])
txt.SetInsertionPoint(pos)
elif evt.GetModifiers() == key.MOD_CTRL and evt.GetRawKeyCode() == key.A:
txt.SelectAll()
else:
KeyboardEventUtils.__mLastEvt[ENTRY_ID] = txt.GetId()
KeyboardEventUtils.__mLastEvt[ENTRY_KEY_EVENT] = evt
return False
KeyboardEventUtils.__mLastEvt[ENTRY_ID] = txt.GetId()
KeyboardEventUtils.__mLastEvt[ENTRY_KEY_EVENT] = evt
return True
Hi you can use this listener here to bind your TxtCtrl.
This will check only for numeric values and the "." value for floating point values.
It will also enable selection and deselection and other keyboard inputs on your TxtCtrl.
Just Do:
self.__MyTxtCtrl.Bind(wx.EVT_CHAR, self.__on_change_text_check_is_int_value)
def __on_change_text_check_is_int_value(self, evt):
KeyboardEventUtils.on_change_text_check_is_int_value(self, evt)
And use this class:
from pyglet.window import key
FLOAT_POINT = "."
ENTRY_ID = "id"
ENTRY_KEY_EVENT = "keyEvent"
ENTRY_SELECTION_EVENT = "selectionEvent"
MASK_STARTED_RIGHT_SELECTION = 1
MASK_STARTED_LEFT_SELECTION = -1
class KeyboardEventUtils(object):
__mLastEvt = {}
def check_input_key_only_numeric_value(value, strng):
return value.isnumeric() or (value == FLOAT_POINT and not FLOAT_POINT in strng and len(strng) > 0)
def on_change_text_check_is_int_value(self, evt):
txt = evt.GetEventObject()
value = chr(evt.GetUnicodeKey())
if KeyboardEventUtils.__mLastEvt != None and len(KeyboardEventUtils.__mLastEvt) > 0 and KeyboardEventUtils.__mLastEvt[ENTRY_ID] != txt.GetId():
self._mLastEvt = {}
if check_input_key_only_numeric_value(value, txt.GetValue()):
pos = txt.GetInsertionPoint()
txt.SetValue(txt.GetValue()[:pos] + value + txt.GetValue()[pos:])
txt.SetInsertionPoint(pos + 1)
elif (evt.GetModifiers() == 4 or evt.GetModifiers() == key.MOD_SHIFT) and evt.GetRawKeyCode() == key.LEFT: # 4 (?) key.MOD_SHIFT (?) TO FIX
pos = txt.GetInsertionPoint()
rng = list(txt.GetSelection())
if rng[0] == rng[1] and ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt:
del KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT]
if ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt and KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] == MASK_STARTED_RIGHT_SELECTION:
rng[1] = rng[1] - 1
else:
KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] = MASK_STARTED_LEFT_SELECTION
rng[0] = rng[0] - 1
txt.SetSelection(rng[0], rng[1])
elif (evt.GetModifiers() == 4 or evt.GetModifiers() == key.MOD_SHIFT): # 4 (?) key.MOD_SHIFT (?) TO FIX
pos = txt.GetInsertionPoint()
rng = list(txt.GetSelection())
if rng[0] == rng[1] and ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt:
del KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT]
if ENTRY_SELECTION_EVENT in KeyboardEventUtils.__mLastEvt and KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] == MASK_STARTED_LEFT_SELECTION:
rng[0] = rng[0] + 1
else:
KeyboardEventUtils.__mLastEvt[ENTRY_SELECTION_EVENT] = MASK_STARTED_RIGHT_SELECTION
rng[1] = rng[1] + 1
txt.SetSelection(rng[0], rng[1])
elif evt.GetRawKeyCode() == key.LEFT:
rng = list(txt.GetSelection())
if rng[0] != rng[1]:
txt.SetInsertionPoint(rng[0])
else:
txt.SetInsertionPoint(txt.GetInsertionPoint() - 1)
elif evt.GetRawKeyCode() == key.RIGHT:
rng = list(txt.GetSelection())
if rng[0] != rng[1]:
txt.SetInsertionPoint(rng[1])
else:
txt.SetInsertionPoint(txt.GetInsertionPoint() + 1)
elif evt.GetRawKeyCode() == key.UP or evt.GetRawKeyCode() == key.DOWN:
pass
elif evt.GetRawKeyCode() == key.BACKSPACE:
pos = txt.GetInsertionPoint()
if txt.GetStringSelection() == "":
txt.SetValue(txt.GetValue()[:pos-1] + txt.GetValue()[pos:])
txt.SetInsertionPoint(pos - 0x1)
else:
r = txt.GetSelection()
txt.SetValue(txt.GetValue()[:r[0]] + txt.GetValue()[r[1]:])
txt.SetInsertionPoint(pos)
elif evt.GetRawKeyCode() == key.DELETE:
pos = txt.GetInsertionPoint()
if txt.GetStringSelection() == "":
txt.SetValue(txt.GetValue()[:pos] + txt.GetValue()[pos+1:])
else:
r = txt.GetSelection()
txt.SetValue(txt.GetValue()[:r[0]] + txt.GetValue()[r[1]:])
txt.SetInsertionPoint(pos)
elif evt.GetModifiers() == key.MOD_CTRL and evt.GetRawKeyCode() == key.A:
txt.SelectAll()
else:
KeyboardEventUtils.__mLastEvt[ENTRY_ID] = txt.GetId()
KeyboardEventUtils.__mLastEvt[ENTRY_KEY_EVENT] = evt
return False
KeyboardEventUtils.__mLastEvt[ENTRY_ID] = txt.GetId()
KeyboardEventUtils.__mLastEvt[ENTRY_KEY_EVENT] = evt
return True