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".# Print Listbox Button my @filenames; my @selected_files = $listbox->curselection; for (@selected_files) { my $file = $listbox->get($_); push @filenames, $file; }
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"; } }
In reply to Re: TK Listbox help
by graff
in thread TK Listbox help
by PrimeLord
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |