Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Perl Tk Scrolled widget callbacks

by zentara (Archbishop)
on Mar 01, 2013 at 12:38 UTC ( #1021240=note: print w/ replies, xml ) Need Help??


in reply to Perl Tk Scrolled widget callbacks

The problem is that the Text widget has default bindings to a Popup menu when you right click. You can either make a custom derived Text widget to remove the Popup menu, and replace it with your code, or you can let the Popup do the work. See Making a derived Tk::Text object for how to make a custom derived Tk::Text widget.

To let the Popup do the work, try something like this:

#!/usr/bin/perl use Tk; my $mw = MainWindow->new; my $txt = $mw->Text()->pack(-side => 'top'); my $number = 1; my $letter = 'a'; my $popup_menu = ""; my $new_menu = $mw->Menu(); $new_menu->add('command', -label => "Click me to show the pop-up menu.", -command => sub { $popup_menu = $mw->Menu(); $popup_menu->add('command', -label => "Number: $number", -command => sub { print STDERR "Number: $number\n"; $number++; } ); $popup_menu->add('command', -label => "Letter: $letter", -command => sub { print STDERR "Letter: $letter\n"; $letter++; } ); $popup_menu->Popup(qw/-popover cursor/); } ); $txt->menu( $new_menu ); MainLoop;

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


Comment on Re: Perl Tk Scrolled widget callbacks
Download Code
Replies are listed 'Best First'.
Re^2: Perl Tk Scrolled widget callbacks
by ghosh123 (Monk) on Mar 01, 2013 at 13:00 UTC

    Hi
    My problem is that, I have to find a solution with this Scrolled text area only. This custom "popup do the work" is wonderful but I have to fit it into my code which is using Scrolled widget. Changing the 'Scrolled' thing is not an option for me

      The example I gave above works with a Scrolled Text widget as well. Just change the line
      my $txt = $mw->Text()->pack(-side => 'top'); #to my $txt = $mw->Scrolled('Text')->pack(-side => 'top');
      Another option is to bind mouse button 3 ( right click ) to the $mw to do your change. Supressing the Text menu is up to you.
      #!/usr/bin/perl use Tk; my $mw = MainWindow->new; my $txt = $mw->Scrolled('Text')->pack(-side => 'top'); $mw->bind('<Button-3>', sub { print "main control 3 \n" }); 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: note [id://1021240]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (17)
As of 2015-07-29 14:45 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 (263 votes), past polls