Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

How can I un-bind a button in Perl Tk

by ghosh123 (Monk)
on Apr 06, 2012 at 15:03 UTC ( #963830=perlquestion: print w/replies, xml ) Need Help??
ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monk, I have the following PERL Tk code. Please tell me how can I un-bind the Button-1 ?

my $obj_A = A->new ; #creating object of my $label = $frame->Label(-anchor =>'nw', -text => 'MyLabel"); $self->{labelName} = $label; $label->bind("<Button-1>", sub { $self->onEnter($label, $obj_A) ; } )

I want to un-bind the Button-1 so that it releases the $obj_A object and its ($obj_A) refcount reduces. Assume $self is object of Can I do something like below ? Will it serve my purpose ?

$label->bind("<Button-1>", undef );

Replies are listed 'Best First'.
Re: How can I un-bind a button in Perl Tk
by zentara (Archbishop) on Apr 06, 2012 at 16:06 UTC
    I don't know how this will work in your object, but this code shows the idea. It might help if you posted a small working example for people to test their ideas on.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow(); # 1. Entry $mw->Label(-text=>'Label 1: ')->pack; my $e1 = $mw->Entry()->pack; # 2. Entry $mw->Label(-text=>'Label 2: ')->pack; my $e2 = $mw->Entry()->pack; # Exit-Button $mw->Button(-text=>'End', -command=>sub {exit} )->pack; # -- Define Bindings -- # -- "Do play a Beep when <Alt-e> ist pressed - except # -- for: focus is on $e1" $mw->bind("<Alt-e>", sub { print "\a"; print "beep\n"; } ); $e1->bind("<Alt-e>", sub { } ); my (@bt) = $e1->bindtags; print "before-> @bt\n"; #In order to change the beep in the entry, without #affecting the $mw beep, you must change the bindtags #for $e1. Remove the toplevel and 'all' specs, or at #least move them to the end of the bind order. $e1->bindtags([$e1, ref($e1)]); # or, depending on your requirements... #$e1->bindtags([$e1, ref($e1), $e1->toplevel, 'all']); (@bt) = $e1->bindtags; print "after-> @bt\n"; MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: How can I un-bind a button in Perl Tk
by Anonymous Monk on Apr 06, 2012 at 23:52 UTC

    Can I do something like below ? Will it serve my purpose ?

    What happens when you try? Yes, that should effectively unbind, and if you're using the latest Tk

    You could always weaken before giving it to Tk

    #!/usr/bin/perl -- use strict; use warnings; use Tk; use Scalar::Util qw/ weaken /; my $mw = tkinit; my $b = $mw->Button->pack; $b->bind( q/<1>/, #~ sub { $self->onEnter($label, $obj_A) ; }, [ sub { warn qq/@_/; my $button = shift; my ( $self, @rest ) = @_; $self->onEnter(@rest); }, weaken( $self ), weaken( $label ), weaken( $obj_A ), ] ); MainLoop; __END__

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963830]
Approved by NetWallah
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-06-24 22:41 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.