http://www.perlmonks.org?node_id=883375

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

Monks-

I want to add (maybe delete) some items from the default menu that comes up when you click <button-3> in a ptk Text window. How can I do this?

The attached code can be used to bring up this default menu. Run the code and right-click-and-hold in the empty text window. You should see a menu containing File, Edit, Search, View.

You'll notice that I'm trying to use bindtags to figure out how this is bound to the text widget by default, but I don't understand the output.

This seems like a very general question that an FAQ should have, but I've failed to find any useful information with my google searches. I got this code from Mastering Perl/Tk (The bindtags Command), but it doesn't seem to be leading in the right direction. Please point me to anything helpful.

Thanks

-Craig

use strict; use warnings; use Tk; # Create new text window... my $top = MainWindow->new(); # Put in the files text box second... my $txt = $top->Text->pack(-expand=>1, -fill=>'both'); foreach my $tag ($txt->bindtags) { print STDERR "$tag has these bindings:\n"; print STDERR $txt->bind($tag), "\n"; } MainLoop();

Update: Changed references from <button-2> to <button-3>

Replies are listed 'Best First'.
Re: Adding items to default Text widget <button-2> menus?
by Anonymous Monk on Jan 20, 2011 at 18:13 UTC
      Thanks for the pointer here. This helps me with how to add/delete a menu item from an existing menu.

      However, my first problem is how to I get access to the default Text menu? I can't seem to find that anywhere!

      Update: Ah, found it $text->menu()

      This should give me all the pieces I need. Thanks monks!

Re: Adding items to default Text widget <button-3> menus?
by Khen1950fx (Canon) on Jan 20, 2011 at 21:05 UTC
    FYI, bindtags is simple enough. First, look at the doumentation for Tk::bindtags. Then try this script. It bindtags and shows that the tags were indeed bound.
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Text; my $top = MainWindow->new(); my $txt = $top->Text->pack(-expand => 1, -fill => 'both' ); $txt->bindtags([<Button-2>, <ButtonRelease-2>]); if (my @tags = $txt->bindtags) { foreach my $tag(@tags) { print $tag, "\n"; } } MainLoop;
Re: Adding items to default Text widget <button-3> menus?
by zentara (Archbishop) on Jan 21, 2011 at 13:12 UTC
    Does this help?
    #!/usr/bin/perl use Tk; use strict; my $mw=tkinit; my $text=$mw->Text->pack; my $textmenu=$text->menu; #get the menu associated with 'File' my $filemenu=$textmenu->entrycget('File',-menu); #Delete the 'Exit' command $filemenu->delete(0); #Replace it with your own $filemenu->insert(0,'command',-label=>'Press Me', -command=>sub{print "I was pressed\n"}); #Get the menu associated with 'Edit' my $editmenu=$textmenu->entrycget('Edit',-menu); #Delete the 'Cut' command $editmenu->delete('Cut'); MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh