perlquestion
thunders
Hello fellow Monks,<br>
<p>I've been working with Tk for a few days now and I've run into something I can't get my brain around. How can I return or pass a value from a button event?<br>
<p>I'm writing an application that will let me compare
photographs from a directory two at a time until I've decided on a favorite. The problem comes with trying to achieve this using Tk Buttons.
<p>I've tried several different methods, but I still cant figure how to pass an array or return it through a button event subroutine, so that my array will eventually shrink as I make choices.
<p>Any suggestions?
<code>
#!/usr/bin/perl -w
use Tk;
chdir('images');
my @image_files = <*.gif>;
#initialize Main Window
my $mw = MainWindow->new;
#eventually I want to have this part continue until only one image remains
while (scalar @image_files > 1){
my $pic1 = shift @image_files;
my $pic2 = shift @image_files;
@image_files = show_two_buttons($mw, $pic1,$pic2,\@image_files);
}
#do something when we have one picture left.
#dosomething();
MainLoop;
sub show_two_buttons{
my $mw = shift @_;
for ($mw->packSlaves()){
$_->destroy() if Tk::Exists($_); #clear out window
}
my $first_pic = shift @_;
my $second_pic = shift @_;
my @image_files = @{shift @_};
#generate two buttons
for $file($first_pic,$second_pic){
my $image = $mw->Photo(-file=>$file);
#cant figure how to return or pass arrays on the command here
$mw->Button( -image=>$image, -text=> $file,
-command=>[\&return_array, $file, \@image_files]
)->pack(-side=>"left");
}
return @image_files;#array never changes
}
sub return_array{
my $file = shift @_;
my @image_files = @{shift @_};
push @image_files, $file;
print @image_files; #only contains one file
return @image_files;#doesn't return to anything
}
</code>