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

Dear Monks,

I have the following script generating a simple GUI. It uses tk:table to show the content of a list (array). What I am not able to do is to select/get the word in colum 2 by selecting the word and pressing the Return button. I'd also like to do the same by double clicking the word.

#!/usr/bin/perl -w use strict; use Tk; use tk::table; my $mw = MainWindow->new(); $mw->configure(-title => 'EnterMode - Version 4.0.1'); $mw->resizable( 0, 0 ); create_table_terms(); MainLoop(); my $nge; sub create_table_terms { my @terms = ("Quarter","Dime","Nickel"); my $ngrams=@terms; my $ngramstable = $mw->Table( -columns => 2, -rows => $ngrams, -scrollbars => 'e', -relief => 'raised', ); $ngramstable->pack(); my $cont=0; foreach my $ngram (@terms){ my $exportngram; my $cbd= $ngramstable->Checkbutton( -variable=>\$exportngram, -indicatoron=>'1', -state=>'normal', -width => 1, -anchor=>'nw', -background => 'white', -relief => 'groove', -padx=>5); $nge = $ngramstable->Entry( -textvariable=>\$ngram, -width => 23, -background => 'white', -relief => 'groove' ); $nge -> bind("<Return>", [ \&start_search_from_extracted_terms, "ent +ry", "Return" ] ); $ngramstable->put( $cont, 0, $cbd); $ngramstable->put( $cont, 1, $nge); ++$cont; } } sub start_search_from_extracted_terms { my $word=$nge -> get(); print "$word"; }

Any idea would be appreciated.

Replies are listed 'Best First'.
Re: tk:table get active cell
by biohisham (Priest) on Sep 14, 2010 at 00:10 UTC
    Your foreach loop will only make your GUI widget print the last element in the array when you select an entry, you need to re-engineer that. You're not making use of the widget state variable $exportngram whose value toggles between 0 and 1 depending on whether it is unselected or selected but again, this might not be serving any purpose in the code snippet you posted.

    I suggest you re-implement the same using a ListBox or a BrowsEntry widget..

    #This snippet uses a listbox to deliver the required behavior. #You can double click a value to show or just use the print button. use strict; use warnings; use Tk; my $mainWindow = MainWindow->new(-title=>"Main Window"); my $listBox = $mainWindow->Scrolled( 'Listbox', -scrollbars=>'e' )->pack; $listBox->insert('end',qw(Nickle Dime Quarter)); $listBox->bind('<Double-1>', \&getActive); $mainWindow->Button( -text=>'print value', -command=>\&getActive, )->pack(); sub getActive{ my $currency; $currency = $listBox->get('active'); #get the current selection print $currency,"\n"; }; MainLoop;


    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

      Thank you

      The <Double-1> works fine. The checkboxes will be used to select the instances of interest and then export them.

      Do you really mean there is no chance to get the active "cell", i.e. the active row $row, of a table? TK::Table information on the web are really sort of "too short" :( Or any chanche to insert checkbuttons in a listbox?

Re: tk:table get active cell
by zentara (Cardinal) on Sep 14, 2010 at 10:04 UTC
    Hi, first in your example, it's "use Tk::Table;"

    Second, Tk::TableMatrix is a better widget, and you can get active cells with it, with

    myTableMatrix->index('active');

    There are some ideas in how to disable the button untill certain fields are filled?

    To get current cell, you can do something like

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Table; #The key point is that the scroll bars only scroll non-fixed columns. my $mw = MainWindow->new; #$mw->geometry("200x200"); my $x; my $y; my $table = $mw->Table (-rows => 9, -columns => 9, -fixedrows => 9)->pack; foreach my $row (0 .. 8) { foreach my $col (0 .. 8) { my $cell = $table->Entry (-width => 4, -text => "$col, $ +row"); $table->put ($row, $col, $cell); $cell->bind('<ButtonPress-1>' => sub{ $x = $col; $y = $row }); } } my $button = $mw->Button(-text => 'Get Current', -command => sub{ print "$x $y\n"; })->pack(); MainLoop;
    also look at this example
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Table; my $mw= tkinit; #$mw->geometry("400x400+100+100"); my $table = $mw->Scrolled('Table', -rows =>4, -columns => 4, -fixedrows => 5, #trick to hide scrollbars -fixedcolumns => 5, -scrollbars => 'osoe', -takefocus => 1,)->pack(-fill=>'both', -expand=>1); my %widgets; for my $row(1..4){ for my $col(1..4){ $widgets{$row}{$col} = $table->Button( -text=> "$row - $col", -background => 'white', -command => sub{ do_me($row,$col) } ); $table->put( $row,$col,$widgets{$row}{$col} ); } } MainLoop; sub do_me{ my ($row, $col) = @_; print "$row $col\n"; }

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