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

I have done all I wanted except that all the options of the menu are not displayed when the cell is picked.
This is shown in the revised code below.
Note the menu is onyl shonw when a cell the column headed 6 is selected.
It would be great if someone can explain how this missing step can be done
#!/usr/bin/perl use strict "vars"; use Tk; use Tk::TableMatrix; require Tk::LabEntry; my $mw = MainWindow->new; # items to help with cell menus my ($menuTest, $last_browse_row, $last_browse_col); my $arrayVar = {}; print "Filling Array...\n"; my ($rows,$cols) = (200, 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, -colwidth => 20, -browsecommand => \&brscmd, -colstretchmode => 'last', -rowstretchmode => 'last', -selectmode => 'extended', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se' ); # existing ---------------------------------- #HERE IS WHERE I TRY TO USE BUTTONS #my $buttonTest = $mw->Button(-text => "test", -command => \&table_but +ton); # $t->set("3,3", $buttonTest); #wrong #$t->windowConfigure("2,2", -window => $buttonTest); #right # new ================================== # option menu test my @colour_list = (1,2,3,4,5,6,7,8,9,10,15,20,30,40,custom); my $sel_item; # diaglog box when chosen colour is custom my $dbox = $mw->DialogBox (-title => 'Custom Colour', -buttons => ['Ok', 'Cancel'], -default_button => 'Ok'); my $custom_colour; $dbox->add('LabEntry', -textvariable => \$custom_colour, -width => 20, -label => 'Qty Required', -labelPack => [-side => 'left'])->pack; # second mneu for hide test my $sel2_item; my $menuTest2 = $mw->Optionmenu( -variable => \$sel2_item, -options => [@colour_list], -command => \&menu_used2); $t->windowConfigure("4,4", -window => $menuTest2); # hide the 2nd menu #$menuTest2->packForget(); # diaglog box when chosen colour is custom my $dbox2 = $mw->DialogBox (-title => 'Custom Colour', -buttons => ['Ok', 'Cancel'], -default_button => 'Ok'); my $custom_colour2; $dbox2->add('LabEntry', -textvariable => \$custom_colour, -width => 20, -label => 'Qty Required', -labelPack => [-side => 'left'])->pack; # new ================================== # radio button test my $rb = 'N'; my $rb1 = $mw->Radiobutton(-text => 'Rb1', -value => '1', -variable => \$rb); my $rb_frame = $t->Frame(); my $rb2 = $rb_frame->Radiobutton(-text => 'N', -value => 'N', -variable => \$rb)->grid (-row => 0, -column => 0); my $rb3 = $rb_frame->Radiobutton(-text => 'P', -value => 'P', -variable => \$rb)->grid (-row => 0, -column => 1); # two radio button in cell - works $t->windowConfigure("4,3", -window => $rb_frame); #=================================== # 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 "[update_table] 1\n"; $t->tagCell('dis', '1,1' ); $t->tagRaise('dis', 'OddCol'); # only needed if coltagcommand used $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 ($cell_str, $cell_value); my ($row, $col) = split ',', $actual_index; $cell_str = "$row,$col"; $last_browse_row = $row; $last_browse_col = $col; print "[brscmd] row $row col $col cell value <$arrayVar->{$cell_str}>\ +n"; if ($col == 2) { print "[brscmd] for coll <$col>\n"; $menuTest->destroy if Tk::Exists($menuTest); $menuTest = $mw->Optionmenu( -variable => \$sel_item, -options => [@colour_list], -command => \&menu_used); $t->windowConfigure("$row,$col", -window => $menuTest); $sel_item = @colour_list[0]; $t->update(); } } 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'); # only needed if coltagcommand used } sub table_button () { print "\n[table_button] pressed\n"; } sub menu_used() { my ($answer); print "\n[menu_used] selection in <$sel_item>\n"; # show the dialog box if choice is custom if($sel_item eq 'custom') { $answer = $dbox->Show(); if($answer eq 'Ok') { $sel_item = $custom_colour; } else { $sel_item = 'none'; } } print "\n[menu_used] row <$last_browse_row> col <$last_browse_col> set +ting value <$sel_item>\n"; $arrayVar->{"$last_browse_row,$last_browse_col"} = $sel_item; $menuTest->destroy if Tk::Exists($menuTest); $t->activate("0,0"); &TMRefresh($t); } ######################################### sub TMRefresh { #Required input TableMatrix object. #use to force matrix to update, a code trick return if (!$_[0]); $_[0]->configure(-padx =>($_[0]->cget(-padx))); #$realmatrix->update; #$t->update; #$top->update; #$t->see("100,100"); #trick to force update? #$t->see("end"); #$t->see("1,1"); } sub menu_used2() { my ($answer); print "\n[menu_used2] selection in <$sel2_item>\n"; # show the dialog box if choice is custom if($sel2_item eq 'custom') { $answer = $dbox2->Show(); if($answer eq 'Ok') { $sel2_item = $custom_colour; } else { $sel2_item = 'none'; } } }
  • Comment on Tk TableMatrix - Hiding & Recalling Widgets (eg OptionMenu) in a TableMatrix cell
  • Download Code

Replies are listed 'Best First'.
Re: Tk TableMatrix - Hiding & Recalling Widgets (eg OptionMenu) in a TableMatrix cell
by stefbv (Priest) on Jun 09, 2011 at 06:40 UTC

    I'm not sure I understood your question, if it is: "Why the two Optionmenus behave differently?" then my answer is:

    The 'brscmd' sub dynamically creates and destroys an Optionmenu in the active cell, the second Optionmenu is fixed and created only once in the cell with index "4,4".

    It's good practice to "use warnings" and follow it's recommendation (Scalar value @colour_list[0] better written as $colour_list[0]). Also quote 'custom' in the @colour_list array and "use strict" (without "vars").

    Best regards, Stefan