[Tkinter-discuss] tkinter Entry validation: insert instead of replace

Teodor the Thinker doctorlans at gmail.com
Fri Feb 22 16:59:50 CET 2013


Thank you. It was so simple! I changed function return to
return P.isdigit() or (P == "")
and it seems work.

But it allows empty string not only during editing. I want to make: when i
clear the Entry and switch to another GUI element Entry rollbacks to last
valid value.

But obvious code to achieve this goal doesnt work:
if P == "" and V == 'focusout':
    return False
else:
    return (P.isdigit() or P == "")

I found when "return False" is executed, *s* variable is empty. So i need
to rollback not for last but to *before last* value. Is there a nice bultin
Tkinter way to achieve that or i should declare some variable with "before
last value" by my own?


2013/2/22 Michael Lange <klappnase at web.de>

> Hi,
>
> On Fri, 22 Feb 2013 01:50:45 -0800 (PST)
> lansman <codelans at gmail.com> wrote:
>
> > Anybody know why it happens?
>
> this is a common issue. To illustrate what's going on, I changed your
> validate callback a little into:
>
>     def _validate(self, d, i, P, s, S, v, V, W):
>         print '"%s"' % P,  P.isdigit()
>         return P.isdigit()
>
> then I ran the program and got:
>
> $ python test8.py
> "111" True # -> program start
> "111" True # -> selected the 111 with the mouse
> "" False # -> pressed the 9 key
> "9111" True
>
> So we see, what happens is that when the selected "111" is overwritten
> with "9" the Entry is *emptied first*, the empty string validates to
> False and is therefore not accepted by the widget and so the 111 remains
> in the widget when the 9 is inserted.
>
> So the trick here is to let the widget accept the empty string, as in:
>
>     def _validate(self, d, i, P, s, S, v, V, W):
>         if P == '':
>             return True
>         return P.isdigit()
>
> Regards
>
> Michael
>
> .-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.
>
> Lots of people drink from the wrong bottle sometimes.
>                 -- Edith Keeler, "The City on the Edge of Forever",
>                    stardate unknown
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20130222/374c893e/attachment.html>


More information about the Tkinter-discuss mailing list