murugu has asked for the wisdom of the Perl Monks concerning the following question:

Hi great Monks,

This is my first program in PERL/TK.

I want user to select some text files from a list of text files in a folder.

I created that number of check buttons inorder to be selected by the user.

Pushing the checked contents into an array.

where the MainLoop must be placed. (i.e) where to use the array @a, whether before mainloop or after it.

My part of code is here:

my @a=(); @dir=qw(a.txt b.txt c.txt);## LIst of the text files in a directory. my $mw=new MainWindow; for my $i (0..$#dir) { $mw->Checkbutton(-text => $dir[$i], -command=>sub {push @a, $dir[$i]}) +->pack; } $mw->Button(-text=>"ok",-command=>sub {exit})->pack; MainLoop; print @a;

here in the above code @a prints none.

For the above mentioned problem can i use Win32::GUI's BrowseForFolder function.

Im struck with this one.

Please give your valuable suggestions.

Sorry for my English.

--Murugesan--

Replies are listed 'Best First'.
Re: Tk and MainLoop
by graff (Chancellor) on Jul 29, 2004 at 05:13 UTC
    There are a few different problems here...
    1. PodMaster's reply is the answer to your main question: your "ok" button invokes exit; the program exits rather than returning from MainLoop; the final print statement is never reached. By following his advice ("ok" button destroys the window rather than exiting the program), you'll see the print-out. But also...

    2. In a Tk GUI, a Checkbutton can be turned on and off any number of times. The callback sub is called every time the user clicks the button (on or off), so the way your code is written, the user could stack the same file name onto @a multiple times, and if the final click turns the button off, the file name is still in the array.

    3. If the directory has a lot of text files, you'll end up creating a lot of Checkbuttons -- does your window layout support this? (For first-time Tk scripters, the answer is usually "no".)

    4. I assume the print @a at the end is just meant for debugging purposes, but it if there are two or file names in the array, they will be printed with nothing to separate them; try print "@a\n" instead -- placing the array in double quotes will cause the items to be printed with a space between them (space is the default value of "$," (output field separator -- see perldoc perlvar).

    It would be better to use a Listbox widget to present file names (or even the ready-made Tk::FileSelect, though I think this sort of method is a pain for users). A simple Listbox scales well from just a few elements to hundreds, and it's easy to make it scrollable:

    my $list = $mw->Scrolled('Listbox', ...);
    (update: forgot to mention -- Listbox does support multiple selections, of course.)
Re: Tk and MainLoop
by davidj (Priest) on Jul 29, 2004 at 05:06 UTC
    Here's a little info that will help you understand Perl/Tk programs:

    Perl/Tk applications are event driven, which means that the program waits for "events" (keyboard, mouse, etc) and then executes accordingly. Event driven programs have a loop somewhere in them that catches and processes these "events". This is what MainLoop in Tk is. It is simply a while loop that catches and processes events. Consequently, the code after MainLoop never gets executed, unless it is

    1) part of a sub that is called before MainLoop executes or
    2) part of a callback attached to a Tk widget.

    This is why print @a; never prints. To get it to print, put it in a sub that is part of a callback. Something like:
    $mw->Button(-text => "print", -command=> sub {print @a })->pack;
    or
    $mw->Button(-text => "print", -command => \&print_it);
    then somewhere below MainLoop
    sub print_it() { print @a; }
    For a better explanation of Perl/Tk programming I would suggest that you get the book Mastering Perl/Tk.

    Hope this helps,
    davidj
Re: Tk and MainLoop
by PodMaster (Abbot) on Jul 29, 2004 at 04:56 UTC
    What do you think exit does? `perldoc -f exit'

    update: try -command=>[$mw => 'destroy']

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Thanks for ur response.

      Its working fine for me now.

      Please let me know whether i can use Win32::Gui.

      Million Thanks.