in reply to Tk MatrixTable - entering data

Hi, you are looking for the -browsecommand option.
#!/usr/bin/perl use Tk; use Tk::TableMatrix; my $mw = MainWindow->new; my $arrayVar = {}; print "Filling Array...\n"; my ($rows,$cols) = (10, 10); foreach my $row (0..($rows-1)){ foreach my $col (0..($cols-1)){ $arrayVar->{"$row,$col"} = 2*$row + 3*$col; } } print "Creating Table...\n"; ## Test out the use of a callback to define tags on rows and columns sub colSub{ my $col = shift; return "OddCol" if( $col > 0 && $col%2) ; } my $t = $mw->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -width => 10, -height => 10, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, -browsecommand => \&brscmd, -colstretchmode => 'last', -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se' ); $mw->Button(-text => "Update", -command => \&update_table) ->pack(-side => 'bottom',-anchor => 'w'); $mw->Button(-text => "Disable", -command => \&dis_table) ->pack(-side => 'bottom',-anchor => 'w'); # hideous Color definitions here: $t->tagConfigure('active', -bg => 'white', -relief => 'sunken'); $t->tagConfigure('OddCol', -bg => 'lightyellow', -fg => 'black'); $t->tagConfigure('title', -bg => 'lightblue', -fg => 'black', -relief +=> 'sunken'); $t->tagConfigure('dis', -state => 'disabled', -bg => 'black'); $t->pack(-expand => 1, -fill => 'both'); $t->focus; Tk::MainLoop; sub update_table { print "1\n"; $t->tagCell('dis', '1,1' ); $t->tagRaise('dis', 'OddCol'); $t->activate('2,2'); $t->tagRow('active',3); # $t->configure(-padx =>( $t->cget(-padx))); # a trick needed sometimes to update } sub brscmd { my ($previous_index, $actual_index) = @_; my ($row, $col) = split ',', $actual_index; print "r$row c$col\n"; } sub dis_table{ print "2\n"; foreach my $col (1..4){ if ($arrayVar->{"6,$col"} >= 1) { print "col->$col\n"; $t->tagCell('dis',"7,$col"); $t->tagCell('dis',"8,$col"); $t->tagCell('dis',"9,$col"); } } $t->tagRaise('dis', 'OddCol'); }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Tk MatrixTable - entering data
by merrymonk (Hermit) on Jun 07, 2011 at 15:02 UTC
    Thanks for that quick reply but I have two more problems and another question.
    I ran your example and found that the cursor worked as I wanted.
    However, I could only see what I had typed in the cell after I moved to another cell (as I used the cursor the cell 'flashed' and went white background).
    I would like to see what is being typed.
    I added '-browsecommand => \&brscmd,' to the options and copied the brscmd code (see below) However, I could only get output from the sub when I used the button twice but data did get typed in to the correct cell and I can saw what is begin typed.
    How do I change things so that just one button hit selects the cell?
    One other thing has just occurred to me. Is there an option I can set so that when the cell highlighted the Delete key will removed the cell's contents?
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::TableMatrix; use Tk::TableMatrix::Spreadsheet; use File::Basename; my ($mw, $arrayvar1, $arrayvar2, $rows1, $cols1, $rows2, $cols2, $tm1, + $tm2, $label, $jh1, $jh2, $rc_str); my (%hd1, %hd2, $pad_x, $pad_y, $wg_row); my (@sym_avail, %sym_att_data); $mw = MainWindow->new; $label = $mw->Label(-text => "Table Matrix Cell Input Test with Browse + Command") ->grid( -row => 0, -column => 0, -columnspan => 6); # set wg_row for widgets $wg_row = 1; # set x and y pads $pad_x = 3; $pad_y = 3; $wg_row += 1; $mw->Button( -text => "Print Selected Rows", -command => sub{&from_use_list})->grid(-row => 4, -column => 1, +-rowspan => 1, -padx => $pad_x, -pady => $pad_y); # list $arrayvar2 = {}; $hd2{0}{text} = 'Part'; $hd2{1}{text} = 'X size'; $hd2{2}{text} = 'Y Size'; $hd2{3}{text} = 'Material'; $hd2{4}{text} = 'Thickness'; $rows2 = 30; $cols2 = 5; for ($jh2 = 0; $jh2 < $cols2; $jh2 ++) { $rc_str = '0,' . $jh2; $arrayvar2->{$rc_str} = $hd2{$jh2}{text}; } print "Creating Table...\n"; #sub colSub{ # my $col = shift; # return "OddCol" if( $col > 0 && $col%2) ; #} $tm2 = $mw->Scrolled('Spreadsheet', -rows => $rows2, -cols => $cols2, -width => 6, -colwidth => 20, -height => 20, -titlerows => 1, -titlecols => 1, -variable => $arrayvar2, -browsecommand => \&brscmd, -colstretchmode => 'last', -flashmode => 1, -flashtime => 2, -wrap=>1, -rowstretchmode => 'last', -selectmode => 'extended', -selecttype=>'cell', -selecttitles => 1, -drawmode => 'slow', -scrollbars=>'se', -sparsearray=>0 )->grid(-row => $wg_row, -column => 0, -rowspan => 10, + -padx => $pad_x, -pady => $pad_y); $wg_row = 15; $mw->Button( -text => "Exit", -command => sub{$mw->destroy})->grid(-row => $wg_row, -column => + 0, -rowspan => 1); #==================================== # # get_selections_from_tm # # this gets the seleect rows from the given tablematrix # #==================================== sub get_selected_rows_from_tm ($$$) { my ($tm, $ref_rows, $ref_rows_total) = @_; my ($sel, $js, @js_split); $sel = $tm->curselection(); foreach $js (@$sel) { # print "$js\n"; @js_split = split(/,/, $js); $ref_rows->{$js_split[0]} = 1; } $$ref_rows_total = scalar(keys %$ref_rows); } #=========================== # # sub to_use_list # # this moves symbols from the given ($tm1) to used list ($tm2) # # button < # #=========================== sub from_use_list() { my (%rows2_sel, $rows2_sel_total, $js, $cell_str, $cell_sym, $row_min) +; get_selected_rows_from_tm ($tm2, \%rows2_sel, \$rows2_sel_total); print "\n[from_use_list] selected rows total <$rows2_sel_total>\n"; $row_min = 9999; foreach $js (sort{$a <=> $b} keys %rows2_sel) { if($js < $row_min) { $row_min = $js; } print "$js\n"; } } sub brscmd { my ($previous_index, $actual_index) = @_; my ($row, $col) = split ',', $actual_index; print "[brscmd] row $row col $col\n"; } Tk::MainLoop;
      However, I could only see what I had typed in the cell after I moved to another cell (as I used the cursor the cell 'flashed' and went white background).

      These widgets have alot of internal bindings to mouse 1 clicks. Have you tried Double-clicking cells to initiate an edit?


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        On your exmaple, it does not matter if I give one of two mouse clicks. The cell still 'flashes' and I cannot see what I type in until I move to another cell.
        Could this be because of some setting on my PC rather than something associated with the TableMatrix?