use strict; use warnings; use Tk; use Tk::Table; use Tk::Entry; use Tk::Label; use constant kRows => 3; use constant kCols => 3; main (); # Use a main sub to ensure that there are no global variabless sub main { my $mw = new MainWindow; $mw->Label ( -text => "Enter any character", -foreground => "red", -font => "verdanafont 10 bold" )->pack (-side => "top"); my $table_frame = $mw->Frame ()->pack (-padx => "10"); my $table = $table_frame->Table ( -columns => kCols, -rows => kRows, -scrollbars => "o", -fixedrows => 1, -fixedcolumns => 1, -relief => 'raised', -pady => "20", -takefocus => "0" ); my $press = $mw->Button ( -text => "Press Me", -command => [\&PressMe, $mw, $table], # pass parameters into callback -font => "verdanafont 10 bold", -state => 'disabled', )->pack ( -side => "left", -padx => "5", -ipadx => "5" ); my $filled = 0; for my $col (0 .. kCols - 1) { my $tmp_label = $table_frame->Label (-text => "$col")->pack (); $table->put (0, $col, $tmp_label); } for my $row (0 .. kRows - 1) { for my $col (0 .. kCols - 1) { my $ent1 = $table->Entry ( -font => "verdana 10", -validate => 'key', # Pass button widget and a ref to $filled into callback. Need a # ref because we manipulate $filled in the callback -vcmd => [\&onEdit, $press, \$filled], )->pack (-ipady => "15"); $table->put ($row, $col, $ent1); } } $table->pack (); MainLoop; } sub onEdit { my ($button, $filled, $new, $edit, $current, $idx, $type) = @_; return 1 if $new eq $current; if (length $new && !length $current) { ++$$filled; } elsif (!length $new && length $current) { --$$filled; } if ($$filled == kRows * kCols) { $button->configure (-state => 'normal'); } else { $button->configure (-state => 'disabled'); } return 1; } sub PressMe { my ($mw, $table) = @_; my $message = "alphanumeric entered"; Outer: for my $row (0 .. kRows - 1) { for my $col (0 .. kCols - 1) { my $name = $table->get ($row, $col); my $val = $name->get (); next if $val =~ m/^[a-z0-9]+$/i; $message = "Bad entry ($val) in row " . ($row + 1) . ", column " . ($col + 1); last Outer; } } my $response = $mw->messageBox ( -type => "ok", -message => $message, -icon => "info", -title => "info!!" ); }