2
$\begingroup$

I want to have some bottons inside manipulate to calculate some time consuming functions. For example the following code changes its color after the calculation of Total[Range[123456000]] is finished. I tried

Dynamic@Button["xxx", Clear[x1]; x1 = 0; FinishDynamic[]; 
  Total[Range[123456000]]; 
  x1 = 1/2, Background -> Dynamic[Hue[x1]]]

with success but when I want to localize the Dynamic variable x1 inside Manipulate(or DynamicModule) there is no result! For example, the following code does not work for me!

DynamicModule[{x1}, 
 Button["xxx", Clear[x1]; x1 = 0; FinishDynamic[]; 
 Total[Range[12345600]]; x1 = 1/2, 
  Background -> Dynamic[Hue[x1]]]]

Any ideas please?

$\endgroup$
2

2 Answers 2

3
$\begingroup$

Here is a version using Manipulate. The color is set when the button is hit, and reset inside the Manipulate expression when the computation is done.

Manipulate[
 tick;
 Total[Range[123456000]];
 color = Green;
 Date[],

 Button["Run", color = Red; tick = Not[tick], Background -> Dynamic@color],

 {{color, Green}, None},
 {{tick, False}, None},
 TrackedSymbols :> {tick}
 ]

enter image description here

$\endgroup$
2
$\begingroup$

For example:

DynamicModule[{x1 = False, bkg = Gray, p = 0}, 
  DynamicWrapper[
      Dynamic@Row[{Button["xxx", x1 = Not[x1], Background -> bkg], p}],
      If[x1, (p = Total[Range[100]]; bkg = Green)]]]

Mathematica graphics ----> Mathematica graphics

$\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.