in reply to Retrieving selections from Tk:TableMatrix

Hi chungley2000 and welcome to the monastery,

I was happy to see your post, because I've been thinking about working up a Tk project. It helps for you to specify in code what modules you use. There can be some overlap in cpan resources in some namespaces. It seems that I didn't have Tk, if that is the module you use. It was a truly eye-poppng install. At one point, the screen was covered in camels.

I state without proof that I think all contemporary perl should be written with "strictness." I achieve this by the line use 5.011; . Then I just took wild guesses for values in order to get the script to compile. Then I can hit it with perltidy to make it legible. The idea is that you come up with some self-contained example such that it can be run:

$ ./1.tk.pl Can't call method "title" on an undefined value at ./1.tk.pl line 7. $ perltidy -i=2 -b 1.tk.pl $ cat 1.tk.pl #!/usr/bin/perl -w use 5.011; use warnings; use Tk; use Data::Dumper; my $mw1->title("CMVC File Retriever"); #----------------------------------------------------------------- # Set up two frames: # - $frameTop - contains radio buttons for selecting table. # - $frameBottom - contains button to quit without update. #----------------------------------------------------------------- my $update = undef; my $frameTop = $mw1->Frame( -label => "QUERY Table (Last update as of $update)", -relief => 'groove', -borderwidth => 1 )->pack( -side => 'top', -fill => 'both' ); my $table = "fileview"; # default table (turns radio button on) $frameTop->Radiobutton( -text => "file table", -value => "fileview", -variable => \$table )->pack( -side => 'left' ); ### Skipping some of the lines... my $frameBottom->Button( -bg => 'green', -text => "Get Files", -anchor => 'center', -command => \&getSelection )->pack( -side => 'left' ) if $table eq "fileview"; ### Skipping more of the lines... my ( $rows, $cols, $arrayVar ) = ( 10, 5, undef ); my $t = $frameTop->Scrolled( 'Spreadsheet', -rows => $rows, -cols => $cols, -width => 150, -height => 10, -titlerows => 1, -titlecols => 2, -rowtagcommand => \&rowSub, -coltagcommand => \&colSub, -selectmode => 'extended', -variable => $arrayVar, -selecttitle => 1, ); my ( $currentText, $activeText ) = ( "current", "active" ); $t->configure( -browsecommand => sub { my ($index) = @_; $currentText = $index; $activeText = $t->get($index); } ); $t->configure( -validate => 1, -validatecommand => sub { my ( $row, $col, $old, $new, $index ) = @_; $activeText = $new; return 1; } ); my $results = "not much yet, but started writing code...."; ### ### This here is interesting since I think getting selection ### should be easy. I would expect selectioncommand is meant ### to be used to "extend" the default behavior... But I don't ### see a real problem with this approach. ### $t->configure( -selectioncommand => sub { my ( $NumRows, $Numcols, $selection, $noCells ) = @_; $results = $selection; # assign results to var to read later print "DEBUG: I am here at selectioncommand with results = $resul +ts,\n"; return $selection; # NOT sure where this is returned, so as +sign above } ); ### Skip again to where we are trying to "access" the ### selection's results: #----------------------------------------------------------------- # Subroutine to get the selected data and copy it to a user dir #----------------------------------------------------------------- sub getSelection { #----------------------------------------------------------------- # Get selected item and parse into values. #----------------------------------------------------------------- ### The program was meant to operate on this global $results, ### but it is empty... ### It seems that selectioncommand never got executed. ### What is even more weird for me is that out of the 15/20 ### different tests that I ran to try to debug this, "twice" ### (and I don't know how to replicate it), the ### selectioncommand executed some seconds AFTER. ### I tried explicitly declared $results as a global variable ### by adding "our $results;" before the use in this AND also ### in selectioncommand's subroutine. ### The other thing that I tried is do use $table->curselection(), ### but I don't know how to use that properly; I tried ### $table->curselection() and $frameTop->curselection(), ### but they failed. print "get Selection results are , $results,\n"; # TESTING print Dumper($results); # TESTING } $

The first problem is the first line, line 7 in this listing. Start tracking your values. A say statement after lines when values are set keeps bad ones from propagating.

say "result is $result";

If your code is too long to post here, put it on github and post a link to it.

I hope this helps, and I'm grateful to have the perl community helping to solve problems for me.

Replies are listed 'Best First'.
Re^2: Retrieving selections from Tk:TableMatrix
by Aldebaran (Curate) on Oct 10, 2018 at 22:40 UTC

    Hi chungley2000 and welcome to the monastery,

    I was happy to see your post, because I've been thinking about working up a Tk project. It helps for you to specify in code what modules you use. There can be some overlap in cpan resources in some namespaces. It seems that I didn't have Tk, if that is the module you use. It was a truly eye-popping install. At one point, the screen was covered in camels.

    I state without proof that I think all contemporary perl should be written with "strictness." I achieve this by the line use 5.011; . Then I just took wild guesses for values in order to get the script to compile. Then I can hit it with perltidy to make it legible. The idea is that you come up with some self-contained example such that it can be run:

    $ ./1.tk.pl Can't call method "title" on an undefined value at ./1.tk.pl line 7. $ perltidy -i=2 -b 1.tk.pl $ cat 1.tk.pl #!/usr/bin/perl -w use 5.011; use warnings; use Tk; use Data::Dumper; my $mw1->title("CMVC File Retriever"); #----------------------------------------------------------------- # Set up two frames: # - $frameTop - contains radio buttons for selecting table. # - $frameBottom - contains button to quit without update. #----------------------------------------------------------------- my $update = undef; my $frameTop = $mw1->Frame( -label => "QUERY Table (Last update as of $update)", -relief => 'groove', -borderwidth => 1 )->pack( -side => 'top', -fill => 'both' ); my $table = "fileview"; # default table (turns radio button on) $frameTop->Radiobutton( -text => "file table", -value => "fileview", -variable => \$table )->pack( -side => 'left' ); ### Skipping some of the lines... my $frameBottom->Button( -bg => 'green', -text => "Get Files", -anchor => 'center', -command => \&getSelection )->pack( -side => 'left' ) if $table eq "fileview"; ### Skipping more of the lines... my ( $rows, $cols, $arrayVar ) = ( 10, 5, undef ); my $t = $frameTop->Scrolled( 'Spreadsheet', -rows => $rows, -cols => $cols, -width => 150, -height => 10, -titlerows => 1, -titlecols => 2, -rowtagcommand => \&rowSub, -coltagcommand => \&colSub, -selectmode => 'extended', -variable => $arrayVar, -selecttitle => 1, ); my ( $currentText, $activeText ) = ( "current", "active" ); $t->configure( -browsecommand => sub { my ($index) = @_; $currentText = $index; $activeText = $t->get($index); } ); $t->configure( -validate => 1, -validatecommand => sub { my ( $row, $col, $old, $new, $index ) = @_; $activeText = $new; return 1; } ); my $results = "not much yet, but started writing code...."; ### ### This here is interesting since I think getting selection ### should be easy. I would expect selectioncommand is meant ### to be used to "extend" the default behavior... But I don't ### see a real problem with this approach. ### $t->configure( -selectioncommand => sub { my ( $NumRows, $Numcols, $selection, $noCells ) = @_; $results = $selection; # assign results to var to read later print "DEBUG: I am here at selectioncommand with results = $resul +ts,\n"; return $selection; # NOT sure where this is returned, so as +sign above } ); ### Skip again to where we are trying to "access" the ### selection's results: #----------------------------------------------------------------- # Subroutine to get the selected data and copy it to a user dir #----------------------------------------------------------------- sub getSelection { #----------------------------------------------------------------- # Get selected item and parse into values. #----------------------------------------------------------------- ### The program was meant to operate on this global $results, ### but it is empty... ### It seems that selectioncommand never got executed. ### What is even more weird for me is that out of the 15/20 ### different tests that I ran to try to debug this, "twice" ### (and I don't know how to replicate it), the ### selectioncommand executed some seconds AFTER. ### I tried explicitly declared $results as a global variable ### by adding "our $results;" before the use in this AND also ### in selectioncommand's subroutine. ### The other thing that I tried is do use $table->curselection(), ### but I don't know how to use that properly; I tried ### $table->curselection() and $frameTop->curselection(), ### but they failed. print "get Selection results are , $results,\n"; # TESTING print Dumper($results); # TESTING } $

    The first problem is the first line, line 7 in this listing. Start tracking your values. A say statement after lines when values are set keeps bad ones from propagating.

    say "result is $result";

    If your code is too long to post here, put it on github and post a link to it.

    I hope this helps, and I'm grateful to have the perl community helping to solve problems for me.