Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

TK Listbox help

by PrimeLord (Pilgrim)
on Mar 10, 2004 at 23:11 UTC ( [id://335653]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks! I have just recently started learnign how to use Tk and I am already stuck on a problem. I have a listbox that lists the textfile names in the current directory. However I can not seem to figure out how to get the users selection from that list box. Here is what I have so far.
#!perl -w use strict; use Tk; # Main Window my $mw = MainWindow->new; # Frames my $label3_frame = $mw->Frame; my $files_frame = $mw->Frame; my $exit_frame = $mw->Frame; # File Listing Label $label3_frame->Label(-text => "Select Files to Track")->pack(-side => +'left'); # File Listing Listbox my @files; push @files, $_ for <*.txt>; my $listbox = $files_frame->Scrolled("Listbox", -scrollbars => "oe", -selectmode => "extended")->pack; $listbox->insert('end', @files); # Print Listbox Button my @filenames; my @selected_files = $listbox->curselection; for (@selected_files) { my $file = $listbox->get($_); push @filenames, $file; } $exit_frame->Button(-text => "Files", -command => sub { print "@filenames\n"; })->pack; $exit_frame->Button(-text => "Exit", -command => sub { exit; })->pack; # Pack Frames $label3_frame->pack(-side => 'top', -fill => 'y'); $files_frame->pack(-side => 'top', -fill => 'y'); $exit_frame->pack(-side => 'top', -fill => 'y'); # Main Loop MainLoop;
The @selected_files array is never getting populated with any of the selections. Any help you can offer would be very appreciated. Thanks!

-Prime

Replies are listed 'Best First'.
Re: TK Listbox help
by graff (Chancellor) on Mar 11, 2004 at 01:50 UTC
    Instead of this being in the main part of your gui-creation code:
    # Print Listbox Button my @filenames; my @selected_files = $listbox->curselection; for (@selected_files) { my $file = $listbox->get($_); push @filenames, $file; }
    you need to put those lines into a subroutine (e.g. called "print_filenames"), and maybe change the comment to read "Callback function for the "Print Listbox Button".

    Then, in the main part where you're declaring the other widgets, you need to actually create a Button widget that has something like "Print Chosen Files" as its text, and has this "print_files" sub as its callback.

    I guess you want the button you have labeled as "Files" to do the actual printing job, so something like this should work for you (not tested) -- note that the "-command" attribute of the "Files" button is being given a reference to an anonymous array, whose elements are a reference to the callback function, followed by the arg being passed to that function (the arg is the handle for the listbox where the selection will come from):

    ... my $listbox = $files_frame->Scrolled("Listbox", -scrollbars => "oe", -selectmode => "extended")->pack; $listbox->insert('end', @files); $exit_frame->Button(-text => "Files", -command => [ \&print_filenames, $listbox ])->pack; $exit_frame->Button(-text => "Exit", -command => sub { exit; })->pack; ... sub print_filenames { # callback for the "File" Button my $listbox = shift; my @selected_files = $listbox->curselection; for (@selected_files) { my $file = $listbox->get($_); print "$file\n"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://335653]
Approved by kvale
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-26 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found