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

Hi Monks I have a single menubutton with few menuitems . My intention is whenever a single click on a menuitem its text for instance "supplier1" to be entered to the following textfield ($expenseentry) Thank You
use warnings; use Tk; 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", -menuitems=> [[ 'command' => "supplier 1", -command=> \&addtextfield], [ 'command' => "supplier 2" ], [ 'command' => "supplier 3" ], [ 'command' => "supplier 4" ]])->pack(-side=>'left',-anc +hor=>'nw'); 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,-ba +ckground => 'black') ->pack(-side => 'top',-fill => 'x'); $expenseentry = $frm_name2->Entry(-width=> 10) -> pack(-side =>'left',-anchor =>'nw'); #sub addtextfield #sub addtextfield { #$val = $$expensemb->configure(-menuitems=> [ 'command' => "" MainLoop;

Replies are listed 'Best First'.
Re: Tk - Menuitems -grab text
by liverpole (Monsignor) on Sep 22, 2007 at 13:51 UTC
    Hi props,

    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.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Tk - Menuitems -grab text
by props (Hermit) on Sep 22, 2007 at 17:56 UTC
    liverpole many thanks !!!! :)