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

Marvellous Monks,

Im selecting more than one item from the list box. But when i print the index of the selected contents it is not printing what i need.

curselection() method is not working for me. May be i have missed something so basic in Tk. Please help me.

my $mw=new MainWindow; $lb = $mw->Listbox( -selectmode =>"multiple")->pack; $lb->insert('end', @dir); my @selected = $lb->curselection; $mw->Button(-text=>"ok",-command=>[$mw => 'destroy'] )->pack; MainLoop; print scalar @selected;

Replies are listed 'Best First'.
Re: curselection in listbox
by strat (Canon) on Aug 02, 2004 at 06:24 UTC

    It is too early when you call the values by curselection (before MainLoop, i.e. before the user had a chance to interact with the window). Try something like

    my $mw=new MainWindow; $lb = $mw->Listbox( -selectmode =>"multiple")->pack; $lb->insert('end', @dir); $mw->Button(-text => "ok", -command=> \&dosomething)->pack; MainLoop; # ---------------------------- sub dosomething { my @selected = $lb->curselection; print scalar @selected; $mw->destroy(); } # dosomething

    (code not tested)

    due to the callback, &dosomething is called when the button is pressed, i.e. while MainLoop is running

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: curselection in listbox
by davidj (Priest) on Aug 02, 2004 at 06:27 UTC
    See my reply to Tk and MainLoop. It will explain in some detail what the problem is.

    davidj