|There's more than one way to do things|
Tk::Entry validate questionby Baboon (Acolyte)
|on Jun 14, 2002 at 17:54 UTC||Need Help??|
Baboon has asked for the wisdom of the Perl Monks concerning the following question:
Hello, wise perl users!
Let's consider quite simple task and it's solution.
It is required to place Tk::Entry widget and allow only digits inside it, but there must be at least one digit.
Problem is quite common, and after reading documentation I wrote following code:
Nothing complicated here, so all seems to be okay.
Let's start it and type digits. Validating works good.
But sometimes not that good. Not very good is when I select all contents inside that widget and
after that will try typing a number. Unexpectedly,
instead of replacing previous text with a new one,
following if occured: number appears
at the very start of Entry widget, and all previous text remains
and furthermore it is selected!
I've searched a bit inside Tk::Entry module and found the reason of this behaviour, which is not very hard to explain.
<KeyPress> callback calls 'Insert' function and it deletes selection at first step and insert new text at a second. So, when selection is tried to be deleted at first, validation fails, and after that next keystroke appended.
And not only validation for number will misbehave, but many other validations as well, so I consider current implementation of validation should be improved somehow.
Does anyone faced similar problem? How this could be solved?
Needless to say, the most elegant way will be found the best :)