[Tutor] Checking Entry Values in Tkinter Widgets

Alan Gauld alan.gauld at btinternet.com
Wed Mar 18 02:20:14 CET 2009


"Wayne Watson" <sierra_mtnview at sbcglobal.net> wrote

> Yes, apply is invoked when I click OK. apply has one statement,
> and returns to the caller, which then checks the validity 
> (try-except)
> of values passed back to it. It seems like apply should do all the
> try-except work instead.

Yes, I'd agree that is how it should work.

> What I really need are checks like is lat (latitude) from -90 to 
> +90,
> and not -700, say. That is bound checking.

Yes, that can be done with binding of validation code to the lat 
field.
You could also use the FocusOut event for example to run something
like:

# in the create widget section....
self.lat.bind('<FocusIn>', self.vLat)

# validation handler
def vLat(self, ev):
     if 90 < int(self.lat.get()) < -90:
         # beep, or error dialog
         self.lat.focus_set()

Which would generate an alert and reset focus to the widget before
even hitting apply().

HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list