0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – bboczeng Jul 8 '14 at 9:10
  • $\begingroup$ "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? $\endgroup$ – Kuba Jul 8 '14 at 9:16
  • $\begingroup$ @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. $\endgroup$ – bboczeng Jul 8 '14 at 9:18
2
$\begingroup$

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}]])]
$\endgroup$
  • $\begingroup$ Thanks for the answer!. But it shows NULL after completing the progress bar if I add ; after Table to suppress output. Any solution? $\endgroup$ – bboczeng Jul 8 '14 at 9:24
  • $\begingroup$ Your first code works great! without the Null issue. $\endgroup$ – bboczeng Jul 8 '14 at 9:25
  • $\begingroup$ 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 :) $\endgroup$ – Öskå Jul 8 '14 at 9:26
  • $\begingroup$ 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... $\endgroup$ – bboczeng Jul 8 '14 at 9:28
  • $\begingroup$ So to this extent, it is not solving the problem.. $\endgroup$ – bboczeng Jul 8 '14 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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