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

This is my first post, so please be gentle if I have made any noob mistakes :)

I have found a post that attempts to tackle this problem, but the solutions provided are not very simple, or complete.

see Using Tk::Text and '<<Modified>>'

I need:

I am looking for something as simple as (or as close as possible):

#!/usr/local/bin/perl use warnings; use strict; use Tk; # Global variables my $entry_val; # Main Window my $mw = new MainWindow; $mw->Entry( -validate=>"all", -validatecommand=>\&handleEvent, -textvariable=>\$entry_val, )->pack(); MainLoop; # Callbacks sub handleEvent { my ($new_val, $chars_2_change, $curr_val, $index, $action) = @_; # do nothing if No Change and No Force Validate if (!$chars_2_change && $action != -1){ return; } # do something meaningful here. # for now, just print the $new_val print $new_val."\n"; }

While the above code works great, it only satisfies 2 out of my 3 requirements. I am hoping that I have missed a widget that provides this functionality simply, or some option(s) of Entry that I may have overlooked to provide scrollable multi-line support.

  • Comment on using TK Text (or something similar), is there a simple way to get the -validate=>"all" functionality in TK Entry?
  • Download Code

Replies are listed 'Best First'.
Re: using TK Text (or something similar), is there a simple way to get the -validate=>"all" functionality in TK Entry?
by Khen1950fx (Canon) on Jun 20, 2011 at 01:30 UTC
    This is as far as I could go with it. I couldn't get it to scroll, but maybe this can get you started.
    #!/usr/local/bin/perl use strict; use warnings; use Tk; use Tk::EntryCheck; my $entry_val; my $mw = MainWindow->new; $mw->geometry("500x200"); $mw->title("Value Entry"); my $mf = $mw->Frame()->pack(-side => 'top', -fill => 'x'); my $tf = $mf->Frame(-background => "red")->pack(-side => 'top', -fill =>'x'); my $lf = $mf->Frame(-background => "black")->pack(-side => 'left', -fill => 'y'); my $rf = $mf->Frame(-background => "white")->pack(-side => "right"); $tf->Label(-text => "Value Entry", -background => "red")->pack(-side =>"top"); $lf->Label(-text => "Enter value:", -background => "black", -foreground => "yellow")->pack(-side => "left"); my $entry = $lf->EntryCheck( -validate => "all" )->pack(-side => "left"); my $copy_button = $lf->Button(-text => "Copy Value", -command => \&copy_values)->pack(-side => "right"); my $clear_entries = $rf->Button(-text => "Clear Value", -command => \&clear_entries)->pack(-side => "top"); my $paste_entries = $rf->Text(-background => "black", -foreground => "green")->pack(-side => "top"); sub handleEvent { my ($new_val, $chars_2_change, $curr_val, $index, $action) = @_; if (!$chars_2_change && $action != -1) { return; } print $new_val."\n"; } sub clear_entries { $paste_entries->delete('0.0', 'end'); } sub copy_values { my $copied_text = $entry->get(); $paste_entries->insert("end", $copied_text); } MainLoop;
Re: using TK Text (or something similar), is there a simple way to get the -validate=>"all" functionality in TK Entry?
by zentara (Cardinal) on Jun 20, 2011 at 11:36 UTC
    I think you've seen all possible tricks for Tk, but I will also mention that Gtk2 has a text widget that might satisfy your requirements.
    #!/usr/bin/perl -w use strict; use Gtk2 -init; my $window = Gtk2::Window->new; $window->signal_connect (destroy => sub {Gtk2->main_quit}); $window->set_default_size (400, 300); my $textview = Gtk2::TextView->new; $window->add ($textview); my $n; $textview->get_buffer->signal_connect ( changed => sub { print "changed! ".(++$n)."\n"}); $window->show_all; Gtk2->main;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: using TK Text (or something similar), is there a simple way to get the -validate=>"all" functionality in TK Entry?
by Anonymous Monk on Jun 20, 2011 at 09:18 UTC
    Hmm, validating only makes sense for single-line widgets ... anywho
    #!/usr/bin/perl -- use Tk; use strict; use warnings; Main(@ARGV); exit(0); sub Main { our $status = ""; my $mw = tkinit; my $t = $mw->Scrolled(qw/ Text -relief sunken -borderwidth 2 -setgrid true -height 30 -scrollbars e /)->pack(qw/ -expand yes -fill both /); my $l = $mw->Label( -textvariable => \$status, -relief => 'groove', -font => "Arial", -anchor => 'center', )->pack( -side => 'bottom', -anchor => 's', -fill => 'both', ); use DDS; Dump( $l ); $t->bind( '<<Modified>>' => [ \&onModified , $t , $l ]); MainLoop; } sub onModified { ## warn " @_ "; ## go-figure , probably a bug ## Tk::Text=HASH(0xeb324c) Tk::Frame=HASH(0xed2fcc) Tk::Label=HASH(0x +fbfb94) my( $t, undef, $l ) = @_; if( $t->editModified ){ $t->editModified(0); ## reset count if( "invalid" ){ $t->bell; $::status = "invalid, forever"; $l->configure ( -background => 'red' ); } } return; }
    widget