|Think about Loose Coupling|
Tk Entry validation conundrumby Anonymous Monk
|on Dec 18, 2015 at 07:57 UTC ( #1150674=perlquestion: print w/replies, xml )||Need Help??|
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to create a Tk::Entry widget in my program that will accept a floating point value with two decimal places of precision, with a range of -10.00 to 10.00.
The way I'd prefer this to work is something like this:
While the user is typing in the Entry widget, pretty much anything is allowed, as long as it matches regex /^-?\d+\.?\d+$/
But as soon as the user leaves the Entry widget and it loses focus, any number that is out of range (< -10 or > 10) gets rejected, and numbers get formated to sprintf %.2f
My conundrum is that the Tk documentation goes to great pains to stress that you shouldn't use validation in an Entry widget to change the text variable. But that's exactly what I'm trying to do with it! If the user has typed a value that I consider out of range or invalid, I want to change it! How do I change it without changing it?
Here's some code to give an idea of how I'm floundering around with this. Please let me know what I should be doing here:
Back to Seekers of Perl Wisdom