I think what you're looking for is this:
use strict; use warnings; use Tk; # User-defined my @menubutton_items = ( 'supplier 1', 'supplier 2', 'supplier 3', 'supplier 3', ); # Main program my $chosen_item = ""; my $mw = new MainWindow; $mw->geometry("400x400+200+0"); my $frm_name = $mw-> Frame( -relief=>'groove', -borderwidth=>5, -background=>'black' )->pack(-side =>'top', -fill=>'x'); my $expensemb = $frm_name->Menubutton( -tearoff => 0, -text =>"Eksodo", )->pack(-side=>'left',-anchor=>'nw'); # Pack each item into the Menubutton foreach my $item (@menubutton_items) { $expensemb->command(-label => $item, -command => sub { addtext($it +em) }); } my $costlabel = $frm_name -> Label(-text=>"Timh", -background => 'red') -> pack(-side=>'left', -anchor=>'nw', -padx= +>70); my $frm_name2 = $mw -> Frame( -relief=> 'groove', -borderwidth => 5,-background => 'black') ->pack(-side => 'top',-fill => 'x'); my $expenseentry = $frm_name2->Entry(-width=> 10, -textvar => \$chosen +_item) -> pack(-side =>'left',-anchor =>'nw'); MainLoop; # Subroutines sub addtext { my ($item) = @_; $chosen_item = $item; }
Using the command -textvar => \$chosen_item links whatever is assigned to $chosen_item to the Entry widget. (You might even consider maken it a Label widget, so that the user can't change its contents; you could use the -relief and -border options to make it look just like an Entry widget, but without being directly modifiable).
The line:
$expensemb->command(-label => $item, -command => sub { addtext($it +em) });
packs each text item into the Menubutton, and associates that text with a callback to addtext. You could even certainly do:
$expensemb->command(-label => $item, -command => sub { $chosen_ite +m = $item });
if you wanted to do nothing other than assign the Entry text, but I made it into a separate subroutine in case you need to take other actions when the user makes a selection.
In reply to Re: Tk - Menuitems -grab text
by liverpole
in thread Tk - Menuitems -grab text
by props
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |