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

Hi, I am currently working on a Popup menu that will be created from a flat file.

How do I determine which value the user has selected. Ive tried returning the value from the menu creation and the popup call. The sample code I am working on at the moment is
#set up main window display use Tk; $mw = new MainWindow(-title => 'PopUp'); @buttons = qw(A B C D E F G); #popup menu set up $popupmenu = $mw->Menu(-tearoff => 0); for (my $ctr = 0; $ctr <@buttons; $ctr++){ $popupmenu->insert(1, 'command', -label => $buttons[$ctr], -command => [sub {print $popupmenu->cget('text') ."\n"}], ); } $mw->bind("<Button-3>" => sub {$popupmenu->Popup(-popover => "curso +r", -popanchor => 'nw' +) -command => [sub { +print "Popup\n"}]}); MainLoop();

any help would be greatly appeciated

Replies are listed 'Best First'.
Re: Dynamic Popup Menus
by zentara (Cardinal) on Sep 29, 2008 at 20:50 UTC
    I changed your insert position from 1 to 0, and reversed the array to give a sane menu. Also use warnings and use strict to avoid problems.
    #!/usr/bin/perl use warnings; use strict; #set up main window display use Tk; my $mw = new MainWindow(-title => 'PopUp'); my @buttons = qw(A B C D E F G); #popup menu set up my $popupmenu = $mw->Menu(-tearoff => 0); foreach my $bt (reverse @buttons){ $popupmenu->insert(0, 'command', -label => $bt, #-command => [sub {print $popupmenu->cget('text') ."\n" +}], -command => [sub {print $bt ."\n"}], ); } $mw->bind("<Button-3>" => sub {$popupmenu->Popup(-popover => "cursor", -popanchor => 'nw'), -command => [sub {print "Popup\n"}]}); MainLoop();

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      Thanks for that. I knew it would be some thing simple, just not that simple.