Validator help - 3 hex digits

I have a validator to allow between 0-3 hex digitals to be entered in
a TextCtrl.

This works as far as allowing only hexdigits, but I can't seem to get
the 3 digit limit to work.

The "Validate" function within the validate class does not seem ever
to get called.

Here is the full code listing. Much of it was taken from the demo.

#!/usr/bin/python

import wx
import string

VALID_HEX = 1
ALPHA_ONLY = 2
DIGIT_ONLY = 7

class MyValidator(wx.PyValidator):
  def __init__(self, flag=None, pyVar=None):
    wx.PyValidator.__init__(self)
    self.flag = flag
    self.Bind(wx.EVT_CHAR, self.OnChar)
    self.count = 0

  def Clone(self):
    return MyValidator(self.flag)

  def Validate(self, win):
    print "in val"
    tc = self.GetWindow()
    val = tc.GetValue()

    if self.flag == ALPHA_ONLY:
      for x in val:
        if x not in string.letters:
          return False

    elif self.flag == DIGIT_ONLY:
      for x in val:
        if x not in string.hexdigits:
          return False

    elif self.flag == VALID_HEX:
      if len(val) > 3:
        return False
      return True

    return True

  def OnChar(self, event):
    key = event.GetKeyCode()

    if key == wx.WXK_BACK:
      print "Backspace, count is %d" % self.count
      if self.count > 0:
        print "decrement"
        self.count -= 1
      event.Skip()
      return

    if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
      print "other"
      event.Skip()
      return

    if self.flag == ALPHA_ONLY and chr(key) in string.letters:
      event.Skip()
      return

    if self.flag == VALID_HEX and chr(key) in string.hexdigits:
      event.Skip()
      return

    if self.flag == DIGIT_ONLY and chr(key) in string.hexdigits:
      event.Skip()
      return

    if not wx.Validator_IsSilent():
      wx.Bell()

    # Returning without calling even.Skip eats the event before it
    # gets to the text control
    return

class TestValidate(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,title='Test Hex Validator')
        p = wx.Panel(self)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        tc = wx.TextCtrl(p,-1,"",size=(65,25),validator =
MyValidator(VALID_HEX))
        sizer.Add(tc,0,wx.ALL,5)
        p.SetSizer(sizer)
        sizer.Fit(p)

if __name__ == '__main__':
     app=wx.App()
     frame=TestValidate()
     frame.Show()
     app.MainLoop()

Thanks to a great help list.

···

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

You need to call Validate, i.e. have a button or similar.

See attached, note that I call validate on the panel, it should be possible to call it on the frame, but can't make it to work. I run into this problem before.

Maybe Robin or someone else can point out what needs to be done to validate the window/frame.

Werner

dubiboy.py (2.68 KB)

···

On 24/05/2010 04:58, dubiboy wrote:

I have a validator to allow between 0-3 hex digitals to be entered in
a TextCtrl.

This works as far as allowing only hexdigits, but I can't seem to get
the 3 digit limit to work.

The "Validate" function within the validate class does not seem ever
to get called.

Here is the full code listing. Much of it was taken from the demo.
   

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Thanks Werner but this is NOT what I need.

I need:

1. validates on every key press (no explicit button)
2. if character is neither hex digit nor backspace then it is blocked,
3. if the character would make string in TextCtrl longer than 3 digits
then also blocked.

Dubiboy

···

On May 24, 11:56 am, werner <wbru...@free.fr> wrote:

On 24/05/2010 04:58, dubiboy wrote:> I have a validator to allow between 0-3 hex digitals to be entered in
> a TextCtrl.

> This works as far as allowing only hexdigits, but I can't seem to get
> the 3 digit limit to work.

> The "Validate" function within the validate class does not seem ever
> to get called.

> Here is the full code listing. Much of it was taken from the demo.

You need to call Validate, i.e. have a button or similar.

See attached, note that I call validate on the panel, it should be
possible to call it on the frame, but can't make it to work. I run into
this problem before.

Maybe Robin or someone else can point out what needs to be done to
validate the window/frame.

Werner

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visithttp://groups.google.com/group/wxPython-users?hl=en

dubiboy.py
2KViewDownload

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Key presses will be processed by your OnChar handler. In there simply
call GetValue on your text control. i.e val =
self.GetWindow().GetValue(), and check that its length is less than 3.
If not don't skip the event.

The Validate function is used if the window the Validator belongs to
is a Dialog. When the Dialog is being closed it will call Validate on
its children to see if the dialog can be closed or not.

Cody

···

On Mon, May 24, 2010 at 5:23 AM, dubiboy <david.wende@gmail.com> wrote:

Thanks Werner but this is NOT what I need.

I need:

1. validates on every key press (no explicit button)
2. if character is neither hex digit nor backspace then it is blocked,
3. if the character would make string in TextCtrl longer than 3 digits
then also blocked.

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Cody - this works fine.
Thanks
Dubiboy

···

On May 24, 3:05 pm, Cody Precord <codyprec...@gmail.com> wrote:

On Mon, May 24, 2010 at 5:23 AM,dubiboy<david.we...@gmail.com> wrote:
> Thanks Werner but this is NOT what I need.

> I need:

> 1. validates on every key press (no explicit button)
> 2. if character is neither hex digit nor backspace then it is blocked,
> 3. if the character would make string in TextCtrl longer than 3 digits
> then also blocked.

Key presses will be processed by your OnChar handler. In there simply
call GetValue on your text control. i.e val =
self.GetWindow().GetValue(), and check that its length is less than 3.
If not don't skip the event.

The Validate function is used if the window the Validator belongs to
is a Dialog. When the Dialog is being closed it will call Validate on
its children to see if the dialog can be closed or not.

Cody

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visithttp://groups.google.com/group/wxPython-users?hl=en