Validating Entry in tkinter

rantingrick rantingrick at gmail.com
Mon Jul 25 15:55:09 EDT 2011


On Jul 25, 2:08 pm, Peter Otten <__pete... at web.de> wrote:
> Terry Reedy wrote:
> > On 7/25/2011 8:31 AM, Peter Otten wrote:
> >> Saul Spatz wrote:
> > is it possible to set an onkey handler, that will pass on
> > valid keys?
>
> With validatecommand you can have tkinter provide the string that is being
> inserted:

Yes but it's messy and requires knowledge of Tcl! We to keep our code
bases as Pythonic as possible.

> If you need something more specific you'd probably have to bind the
> <KeyPress> event to a custom handler:

Exactly!

If you compare the code of the two approaches you'll see that the
python approach is more readable and does not export any "magic"
behind the scenes. By binding the KeyPress event and handling it in
some derived class people who are not familiar with Tcl\Tk can read
the code (and change it to suit their needs). The next best
alternative would be to extend Tk.Entry with a pythonic wrapper for
this functionality.

MORAL: Sometimes you are forced to export these things but in this
case i would argue that readability counts; so keep it in Python!



More information about the Python-list mailing list