[Tutor] Validity check for different data types.......................

Alan Gauld alan.gauld@blueyonder.co.uk
Thu May 22 05:14:02 2003


> I expect validity checks  for "KeyRelease" event for all entry 
> widgets. Now the problem is "do i need to have 10 validity check 
> routines one for each entry widget? or  having 3 routines 
> (one for string, one for int,one for float) is enough?".    


3 should suffice. You can call the routines from your entry 
handlers. 

<PSEUDOCODE ALERT!>

def isString(s): return type(s) == type('')
def isInteger(i): return type(i) == type(7)
def isFloat(f): return type(f) == type(7.0)

Entries = {}
for e in range(10):
   Entries[e] = Entry(.....)
   if e % 3 == 0:
      bind(<KeyRelease>, Entries[e], lambda e: isString(e.value))  
   elif e % 2 == 0:
      bind(<KeyRelease>, Entries[e], lambda e: isInteger.value())  
   else:
      bind(<KeyRelease>, Entries[e], lambda e: isFloat(e.value())  

Creates 10 entries with some checking integers, some strings etc.
You probably want to control which entry does what rather more 
methodically than that but you get the idea...

Your type checking might want to be a tad more sophisticated too!

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld