Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Adding items to default Text widget <button-3> menus?

by cmv (Chaplain)
on Jan 20, 2011 at 17:55 UTC ( #883375=perlquestion: print w/ replies, xml ) Need Help??
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>

Comment on Adding items to default Text widget <button-3> menus?
Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://883375]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2015-07-05 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls