Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Three techniques for Tk button callbacks

by blssu (Pilgrim)
on Nov 11, 2002 at 17:28 UTC ( #211982=note: print w/replies, xml ) Need Help??


in reply to code ref

Here are three examples of different ways to invoke a callback from a Tk button. The first is the simplest and "cleanest", but it uses the most memory. The second is simple and more memory efficient, but it is ugly. The third takes advantage of Tk's OO system and uses the least amount of memory, but it is inflexible if improperly designed.

use strict; use Tk; use Tk::FooButton; my $MW = new MainWindow; # 1. callback using anonymous sub (also known as a # "closure") -- the arguments are embedded in # the closure. my $a_obj = new Foo (1); my $a = $MW->Button(-text => 'A', -command => sub { $a_obj->callback("pushed 'A'") } ) ->pack; # 2. callback using sub reference (looked up using # Perl's object-oriented "can" method) -- the # arguments are passed through the array reference. my $b_obj = new Foo (2); my $b = $MW->Button(-text => 'B', -command => [ $b_obj->can('callback'), $b_obj, "pushed 'B'" ] ) ->pack; # 3. callback using a custom (derived) Tk::Button -- # the arguments are stored as properties on the # button itself. my $c_obj = new Foo (3); my $c = $MW->FooButton(-text => 'C', -object => $c_obj, -message => "pushed 'C'") ->pack; MainLoop; # --------------------------------------------------------- package Foo; use strict; sub new { my ($class, $i) = @_; my $self = { -id => $i }; return bless($self, $class) } sub callback { my ($self, $message) = @_; print "$message using object #$self->{-id}\n"; } # --------------------------------------------------------- # SAVE THIS CODE AS "Tk/FooButton.pm" package Tk::FooButton; use strict; use base qw(Tk::Derived Tk::Button); Construct Tk::Widget 'FooButton'; sub Populate { my ($self, $args) = @_; $self->SUPER::Populate($args); $self->ConfigSpecs(-object => [ "PASSIVE", undef, undef, undef ], -message => [ "PASSIVE", undef, undef, undef ]) } sub invoke { my ($self) = @_; my $object = $self->cget(-object); my $message = $self->cget(-message); $object->callback($message) if ($object) } 1;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://211982]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2021-10-26 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (90 votes). Check out past polls.

    Notices?