kkma has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm new to perl-Tk, and was trying to do something with the Button. I have two commands to execute and it has to be executed separately. In the usual way, I can do it with two subs and two buttons. The question is, can I do it with one Button which will go to first sub on first click and change the text of Button to the second command and after clicking it the second time, will go to the second sub? I've googled and did not get any solution. It will be helpful, if anybody can help me out.

Thanks.

Replies are listed 'Best First'.
Re: Changing the button function
by choroba (Cardinal) on Dec 07, 2013 at 21:05 UTC
    Use the configure method to change a widget:
    #!/usr/bin/perl use warnings; use strict; use Tk; sub quit_button { my $button = ${+shift}; $button->configure(-text => 'Quit', -command => sub { exit }); } sub flip_button { my $button = ${+shift}; $button->configure(-text => 'Press me again', -command => [ \&quit_button, \$button ]); } my $mw = 'MainWindow'->new; my $button; $button = $mw->Button(-text => 'Press me', -command => [ \&flip_button, \$button ], )->pack; MainLoop();
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Changing the button function
by zentara (Cardinal) on Dec 08, 2013 at 16:47 UTC
    Hi, here is another option which might be useful. You basically use 1 callback, but do something different based on the cget('Text') on the button. The $toggle is thrown in there just as an extra in case you need a global flag changed by the button.

    If you think about it, you can have more than 2 options by extending the idea.

    #!/usr/bin/perl use warnings; use strict; use Tk; my $toggle = 0; my $mw = MainWindow->new(-title => "Testing ToggleButton\n"); my $button = $mw->Button(-text => "RUN", -bg => 'white', -command => \&toggle)->pack(-expand=>1, -fill=>'b +oth'); MainLoop; sub toggle{ print $button->cget('-text'),"\n"; if ( $button->cget('-text') eq 'RUN'){ $button->configure(-text => 'STOP', -bg => 'yellow', -activebackground => 'yellow', -highlightbackground => 'yellow', ); print "Do your RUN stuff here\n"; $toggle = 1; } elsif ( $button->cget('-text') eq 'STOP'){ $button->configure(-text => 'RUN', -bg => 'white', -activebackground => 'white', -highlightbackground => 'white', ); print "Do your STOP stuff here\n"; $toggle = 0; } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Changing the button function
by taint (Chaplain) on Dec 07, 2013 at 21:07 UTC

    While I'm not "up" on the Perl Tk bindings. I'm wondering if there was already a Module, or application. That already implements what you're attempting to achieve. If so, maybe examining the source would provide the clues you need. Or at least a start.

    HTH

    --Chris

    EDIT: looks like choroba was providing an example while I was writing this.
    Hey. I'm not completely useless. I can be used as a bad example.
    

      Hey Chris, if you replace the first sentence of your reply with "While I don't know anything about what you are asking." you have a reply for any question ever posted!

        While I'm not familiar with Perl Tk bindings. How does that mean that sourcing other (PerlTK) Modules/applications is any less useful, for finding examples/solutions to the question?

        Thank you for your thought(ful|less) reply.

        Hey. I'm not completely useless. I can be used as a bad example.