http://qs1969.pair.com?node_id=1150674

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:

my $fctrEntry = $mw->Entry( -textvariable => \$factor, -validate => 'focus', -validatecommand => \&validate, #-invalidcommand => \&invalidate, -insertbackground => 'cyan', -selectforeground => 'white', -selectbackground => 'DarkRed', -highlightcolor => 'cyan', -background => 'black', -foreground => 'white')->grid( -row => $chartrow[2] + 2, -column => 32, -columnspan => 3, -sticky => 'new'); sub validate { my $val = shift; $val ||= "1.00"; if ($val !~ /^-?\d{1,2}\.\d\d$/ or $val < -10 or $val > 10) { print "Bad! "; return 0; } else { print "Good! "; return 1; } } sub invalidate { $factor = sprintf("%.2f", $_[0]); print "Validate! $factor "; }

Replies are listed 'Best First'.
Re: Tk Entry validation conundrum
by GrandFather (Saint) on Dec 18, 2015 at 08:08 UTC

    This is a common interface conundrum. One solution that neatly skirts the line between intrusive and ineffective is to show an error indicator when the entry is invalid. You can then disable any action button (like OK) if there are any error indicators showing. A mouse over popup can then be used to show an error message and suggest a way to resolve the error.

    A problem with "correcting" values is that where one value range depends on another value it can get really hard for a user to get all values to the state they want. Using the error indicator trick means values can be edited in any order and transient bad states simply don't matter.

    Premature optimization is the root of all job security
Re: Tk Entry validation conundrum
by Discipulus (Canon) on Dec 18, 2015 at 08:55 UTC
    And if you use a sliding bar for that value? with a little space where show the current value? If you 've been told to not validate just constraint!

    just to give another perspective to your conundrum! (nice word anyway..)
    I think this is named 'scale' in Tk world:Tk::Scale

    $parent->new_ttk__scale(-orient => 'horizontal', -length => 200, -from + => -10.0 -to => 10.0)

    L*
    PS: tkdocs
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Why are you confusing Tk::Scale with ttkscale?
        because of my ignorance! thanks for the tip.

        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Tk Entry validation conundrum (numentry/spinbox)
by Anonymous Monk on Dec 18, 2015 at 09:06 UTC