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

I have used TK Spinbox to select different units. when the user changes the unit, my perl script should change the entire number to the 'user selected unit'. so i would like to know what was the unit BEFORE change and what is it CHNAGED to. using this i can convert the numbers to rigth unit. See the code below
$u1 = $mw->Spinbox(-values => [qw/Mil MM Inch/],-exportselection=>STRI +NG, -textvariable=>\$dtm_unit, -width => 5,-command=>\&change_unit ) +->grid(-row=>1,-column=>2 ,-sticky=>"ew");
when this perl script s run all numbers will be in Mil(1/1000 of an inch) when user rolls the spinbox to MM, all numbers should be converted from Mil to Milimeters(MM), when user rolls spinbox again then all numbers in MM should be changed to Inch. likewise if user rolls down the unit to MM then numbers should be converted from Inch to MM. now i can get the CHANGED (New) unit, but am unable to check what was it BEFORE change. is there a way to capture the previous unit in this example? i used spinbox just for convenience. i am open to other TK widgets such as Radiobutton, Optionmenu etc, any idea?

Replies are listed 'Best First'.
Re: Previous Selection in Spinbox
by stiller (Friar) on Apr 28, 2008 at 06:59 UTC
    I'd prefer to always have one internal representation of these numbers, and only change the visual representation on request to the current choice. That way I'd avoid this issue, and also I wouldn't have to worry about accummulating errors.

    To capture the previous value, is a variant of capturing the current value. In sub change_unit you keep the old choice. When entering that sub, you have a new choice in $dtm_unit, and you have the old in your sub, or in a global, and you can recalculate from old to new, and set $old_unit  = $dtm_unit; for use next time.

Re: Previous Selection in Spinbox
by zentara (Cardinal) on Apr 28, 2008 at 14:19 UTC
    Just do your conversions in the -command sub.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Spinbox; #see also JComboBox my $mw = MainWindow->new; $mw->title("Spinner"); $mw->fontCreate('big', -family=>'courier', -weight=>'bold', -size=>int(-18*18/14)); my $dtm_unit = 'm'; my $dtm_old = 'm'; my $sp1 = $mw->Spinbox(-width => 2, -font => 'big', -textvariable=>\$dtm_unit, -command => sub{ &show_set }, -value => ['m','cm','mm','in'])->pack (-side=>'left'); MainLoop; ###########################################################3 sub show_set{ print "@_\n"; print "$dtm_unit\n"; print "old-> $dtm_old\n"; $dtm_old = $dtm_unit; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum