in reply to Re^2: Tk Buttons
in thread Tk Buttons

Hello PilotinControl,

In the snippet you’ve posted, I can’t see any code to count or reset a value. Please provide a minimal but complete example that reproduces the problem. See How do I post a question effectively? and the SSCCE.

Cheers,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^4: Tk Buttons
by PilotinControl (Pilgrim) on Nov 14, 2015 at 16:03 UTC

    It has been figured out....I needed to add another Bind function in order to set the opposite button back to speed stop 0....otherwise when pressed again it would continue onto the next sub routine in line

      That's weird. Why are you changing the binding? You should just change the speed. Here's how I understood your description:
      #!/usr/bin/perl use warnings; use strict; use Tk; { my $speed = 0; sub change_speed { my $diff = shift; my $state = $speed <=> 0; if (! $state || $state == ($diff <=> 0)) { $speed += $diff if abs($speed + $diff) <= 100; } else { $speed = 0; } } sub create_label { my $parent = shift; return $parent->Label(-textvariable => \$speed, -relief => 'sunken', )->pack(-ipadx => 5) } } my $mw = 'MainWindow'->new(-title => 'Motor'); my $mf = $mw->Frame->pack; $mf->Label(-text => 'Tk Motor Controller')->pack; my $l = create_label($mf); my $bf = $mf->Frame->pack; $bf->Button(-text => '<', -command => [ \&change_speed, -10 ], )->pack(-side => 'left'); $bf->Button(-text => '>', -command => [ \&change_speed, 10 ], )->pack(-side => 'right'); my $qb = $mf->Button(-text => 'Quit', -underline => 0, -command => sub { Tk::exit() }, )->pack( -pady => 10); $mw->bind('<Alt-q>', sub { $qb->invoke }); MainLoop();

      Update: Used closure over the $speed variable.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        That works great and that is what I was after...now each time the button is clicked it is suppose to increase and decrease the speed...do I just add the motor command....via serial port....to the &change_speed sub routine?