Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi thunders,

Your issues aren't so much related to passing that array around. Your main problem here is that you are trying to use functional code in an event driven framework. The @image_files array is already paired down to one element before any widgets are drawn because Tk doesn't render anything until you call MainLoop. This means your code for pairing down the images has to happen somewhere else, like in your return array function.

But before you go re-engineering your code to meet that requirement let me talk about returning your array. Don't do it! You are already passing a reference to the array so when your subroutine returns it has alread been modified. You'll see in my sample code that I don't return *anything* because I don't have to. I also clearly labled my reference, which IMO is a good coding practice.

My guess is that you haven't worked that much with event driven programming. I hadn't myself until I started playing a lot with Tk. I think you are on the right track, but there are probably easier (though more long winded) ways to approach what this accomplishes. I think this technique you are using is a neat hack. In either case I hope this helps you.

#!/usr/bin/perl -w use strict; use Tk; chdir('images'); my @image_files = <*.gif>; #initialize Main Window my $mw = MainWindow->new; show_two_buttons($mw,\@image_files); MainLoop; exit; # Subroutines # sub show_two_buttons{ my $mw = shift; for ($mw->packSlaves()){ $_->destroy() if Tk::Exists($_); #clear out window } my $image_file_REF = shift; my $first_pic = shift @{$image_file_REF}; my $second_pic = shift @{$image_file_REF}; #generate two buttons for my $file ($first_pic,$second_pic){ my $image = $mw->Photo(-file=>$file); $mw->Button( -image=>$image, -text=> $file, -command=>[\&return_array, $mw, $file, $image_file_REF] )->pack(-side=>"left"); } } sub return_array{ my $mw = shift; my $file = shift; my $image_file_REF = shift; push @{$image_file_REF}, $file; # Uncomment to show what's left. #print join ", ", @{$image_file_REF}; #print "\n"; # Here is where we do our test to see what is left if (scalar @{$image_file_REF} > 1) { show_two_buttons($mw, $image_file_REF); } else { print "All done!\n"; exit; } }
{NULE}
--
http://www.nule.org

In reply to Re: returning values from Perl-Tk events by {NULE}
in thread 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: (5)
As of 2024-04-19 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found