Validating Entry in tkinter

rantingrick rantingrick at
Sun Jul 24 21:03:19 EDT 2011

On Jul 24, 7:11 pm, Saul Spatz <saul.sp... at> wrote:
> Can one do something like this in tkinter?

(1) First of all what exactly do you wish return?
 * an integer
 * a float
 * something else?

(2) Is this input part of a modal or non-modal interface?

For me, input validation should happen in *real* time and NOT after
the fact. For instance; if you have two entrys forms and a button in
your GUI like below...

 | Tk                 | X | |
 |                          |
 |  Value1: [          ]    |
 |  Value2: [          ]    |
 |                          |
 |       [Calculate]        |

... and your "validator" only checks the contents of "Value1" and
"Value2" AFTER the user presses the "Calculate" button then
congratulations because you have just created an unintuitive GUI
design. Instead, your entry widgets should have been filtering the key-
presses so that it would be impossible to enter anything but an
integer or float or whatever you want. Running a validater after the
fact is an anti pattern.

It is not very difficult to subclass a tk.Entry widget and create some
validation/filtering logic. However it would be helpful if you could
be a wee bit more specific about your validation needs.

More information about the Python-list mailing list