Hello fellow Monks,

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?

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.

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.

Any suggestions?

#!/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 rem +ains 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 }

In reply to returning values from Perl-Tk events by thunders

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.