Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-19 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found