Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: returning values from Perl-Tk events

by {NULE} (Hermit)
on Mar 26, 2002 at 23:16 UTC ( [id://154532]=note: print w/replies, xml ) Need Help??


in reply to returning values from Perl-Tk events

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

Replies are listed 'Best First'.
Re: Re: returning values from Perl-Tk events
by thunders (Priest) on Mar 26, 2002 at 23:33 UTC
    My guess is that you haven't worked that much with event driven programming

    Good Guess.

    I've had Learning Tk for a while. I've written a few dozen GUI apps with it, and I've done VB, but The event driven model was mostly transparent to me until I tried something like this.

    Thank you so much for the code and advice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://154532]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-29 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found