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

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

Replies are listed 'Best First'.
Re^5: Tk Buttons
by choroba (Cardinal) on Nov 14, 2015 at 16:39 UTC
    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?