This is an unofficial mirror of Tkinter reference documentation (based on Python 2.7 and Tk 8.5) created by the late John Shipman.
It was last updated in 2013 and is unmaintained. [More info]
In some applications, you will want to check the contents of
Entry widget to make sure they are valid
according to some rule that your application must enforce.
You define what is valid by writing a callback function that
checks the contents and signals whether it is valid or not.
Here is the procedure for setting up validation on a widget.
Write a callback function that checks the text in the
Entry and returns
the text is valid, or
False if not. If the
False, the user's attempt
to edit the text will be refused, and the text will be
Register the callback function. In this step, you will produce a Tcl wrapper around a Python function.
Suppose your callback function is a function named
isOkay. To register this function, use the universal widget method
.register(isOkay). This method returns a
character string that Tkinter can use to call your
When you call the
Entry constructor, use
validatecommand option in the
Entry constructor to specify your callback, and
validate option to specify when the
callback will be called to validate the text in the
callback. The values of these options are discussed
in more detail below.
Here are the values of the
validate option and
what they mean.
Validate whenever the
widget gets or loses focus (see Section 53, “Focus: routing keyboard input”).
Validate whenever the widget gets focus.
Validate whenever the widget loses focus.
Validate whenever any keystroke changes the widget's contents.
Validate in all the above situations.
Turn off validation. This is the default option
value. Note that this is the string
'none', not the special Python value
The value of the
validatecommand option depends
on what arguments you would like your callback to receive.
Perhaps the only thing the callback needs to know is what
text currently appears in the
that is the case, it can use the
method of the
textvariable associated with
the widget to retrieve that text.
In this case, all you need is the option “
is the name of your callback function.
Tkinter can also provide a number of items of information
to the callback. If you would like to use some of these
items, when you call the
use the option
is the name of your callback
function, and each additional
substitution code. For each substitution code that you
provide, the callback will receive a positional argument
containing the appropriate value.
Here are the substitution codes.
Table 18. Callback substitution codes
Action code: 0 for an attempted deletion, 1 for an
attempted insertion, or -1 if the callback was
called for focus in, focus out, or a change to
When the user attempts to insert or delete text,
this argument will be the index of the beginning
of the insertion or deletion. If the callback
was due to focus in, focus out, or a change to the
||The value that the text will have if the change is allowed.|
||The text in the entry before the change.|
||If the call was due to an insertion or deletion, this argument will be the text being inserted or deleted.|
The current value of the widget's |
The reason for this callback: one of |
||The name of the widget.|
Here is a small example. Suppose you want your callback to
'%d' to find out why it was called;
'%i' to find out where the insertion or
deletion would occur; and
'%S' to find out what
is to be inserted or deleted. Your method might look like
def isOkay(self, why, where, what): ...
Next you use the universal
to wrap this function. We assume that
okayCommand = self.register(isOkay)
To set up this callback, you would use these two options in
self.w = Entry(self, validate='all', validatecommand=(okayCommand, '%d', '%i', '%S'), ...)
Suppose that the
Entry currently contains the
'abcdefg', and the user selects
'cde' and then presses Backspace.
This would result in a call
isOkay(0, 2, 'cde'): 0 for deletion, 2 for the position before
'cde' for the string to be deleted. If
True, the new
text will be
'abfg'; if it returns
False, the text will not change.
Entry widget also supports an
invalidcommand option that specifies a callback
function that is called whenever the
command may modify the text in the widget by using the
.set() method on the widget's associated
textvariable. Setting up this option works the same
as setting up the
validatecommand. You must use
.register() method to wrap your Python
function; this method returns the name of the wrapped function
as a string. Then you will pass as the value of the
invalidcommand option either that string, or as
the first element of a tuple containing substitution codes.