7

I have a DynamicModule in which I which I wish to start an operation (actually import files) with a button. I would like to monitor the progress with a ProgressIndicator but can't get it to work. This simple example works:

Monitor[
Table[Pause[0.1]; n, {n, 1, 100}],
Dynamic[ProgressIndicator[n, {1, 100}]]
];

I find it interesting that it works even with the final semi-colon. In a DynamicModule this also works:

ClearAll[test];
test[] := DynamicModule[{},
 Row[{Monitor[
   Table[Pause[0.1]; n, {n, 1, 100}],
  Dynamic[ProgressIndicator[n, {1, 100}]]
  ];}]
  ]

test[]

Now I try to put in a button to start and I am lost

ClearAll[test1];
test1[] := DynamicModule[{},
  Row[{Button["Start", Monitor[
   Table[Pause[0.1]; n, {n, 1, 100}],
   Dynamic[ProgressIndicator[n, {1, 100}]]
   ];]}]
  ]   

test1[]

I have tried several alternatives with no success. I would like the progress indicator to appear in the same row as the button once it has been pressed.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Hugh
  • 16,387
  • 3
  • 31
  • 83
  • I do not understand what you are doing, but try Method -> "Queued" on the Button and see if this helps – Nasser Mar 17 '15 at 11:25

2 Answers2

7

With small modifications of the code provided by m_goldberg you can get the Button and the ProgressIndicator in the same Row. However, it is always there now and will not appear and disappear.

DynamicModule[{n = 1}, 
 Row[{Button["Start", n = 1;
  Do[Pause[0.1]; ++n, {i, 1, 100}], Method -> "Queued"], Spacer[23],
  Dynamic[ProgressIndicator[n, {1, 100}]]}]]

output

In order to get an appearing and disappearing ProgressIndicator you can use

DynamicModule[{n = 0}, 
  Row[{Button["Start", n = 1;
    Do[Pause[0.1]; ++n, {i, 1, 100}], Method -> "Queued"], Spacer[23],
    Dynamic@If[n == 0 || n == 101, "", ProgressIndicator[n, {1, 100}]]}]]

gif

Karsten7
  • 27,448
  • 5
  • 73
  • 134
4

The closest I can get to what you ask for is

DynamicModule[{n},
  Button["Start",
    n = 0;
    Monitor[Do[Pause[0.1]; ++n, {i, 1, 100}], 
      Dynamic[ProgressIndicator[n, {1, 100}]]],
    Method -> "Queued"]]

which, after the button is clicked on, produces

progress

The progress indicator appears in its own temporary cell, not in a row with the button. This is how Monitor behaves, and I don't know how to change that behavior.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • Thanks -it works. As you say it appears in a temporary cell. Perhaps I will have to give up my hope of having it in the same row as the button. However significant progress from where I was which is good. – Hugh Mar 17 '15 at 14:25