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

Good Evening Monks!

I have 2 Perl Tk buttons side by side one labeled Left and one Labeled Right and each time one is pressed its text label is incremented by 10s all the way to 100. When you press the opposite button the count on the opposite button is reset to 0. Once you press that button again it starts counting where it left off when it should be reset to 0 to start counting by 10s again. My snippet of code is posted below. I will only post the sub routines responsible for counting...not the Tk code for the buttons since that should be understood.

sub leftthrottle { $button33 = $rows[$rzero]->Button(-text=>'L-FWD', -width => '3', -he +ight => "$h")->pack(-expand => 1, -fill => 'both', -padx => 2, -pad +y => 2, -side => 'left', -ipadx => 5, -ipady => 5); $button33->configure(-background => "gray", -foreground => "black"); $balloon->attach($button33, -balloonmsg => "Left Forward Speed"); $button33->bind('<ButtonPress>', \&leftfwdspeedone); $button34 = $rows[$rzero]->Button(-text=>'L-REV', -width => '3', -he +ight => "$h")->pack(-expand => 1, -fill => 'both', -padx => 2, -pad +y => 2, -side => 'left', -ipadx => 5, -ipady => 5); $button34->configure(-background => "gray", -foreground => "black"); $balloon->attach($button34, -balloonmsg => "Left Reverse Speed"); $button34->bind('<ButtonPress>', \&leftrevspeedone); } sub leftfwdspeedone { $button34->configure(-text=>'L REV 0', -background => "gray", -foregro +und => "black"); $balloon->attach($button34, -balloonmsg => "Left Reverse Speed 0"); $button33->configure(-text=>'L FWD 0', -background => "gray", -foregro +und => "black"); $balloon->attach($button33, -balloonmsg => "Left Forward Speed 0"); $button33->bind('<ButtonPress>', \&leftfwdspeedtwo); } # END SUB sub leftfwdspeedtwo { $button34->configure(-text=>'L REV 0', -background => "gray", -foregro +und => "black"); $balloon->attach($button34, -balloonmsg => "Left Reverse Speed 0"); $button33->configure(-text=>'L FWD 10', -background => "gray", -foregr +ound => "black"); $balloon->attach($button33, -balloonmsg => "Left Forward Speed 10"); $button33->bind('<ButtonPress>', \&leftfwdspeedthree); } sub leftrevspeedone { $button33->configure(-text=>'L FWD 0', -background => "gray", -foregro +und => "black"); $balloon->attach($button33, -balloonmsg => "Left Reverse Speed 0"); $button34->configure(-text=>'L REV 0', -background => "gray", -foregro +und => "black"); $balloon->attach($button34, -balloonmsg => "Left Reverse Speed 0"); $button34->bind('<ButtonPress>', \&leftrevspeedtwo); } # END SUB sub leftrevspeedtwo { $button33->configure(-text=>'L FWD 0', -background => "gray", -foregro +und => "black"); $balloon->attach($button33, -balloonmsg => "Left Reverse Speed 0"); $button34->configure(-text=>'L REV 10', -background => "gray", -foregr +ound => "black"); $balloon->attach($button34, -balloonmsg => "Left Reverse Speed 10"); $button34->bind('<ButtonPress>', \&leftrevspeedthree); } # END SUB sub leftrevspeedthree { $button33->configure(-text=>'L FWD 0', -background => "gray", -foregro +und => "black"); $balloon->attach($button33, -balloonmsg => "Left Reverse Speed 0"); $button34->configure(-text=>'L REV 20', -background => "gray", -foregr +ound => "black"); $balloon->attach($button34, -balloonmsg => "Left Reverse Speed 20"); $button34->bind('<ButtonPress>', \&leftrevspeedfour); } # END SUB

Thanks in advance!

Replies are listed 'Best First'.
Re: Tk Buttons
by Anonymous Monk on Nov 14, 2015 at 04:02 UTC

    no question to be seen ... again ... Thanks in advance!

    So what is your question?

      The question is right here: Why is it that when you press that button again it starts counting where it left off when it should be reset to 0 to start counting by 10s again. Thanks.

        And to further explain, the buttons, when pressed control the speed and direction of a motor connected to the laptop via a usb cable using the Win32::SerialPort module....that part works. What is happening when the direction button is pressed it gets faster in the forward direction and when pressed again it gets faster and faster...and when the reverse button is pressed the motor stops and proceeds to going in the reverse direction and each time that button is pressed it gets faster and faster....here is what is happening....if the forward button is pressed again....instead of starting at speed 0....it picks up where the last forward button press left off...lets say it stopped at 40 when we first pressed the reverse button....when the forward button is pressed again it goes to speed 50 instead of starting back at zero....same holds true if the reverse button was pressed again.