[Tutor] Query about using Pmw entry widget...

Alan Gauld alan.gauld at btinternet.com
Sun Jan 7 09:54:31 CET 2007


"Luke Paireepinart" <rabidpoobear at gmail.com> wrote

OP>> Can some one help me how to add validation : only integers are 
allowed
OP>> and minimum value should be 1.
OP>>
OP>> timeInterval = Pmw.EntryField(label_text='Time Interval(in
OP>> sec.):',labelpos='w',validate = 'numeric')

I don;t have PMW installed. I tend to use Tix now that its part
of standard Python. However Grayson says this:

"validation is [performed by a function which takes
as its first argument the entered text and returns
one of three standard values...
Pmw.OK, Pmw.ERROR, Pmw.PARTIAL"

So it seems you need

def numeric(val):
    try: float(val)
    except ValueError: return Pmw.ERROR
    else: return Pmw.OK

timeinterval = Pmw.EntryField(.....validate=numeric)

At least that's how I read it, obviously without Pmw I can't try it...

>> Also I want to validate date and time? I know that there is a way 
>> to
>> do it but I dont know the exact syntax..

I'd use a similar technique as above. parse the string and try
to create a datetime object then handle the exceptions.

Luke> it's a lot easier to validate the input when you're using it 
instead of
Luke> trying to restrict their input.

Its easier for the programmer but much worse for the user.
We should always catch erroneous input as early as possible.
Early CGI Web pages were the classic example of late error
handling and there is nothing more annoying than filling in a form,
submitting it and then being told you filled in a field wrong right
at the start!

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list