2

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?

kornaros
  • 1,047
  • 5
  • 14

2 Answers2

3

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

Nasser
  • 143,286
  • 11
  • 154
  • 359
2

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

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453