0

I've recently struggling with Buttons. So I understand Buttons have the form Button["text",action]. Now I want the action to be some sort to function defined to show a progress bar, so I can achieve the effect of showing a progress bar after pressing the button, inside a 'Panel' of DynamicModule for example. (One answer pointed below can display the progress bar but it is displayed after DynamicModule, making it invisible if the content of DynamicModule is a full page)

Here is the simple code I have:

DynamicModule[{i = 0}, 
  Button["Show Bar", i++; SomeFunction[]]]

and the definition of some function:

SomeFunction[] := Monitor[Table[1; Pause[0.1], {j, 1, 10}], 
  ProgressIndicator[j, {0, 11}]]

If I run SomeFunction[] by itself, the progress bar shows nicely. If I click the button, though, I didn't see any thing. Any idea why? (Because I didn't specify how to display the output of the function?).

Thanks.

bboczeng
  • 449
  • 5
  • 9
  • I found that by using a global variable g that records the progress and putting Dynamic[ProgressIndicator[g,{1,100}]] inside the DynamicModule is working... But this is not a good workaround if you have many , many such progress bars. And the problem is the progress bar will always be there. – bboczeng Jul 08 '14 at 09:10
  • "If I run SomeFunction[] by itself, the progress bar shows nicely...". It does not for me. But what is the goal? You wan to print ProgressIndicator each time? Where do you wan to print it? – Kuba Jul 08 '14 at 09:16
  • @kuba it works for me. Maybe you should try SomeFunction[]. The goal is to display a progress bar while the calculation is in progress. so the user will understand his computer is not freezing .After the calculation is done, the progress bar is removed and I have other actions to show. – bboczeng Jul 08 '14 at 09:18

1 Answers1

2

You need to use Method -> "Queued":

SomeFunction[] := Monitor[Table[1; Pause@.1, {j, 1, 10}], ProgressIndicator[j, {0, 11}]]
DynamicModule[{i = 0}, Button["Show Bar", i++; SomeFunction[], Method -> "Queued"]]

or the following would be better in a concrete example:

DynamicModule[{i = 0, SomeFunction, something = "hello"}, 
  Panel@Column@{Button["Show Bar", i++; something = SomeFunction[];, 
    Method -> "Queued"], Dynamic@something}, 
  Initialization :> (SomeFunction[] := 
    Monitor[Table[1; Pause@.1, {j, 1, 10}], ProgressIndicator[j, {0, 11}]])]
Öskå
  • 8,587
  • 4
  • 30
  • 49
  • Thanks for the answer!. But it shows NULL after completing the progress bar if I add ; after Table to suppress output. Any solution? – bboczeng Jul 08 '14 at 09:24
  • Your first code works great! without the Null issue. – bboczeng Jul 08 '14 at 09:25
  • Well, the Null issue has nothing to do with the first issue :) I just added the second example because it's a better habit to define everything inside DynamicModule :) – Öskå Jul 08 '14 at 09:26
  • Thanks. But actually, this progress bar is not displayed within the Panel. It is displaying, but after the Panel.. This makes it not useful for a dynamic module that displays information inside a region... – bboczeng Jul 08 '14 at 09:28
  • So to this extent, it is not solving the problem.. – bboczeng Jul 08 '14 at 09:28
  • @bboczeng Then you should specify your problem :) The first part of this answer answers your question above, now if you want everything properly set inside the DynamicModule it's an other story :) – Öskå Jul 08 '14 at 09:30
  • OK.. I should make it clearer! – bboczeng Jul 08 '14 at 09:30