Message Boards Message Boards

GROUPS:

Use Button[] with Dynamic[] expressions?

Posted 2 months ago
361 Views
|
2 Replies
|
0 Total Likes
|

Hi, I just don't get my head around this... when I call a Module with a Dynamic expression (i.e. a progress indicator), this works fine unless I call the same code from Button[]. See below sample code... when calling bigFunc[] standalone from the notebook, everything works fine. When it gets called on the push of the Button[], the progress indicator doesn't move, and the progress dialog window also doesn't close.

What causes this? My intention is to call some Modules that process loads of data (thus the progress indicator dialog...) from a window with buttons, as a menu.

Using DynamicModule in any combination doesn't seem to make a difference... I couldn't find any hint anywhere in the documentation... Mathematica 11.3 on MacOS 10.14.3

Any thoughts, help - thank you!

In[42]:= doSomething[i_] := Module[{},
   Speak[progi]; Pause[2]; progi++;
   ];

bigFunc[] := Module[{},
   progi = 1; proglen = 3;

   prognb = 
    CreateDialog[
     Column[{Style[
        "Doing, " <> ToString[proglen] <> " things in total", 
        FontSize -> 12], 
       Row[{ProgressIndicator[Dynamic[progi], {1, proglen}, 
          ImageMargins -> 5], " ", 
         Dynamic[Round@N[100 progi/proglen]], " %"}]}], 
     WindowSize -> Fit];

   Table[doSomething[i], {i, 1, 3}];

   NotebookClose[prognb];
   ];

callFromButton[] := Module[{},
  Button["Start!", bigFunc[]]
  ]

In[45]:= bigFunc[] (** works fine! **)

In[46]:= callFromButton[] (** doesn't work! **)
Attachments:
2 Replies
Posted 2 months ago

I found a workaround, using Manipulate[] and TogglerBar instead of a Button[]...

Manipulate[
If[i == {1}, bigFunc[]; i = 0];
"Hello!"
, {{i, 0, "Select:"}, {1 -> "bigFunc!", 2 -> "Other"}, 
ControlType -> TogglerBar}]

A minimum working example to have progress bar activated in a dialog window would be:

callFromButton[]:=DynamicModule[{nb,x},
    x=0;
     Button["Start!",
     If[x==0,nb=CreateDialog[{ProgressIndicator[Dynamic[x/3]],Dynamic@x},opt]];
      x++;
     If[x>3,NotebookClose[nb];x=0;]
    ]
]

where opt is

In[*]:= opt
Out[*]= WindowMargins->{{0,Automatic},{Automatic,0}}

Use the function and try clicking the button continuously:

callFromButton[]

result

Another good example is on SE

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract