data entry validation with Tkinter

xtian at hyperactive.co.nz xtian at hyperactive.co.nz
Tue Aug 7 20:22:27 EDT 2001


On 07 Aug 2001 11:03:26 +0100, Ken Guest <kwg at renre-europe.com> wrote:

>
>attempts at specifying python procedures for vcmd and invcmd only seem
>to be called when the app in initialising and don't exactly work.
>For example, with
>
>self.__entry = Entry(newframe, validate='key', vcmd=self.validate('%P'),
>invcmd=self.invalid())
>
>the validate and invalid methods are called when the app starts up, but
>neither of those methods are called when any keys are pressed, and I'm
>really not sure that the second definition above is syntatically
>correct, even though the parser didn't catch any problems.

The fact that they're called when you create the entry sounds like you
should be passing callbacks (which should be callable objects) to
those parameters. Try the following: 

self.__entry = Entry(newframe, validate='key', 
			vcmd=lambda: self.validate('%P'),
			invcmd=self.invalid)

(Or the equivalent named function object instead of the lambda.) 

elsewhere in the class...
def val(self):
    self.validate('%P')

self.__entry = Entry(newframe, validate='key', 
			vcmd=self.val,
			invcmd=self.invalid)

Passing function objects (rather than the result of the function call)
to the constructor means that the Entry can call them later, when it
wants to validate some input.

HTH
xtian
No ants, no pants.



More information about the Python-list mailing list