[wxPython] how to control length(validator) of text

Hi,

How can i limit the test in wxtextctrl to specific value like only 10 character.I check to wxPython documention but didn’t get any idea.

i’m also want to make auto writing to txtCtrl like phone number where when the user type in first 2 digit and he/she will get “-” character next of the second character and then continue to write other digit rather then to write all value something like this “04-83788278”

izam

Irwan Azam wrote:

Hi,
How can i limit the test in wxtextctrl to specific value like only 10
character.I check to wxPython documention but didn't get any idea.

Here's the OnChar handler from a subClass of wxTextCtrl. It limits input
to 2 or 3 characters and does some other fancy stuff. In the case of the
CW_YARDAGE, the input can be -0 to -49, 50, or +0 to +49, or for
CW_PLAYER, 1 or 2 characters, numeric only. Hope it helps.

  def OnChar(self, event):
    key = event.KeyCode()
          #it was some edit key
    #it is possible to delete the - or + then tab out, the validate
    #routines in edit panel won't let it escape...
    if key < WXK_SPACE or key == WXK_DELETE or key > 255:
      event.Skip()
      return

    char = chr(key)
    caret = self.GetInsertionPoint()
    contents = self.GetValue()
    text = contents[:caret] + char + contents[caret:]

    if (self.type == CW_YARDAGE):
      if (self.validateYardEntry(text)):
        event.Skip()
        return
    else:
      if (self.validatePlayerEntry(text)):
        event.Skip()
        return
    
    if not wxValidator_IsSilent(): wxBell()
      
    # Returning without calling event.Skip eats the event before it
    # gets to the text control
    return

  def validateYardEntry(self, y):
    sl = len(y)
    if sl :
      fc = y[0]
      if (fc == "+") or (fc == "-") :
        if (sl == 1) : return 1
        elif (sl == 2) :
          if y[1] in "0123456789" : return 1
          else : return 0
        elif (sl == 3) :
          if (y[1] in "1234") and (y[2] in "0123456789") :
            return 1
          else : return 0
        elif (sl == 4) : return 0
      elif (fc == "5") :
        if (sl == 1) : return 1
        elif ((sl == 2) and (y[1] == "0")) : return 1
        else : return 0
      else : return 0
    else : return 1

  def validatePlayerEntry(self, y):
    sl = len(y)
    valid = 1
    if sl > 2: return 0
    
    for i in range(sl):
      if y[i] not in "0123456789": valid = 0
    return valid

···

--
"I ain't no stewin' rabbit, I'm a fricassein' rabbit. Have you got a
fricassein' rabbit license?"

Daryl Stultz - python, blender, robots, really bad harmonica playing...
daryl@together.net
RedHat Linux 6.1 - Dual Pentium Pro