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 <klappn...@web.de>

> Hi,
>
> On Fri, 22 Feb 2013 01:50:45 -0800 (PST)
> lansman <codel...@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@python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to