Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Perl Tk Scrolled widget callbacks

by ghosh123 (Monk)
on Mar 01, 2013 at 09:28 UTC ( #1021198=perlquestion: print w/ replies, xml ) Need Help??
ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I have a scrolled widget in my Perl Tk code. How can I introduce a callback just to check the -wrap value of it and take some action accordingly.

Following is just a snippet of my code :

use warnings; use strict; use Tk; my $mw = new MainWindow; $mw->geometry("200x200"); my $frame = $mw->Frame()->pack; my $textbox = $frame->Scrolled('Text', -scrollbars => 'soe', -foreground => 'blue', -background => 'white', -wrap => 'none', )->pack; $textbox->insert('end', "This is scrolled widget.\nThis is scrolled wi +dget.This is scrolled widget"); MainLoop;


I want the callback to be called when I click RightMouseButton > View > Wrap on the scrolled frame and change the Wrap to something.
Please help. Thanks.
My callback should be like below

sub func { my $wr = $textbox->Subwidget('scrolled')->cget(-wrap); if ($wr =~ /\bnone\b/) { # do something } else { # do something else }

Comment on Perl Tk Scrolled widget callbacks
Select or Download Code
Re: Perl Tk Scrolled widget callbacks
by zentara (Archbishop) on Mar 01, 2013 at 12:38 UTC
    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

      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
Re: Perl Tk Scrolled widget callbacks
by thundergnat (Deacon) on Mar 01, 2013 at 16:56 UTC

    It should be possible to hook into the <<MenuSelect>> virtual event bound to the popup menu but I am having a hard time figuring out how to do so...

    Another less elegant way is to just poll the state of the -wrap option and do your callback when it changes.

    use warnings; use strict; use Tk; my $mw = new MainWindow; $mw->geometry("200x200"); my $frame = $mw->Frame()->pack; my $textbox = $frame->Scrolled('Text', -scrollbars => 'soe', -foreground => 'blue', -background => 'white', -wrap => 'none', )->pack; $textbox->insert('end', "This is scrolled widget.\nThis is scrolled wi +dget.This is scrolled widget"); my $wrapmonitor = $textbox->cget('wrap'); $textbox->repeat(100, sub { if ( $textbox->cget('wrap') ne $wrapmonitor ){ $wrapmonitor = $textbox->cget('wrap'); mycallback($wrapmonitor) #whatever } } ); MainLoop; sub mycallback { print shift, "\n"; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-09-19 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls