in reply to Re: disable button
in thread disable button

I've tried this, but I'm not sure how to pass $push to the subroutine; specifically in the same statement in which it is being defined. I'm new to local variable usage. -honyok

Replies are listed 'Best First'.
Re^3: disable button
by zentara (Cardinal) on Feb 05, 2009 at 15:48 UTC
    Yeah, it can get tricky. One option is to create the button first, without a command, then after creation, configure it to contain a command, with it's name as a parameter. However, it is probably easiest to use the caller method. This will restore the button after 2 seconds.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = tkinit; my $push= $mw->Button(-text => "test ", -command =>[\&save_parms,$mw,'somedata'] )->pack(); MainLoop; sub save_parms { print "@_\n"; my $caller = $Tk::widget; #disable $push $caller->configure(-state=>'disabled'); #do something my $timer = $mw->after(2000,sub{ $caller->configure(-state=>'normal'); #enable $push }); }

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      I'm not sure how to pass $push to the subroutine; specifically in the same statement in which it is being defined.

      The other alternative is to declare the variable first, before using it, then it will be available throughout the line:

      my $push; $push = $mw->Button( -text => "test ", -command => sub { #disable $push $push->configure(-state=>'disabled'); #do something #enable $push $push->configure(-state=>'normal'); }, )->pack();

      As long as you're not trying to use the value of the variable before it has been set, e.g. on the right-hand side of the assignment. But here, the variable is merely being bound, inside the anonymous subroutine.

      Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.
        jdporter, I think you may be missing the fine point of his question. It's about passing the button itself, in the format [\&callback,$val,'somedata']

        I'm not exceptionally clear on why it has this effect, but look at the following example. Your way still dosn't pass the button object, while the second way does.

        #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = tkinit; my $push; # dosn't work $push = $mw->Button(-text => "test ", -command =>[\&save_parms,$push,'somedata'] )->pack(); # works my $push1 = $mw->Button(-text => "test1 ", )->pack(); $push1->configure( -command =>[\&save_parms,$push1,'somedata']); MainLoop; sub save_parms { print "@_\n"; }

        I'm not really a human, but I play one on earth Remember How Lucky You Are