grasshopper!!! has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to tie a scalar to Tk entry and a subroutine as to return length of input on each letter input. Writting twitter client and wish to display number of characters input to entry in label on each keystroke at a loss. thank you

Replies are listed 'Best First'.
Re: tie variable
by AnomalousMonk (Archbishop) on May 29, 2015 at 22:44 UTC
    ... wish to display number of characters input to entry in label on each keystroke ...

    Here's one approach using entry input validation. I'm sure there are others and better.

    c:\@Work\Perl\monks>perl -wMstrict -le "use Tk; use Tk::LabEntry; ;; my $MW = tkinit; ;; my $entrytext = ''; my $le = $MW->LabEntry( -label => 'chars entered so far: 0', -textvariable => \$entrytext, -validate => 'key', -invalidcommand => undef, ); $le->configure( -validatecommand => sub { showcount($le, @_) }, ); $le->pack; ;; MainLoop; ;; print qq{final entry: '$entrytext'}; ;; sub showcount { my ($widget, $newtext, ) = @_; ;; my $label = $widget->cget(-label); $label =~ s{ \d+ \z }{ length $newtext }xmse; $widget->configure(-label => $label); 1; } "


    Give a man a fish:  <%-(-(-(-<

Re: tie variable
by Anonymous Monk on May 30, 2015 at 07:45 UTC
    Tk::Trace
    #!/usr/bin/perl -- use strict; use warnings; use Tk; use Tk::Trace; my $mw = tkinit; my $tv = "yo"; my $tt = $mw->Entry( )->pack; $tt->configure( -textvariable , \$tv ); $mw->traceVariable( \$tv, 'w' => [ sub { warn "you changed it @_ "; $ +_[1]; } ] ); $mw->after( 1000 => sub { $tv.= ' '.rand.' '; } ); $mw->MainLoop;

      Thanks worked out that binding the entry keypess to sub worked