RE: bind callbacks with arguments

[email protected]
Newsgroups gmane.comp.lang.perl.tk
Message-ID <[email protected]>
Tk supplies the widget reference as the first argument passed to your
callback, and your argument(s) will be next in line:

sub resize () {
        my $widget=shift;
        my $parm=shift;

        print "WIDGET: $widget\n";
        print "PARM: $parm\n";
}

	-- Dave

-----Original Message-----
From: [email protected] [mailto:[email protected]]
On Behalf Of [email protected]
Sent: Thursday, September 22, 2005 9:40 AM
To: Perl TK Mailing List
Subject: bind callbacks with arguments

Sorry to beat this to death, but I do have a solution to resizing 
widgets by using GeometryRequest. Right now a fundamental problem has 
cropped up with the callback in the bind definition.  For some reason 
when I shift the parameters to the variable $parm, $parm becomes the 
HASH for the actual Text widget and not the value 0 or 1 as I believe I 
have defined it.  Please run this scripts and double click one of the 
text widgets and review the print output.  I really dont know how this 
is possible.  


#Script
use strict;
use warnings;
use Tk;
use Tk::Adjuster;

my $mw = MainWindow->new(-title => "Adjuster example");

my $frame1=$mw->Frame();
$frame1->pack();

my $topw = $frame1->pack(-side => 'top', -fill => 'x');
my $botw = $frame1->pack(-side => 'bottom', -fill => 'both', -expand => 
1);

my $tw = $topw->Scrolled('Text', -scrollbars => 'osoe', -height => 10)-
>pack(-expand => 1, -fill => 'both');
my $adj1 = $mw->Adjuster();
$adj1->packAfter($tw, -side => 'top');
my $bw = $botw->Scrolled('Text', -scrollbars => 'osoe', -height => 10)-
>pack(-expand => 1, -fill => 'both');

#here value of 0 or 1 is passed when doubleclicking the Text widget
$tw->bind('<Double-Button-1>', [ \&resize, 0 ]);
$bw->bind('<Double-Button-1>', [ \&resize, 1 ]);

MainLoop();

sub resize () {
        my $parm=shift;

        #after double-clicking either Text widget for some reason $parm 
is a HASH pointing to the widget and not 
        #a value of 0 or 1 ???
        print "PARM: $parm\n";
}
#
#Output is as follows (clicked both widgets)
#PARM: Tk::Text=HASH(0x315ddbc)
#PARM: Tk::Text=HASH(0x32c3964)
-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ptk" to [email protected]

-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ptk" to [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.