Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

More Tk Help

by PrimeLord (Pilgrim)
on Jan 10, 2005 at 22:13 UTC ( [id://421116]=perlquestion: print w/replies, xml ) Need Help??

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

Monks I am again here hoping you can help me in my pursuit to learn Tk. I am creating a small program to create reports from various text files. The application first gives 3 different output types, then 2 different report types, and finally has a listbox of all the txt files in the directory. I am trying to pass all of this informatrion into a subroutine where I can start tracking the file changes. To test this I am just trying to get the value of the output, report_type, and listbox into the sub routine. The listbox valuse are fine, but the output type and report type is not changign with the radio buttons when I pass them. However outside of the subroutine the values do change with the radio buttons. Any suggestions on how to fix this? Here is the code.

use strict; use Tk; # Defaults my $version = "change_tracker 0.5 beta"; my $output = "HTML"; my $report_type = "SHORT"; # Main Window my $mw = MainWindow->new; $mw->title("$version"); # Frames my $label1_frame = $mw->Frame; my $outputlist_frame = $mw->Frame; my $label2_frame = $mw->Frame; my $typelist_frame = $mw->Frame; my $label3_frame = $mw->Frame; my $files_frame = $mw->Frame; my $exit_frame = $mw->Frame; # Output Lablel $label1_frame->Label(-text => "Type of Output")->pack; # Output List for ("HTML", "CSV", "TEXT") { $outputlist_frame->Radiobutton(-text => $_, -value => $_, -variable => \$output)->pack(-side => 'left'); } # Report Type Label $label2_frame->Label(-text => "Report Type")->pack; # Report Type List for ("SHORT", "LONG") { $typelist_frame->Radiobutton(-text => $_, -value => $_, -variable => \$report_type)->pack(-side => 'left'); } # File Listing Label $label3_frame->Label(-text => "Select Files to Track")->pack(-side => +'left'); # File Listing Listbox my @files; opendir DIR, "." or die "$!"; @files = sort grep /\.txt$/, readdir DIR; closedir DIR; my $listbox = $files_frame->Scrolled("Listbox", -scrollbars => "oe", -selectmode => "extended")->pack; $listbox->insert('end', @files); # Track Changes $exit_frame->Button(-text => "Track", -command => [ \&track_changes, ($listbox, $output, $report_typ +e) ])->pack; # Exit Button $exit_frame->Button(-text => "Exit", -command => sub { exit; })->pack; # Pack Frames $label1_frame->pack(-side => 'top', -fill => 'y'); $outputlist_frame->pack(-side => 'top', -fill => 'y'); $label2_frame->pack(-side => 'top', -fill => 'y'); $typelist_frame->pack(-side => 'top', -fill => 'y'); $label3_frame->pack(-side => 'top', -fill => 'y'); $files_frame->pack(-side => 'top', -fill => 'y'); $exit_frame->pack(-side => 'top', -fill => 'y'); # Main Loop MainLoop; sub track_changes { my ($listbox, $output, $report_type) = @_; my @files = $listbox->curselection; for (@files) { my $file = $listbox->get($_); print "$file\n"; } print "$output\n"; print "$report_type\n"; }


Any help you can offer would be very appreciated. Thanks!

-Prime

Replies are listed 'Best First'.
Re: More Tk Help
by tall_man (Parson) on Jan 11, 2005 at 00:00 UTC
    Your problem is in this code:
    $exit_frame->Button(-text => "Track", -command => [ \&track_changes, ($listbox, $output, $report_typ +e) ])->pack;
    You are capturing the initial values of $output and $report_type, not the values as they will be later, after you have toggled the radio buttons.

    A simple fix would be to hold onto references instead, like this:

    $exit_frame->Button(-text => "Track", -command => [ \&track_changes, ($listbox, \$output, \$report_t +ype) ])->pack;
    Then in your track_changes subroutine, do:
    sub track_changes { my ($listbox, $output_ref, $report_type_ref) = @_; my @files = $listbox->curselection; for (@files) { my $file = $listbox->get($_); print "$file\n"; } print "$$output_ref\n"; print "$$report_type_ref\n"; }
      Even better would be to use the closure form of a Tk command, a-like so:
      -command => sub { track_changes( $listbox, $output, $report_type ) + }
      This technique requires that the variables ($output and $report_type) be lexicals, which they are.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://421116]
Approved by jfroebe
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-20 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found