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

Good Evening Monks!

I've posted a snippet of code that I need a Tk Button each time that it is pressed to do a different function. In this instance F0 button each time it is pressed it will turn ON the lights and then turn OFF the lights. The serial port code works....the button code does not reflect that. Thanks for pointing me in the right direction.

sub btnpress { my $btn = shift; if($btn eq 'C') { $display = ''; $buffer = ''; $dbuffer = ''; } if( $btn =~/F0/ ) { $display = sprintf "0 - Lights On", $dbuffer; $mw->bind('<ButtonPress-1>', \&one); } else { display($btn); } } sub one { $display = sprintf "0 - Lights Off", $dbuffer; }

Replies are listed 'Best First'.
Re: Tk Button Press
by SuicideJunkie (Vicar) on Nov 02, 2015 at 15:39 UTC

    The first thing to do is decide exactly what you want to have happen. From the code it isn't clear.

    I'm pretty sure you don't want to bind all left clicks everywhere. You probably want to set just the button's command to run a function whenever the button is triggered by any of the normal means not just clicking.

    Your function should query the state of the lights if possible, and set them to the opposite state. Otherwise, you'd have to store the previously requested state and request the other state blindly.

Re: Tk Button Press (more global variables)
by Anonymous Monk on Nov 02, 2015 at 07:53 UTC

    You know how you have all these global variables?

    Why don't you store the next function to be called in one of those?

    Yeah, that sounds good, more global variables, that will solve it