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

Hi, My question is on Perl-Tk (Perl v5.10.0, perl-tk 1:804.028). I'm trying to write a context-menu, being called by a right-click and containing commands 'Copy' and 'Paste'. This context-menu must work with all 'entry' widgets of the main window. There are multiple 'entry' widgets in the main window, and their number can vary, because they are generated by a subroutine.
use Tk ':variables'; ... # It handles right-clicks on all entries of the main window $mwindow->bind("Tk::Entry", "<Button-3>", sub {$popup->Popup( -popover => "cursor", -popanchor => 'nw' )} ); # It makes the menu row 'Copy' and calls the sub callback my $popup = $mwindow->Menu( -tearoff => 0, -menuitems => [ ['command' => "~Copy", -command => \&callback] ] ); # It must copy the content of the event's entry to the clipboard, but. +.. sub callback { my $f = $Tk::event->ENTRY; # what instead of ENTRY? which method? +(A, B...) my $g = $f->get(); $mw->clipboardAppend($g); }
Thanks

Replies are listed 'Best First'.
Re: Perl-Tk, how to call the event object (entry)
by lamprecht (Friar) on Aug 09, 2009 at 21:17 UTC
    Hi,

    the first arg passed to the callback is always the widget the binding was triggered on.

    # It handles right-clicks on all entries of the main window $mwindow->bind("Tk::Entry", "<Button-3>", \&process_context_click ); # It makes the menu row 'Copy' and calls the sub callback my $popup = $mwindow->Menu( -tearoff => 0, -menuitems => [ ['command' => "~Copy", -command => \&callback] ] ); # It must copy the content of the event's entry to the clipboard, but. +.. {# private scope my $entry; sub process_context_click{ $entry = shift; $popup->Popup( -popover => "cursor", -popanchor => 'nw' ) } sub callback { my $f = $entry; my $g = $f->get(); $mw->clipboardAppend($g); } }

    Cheers, Christoph
      A lot of thanks, Christoph!

      Good luck!