Home > Archive > PerlTk > September 2005 > bind callbacks with arguments
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
bind callbacks with arguments
|
|
| listmail@triad.rr.com 2005-09-22, 6:57 pm |
| 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 majordomo@lists.stanford.edu
| |
| dgordon@micron.com 2005-09-22, 6:57 pm |
| 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: owner-ptk@lists.Stanford.EDU [mailto:owner-ptk@lists.Stanford.EDU]
On Behalf Of listmail@triad.rr.com
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 majordomo@lists.stanford.edu
-++**==--++**==--++**==--++**==--++**==--++**==--++**==
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 majordomo@lists.stanford.edu
| |
| Ala Qumsieh 2005-09-22, 6:57 pm |
| --- listmail@triad.rr.com wrote:
> 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.
It is a by-product of how Perl/Tk glue logic evaluates
callbacks. In this case, there are two arguments
passed to resize:
$tw (or $bw), 0 (or 1)
one fix:
> sub resize () {
> my $parm=shift;
my $parm = pop;
--Ala
________________________________________
______________
Yahoo! for Good
Donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/
-++**==--++**==--++**==--++**==--++**==--++**==--++**==
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 majordomo@lists.stanford.edu
|
|
|
|
|