[Tkinter-discuss] validating an entry
Michael Lange
klappnase at web.de
Tue Aug 22 11:49:28 CEST 2006
On Mon, 21 Aug 2006 23:03:57 -0400
Jeff Cagle <jrcagle at juno.com> wrote:
> So it turned out that validating was not too hard, but you had to Google
> for the documentation. Your name, Cameron, showed up a couple of times...
>
> Here's the code:
>
> # Testing an edit box with validation
>
> from Tkinter import *
>
>
> class MyEntry(Entry):
>
> def __init__(self, master, maxchars):
> Entry.__init__(self, master, validate = "key",
> validatecommand=self.validatecommand)
> self.MAX = maxchars
>
> def validatecommand(self, *args):
> return len(self.get()) < self.MAX
>
>
> if __name__ == '__main__':
> tkmain = Tk()
> e = MyEntry(tkmain, 5)
> e.grid()
> tkmain.mainloop()
>
> The validatecommand has to return a Boolean. I have no idea of the
> following, and haven't been able to find answers yet online:
>
> 1) what args are passed to validatecommand? Obviously *args scoops them
> up, but would one ever want to use them?
> 2) what values other than "key" are acceptable to validate?
>
Hi Jeff,
unfortunately these things do not seem to be documented for Tkinter, the Tk manpages
are your friend. From the entry man page:
Command-Line Name:-validate
Database Name: validate
Database Class: Validate
Specifies the mode in which validation should operate: none,
focus, focusin, focusout, key, or all. It defaults to none.
When you want validation, you must explicitly state which mode
you wish to use. See Validation below for more.
VALIDATION
Validation works by setting the validateCommand option to a script
which will be evaluated according to the validate option as follows:
none Default. This means no validation will occur.
focus validateCommand will be called when the entry receives or
loses focus.
focusin validateCommand will be called when the entry receives focus.
focusout validateCommand will be called when the entry loses focus.
key validateCommand will be called when the entry is edited.
all validateCommand will be called for all above conditions.
It is possible to perform percent substitutions on the validateCommand
and invalidCommand, just as you would in a bind script. The following
substitutions are recognized:
%d Type of action: 1 for insert, 0 for delete, or -1 for focus,
forced or textvariable validation.
%i Index of char string to be inserted/deleted, if any, otherwise -1.
%P The value of the entry if the edit is allowed. If you are config-
uring the entry widget to have a new textvariable, this will be
the value of that textvariable.
%s The current value of entry prior to editing.
%S The text string being inserted/deleted, if any, {} otherwise.
%v The type of validation currently set.
%V The type of validation that triggered the callback (key, focusin,
focusout, forced).
%W The name of the entry widget.
Passing these percent substitutions in Tkinter is a little tricky. Here is a code
snippet I used to validate a Spinbox widget, it should work with an Entry just the
same I think:
vcmd = (self.register(self._validate), '%s', '%P')
self.config(vcmd=vcmd)
def _validate(self, old, new):
(...)
Remember that Tk will pass any values to the validatecommand as strings, even if you
would expect an integer (as in '%i').
I hope this helps
Michael
More information about the Tkinter-discuss
mailing list