1
$\begingroup$

Consider the following as an example:

DynamicModule[{
    $Return
},
    Manipulate[
        $Return=$Dollars+$Quarters/4;(*first line*)
        Row[{"Value of $Return that will be used: ",DecimalForm[N@$Return,{16,2}]}],
        
        {{$Return,10,"Return"},3/4,20,1/4},
        {{$Dollars,10,"Dollars"},0,20,1,ControlType->Setter},
        {{$Quarters,0,"Quarters"},0,3,1,ControlType->Setter},
        
        ControlPlacement->Top
    ]
]

This produces an interface where I can specify the value of $Return that can be changed via the Setters and the Slider adjusts accordingly.

If I change the (*first line*) in Manipulate to

{$Dollars,$Quarters}={1,4}*MixedFractionParts@$Return;

Then the Slider gives the $Return value and the Setters adjust accordingly.

Is there a way to make this Manipulate have both the Slider and the Setters be a way to specify the $Return value and the others adjusting accordingly?

One more thing, lets say I want to restrict $Return to be in the range from 3/4 to 20 which means if the $Dollars Setter is set to 0 then the $Quarters Setter should grey out the options 0, 1 and 2 and similarly for 20 the options 1, 2 and 3.

This is just a toy example with example numbers so if the mechanism is general then that would be very helpful.

$\endgroup$

2 Answers 2

2
$\begingroup$

Use the option TrackingFunction in each control to dynamically change the other two control values.

Define three tracking functions for the three control variables:

trackingFunc$R = ($Return = Round[#, 1/4];
    {$Dollars, $Quarters} = {1, 4} MixedFractionParts@#;) &;

trackingFunc$D = ($Dollars = #;
    $Return = Min[20, Max[# + $Quarters/4, 3/4]];
    $Quarters = If[# == 20, 0, If[# == 0, 3, $Quarters]];) &;

trackingFunc$Q = ($Quarters = #;
    $Return = Min[20, Max[$Dollars + #/4, 3/4]];) &;

Use these as the option values for TrackingFunction in controls:

Manipulate[
 Row[{"Value of $Return that will be used: ", 
      DecimalForm[N @ $Return, {16, 2}]}],
 {{$Return, 10, "Return"},
   Manipulator[#, {3/4., 20., 1./4}] &,
   Appearance -> {"Labeled", "Open"},
   TrackingFunction -> trackingFunc$R,  
   DefaultOptions ->
     {Slider -> {ImageMargins -> 5},
       InputField -> {Alignment -> {Left, Center}, ImageSize -> {50, 30}}}},
 {{$Dollars, 10, "Dollars"}, 0, 20, 1,
    Setter,
    TrackingFunction -> trackingFunc$D},
 {{$Quarters, 0, "Quarters"}, 
    If[$Return == 3/4 || $Dollars == 0, {3}, 
       If[$Return == 20 || $Dollars == 20, {0}, Range[0, 3]]],
    SetterBar,
    TrackingFunction -> trackingFunc$Q},
 ControlPlacement -> Top]

enter image description here

$\endgroup$
2
  • $\begingroup$ How are you recording this and the mouse movement? $\endgroup$ Commented Dec 8, 2021 at 18:06
  • 1
    $\begingroup$ @morbo, I am using the app ScreenToGif $\endgroup$
    – kglr
    Commented Dec 8, 2021 at 18:27
0
$\begingroup$
Clear["Global`*"]

$Temp = 10.0;

Manipulate[
 If[$Temp == $Return,
  $Return = $Dollars + $Quarters/4,
  ($Dollars = IntegerPart[$Return];
   $Quarters = 4 ($Return - $Dollars))];
 $Temp = $Return;
 Row[{"Value of Return that will be used: ", 
   DecimalForm[$Return // N, {4, 2}]}],
 {{$Return, 10, "Return"}, 0, 20, 1/4},
 {{$Dollars, 10, "Dollars"}, 0, 20, 1, ControlType -> Setter},
 {{$Quarters, 0, "Quarters"}, 0, 3, 1, ControlType -> Setter},
 ControlPlacement -> Top]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.