2
$\begingroup$

Consider the following snippet of code:

  Module[{pr, ps, var, option},
   option = 1;
   var = 0;
   pr[] := Module[{}, var++; Print[var]];
   ps[] := Print["Test..."];

   PaneSelector[
    {1 ->
      Button["Defaults 1",
      option = 2,
      ImageSize -> 100],
     2 ->
      Button["Defaults 2",
      option = 1;
      pr[],
      ImageSize -> 100]},
     Dynamic[option]]
  ]

Note that pr[] is executed when the button "Defaults 2" is clicked.

Question: I want to execute a function immediately after a Pane is selected in a PaneSelector construction. How should this be implemented?

( Question translated to the context of the above snippet: how to execute either ps[] or pr[] immediately after setting option to 1 or 2?)

$\endgroup$

1 Answer 1

4
$\begingroup$

The following seems to work fine:

DynamicModule[{pr, ps, var = 0, option = 1},
 pr[] := (var++; Print[var]);
 ps[] := Print["Test... Option value is ", option];
 PaneSelector[{1 -> 
    Button["Defaults 1", option = 2, ImageSize -> 100], 
   2 -> Button["Defaults 2", option = 1;
     pr[], ImageSize -> 100]}, 
  Dynamic[Refresh[ps[]; option, TrackedSymbols :> {option}]]]]

It executes ps[] every time option changes. Refresh with TrackedSymbols :> {option} is used to prevent infinite recursion. Note: I also changed Module to DynamicModule.

$\endgroup$
3
  • $\begingroup$ Interesting. I haven't used Refresh or TrackedSymbols before. Will try the code now. $\endgroup$ May 30, 2012 at 19:36
  • $\begingroup$ It does work in a Module too. Is there a compelling reason to use DynamicModule ? $\endgroup$ May 30, 2012 at 19:43
  • $\begingroup$ @ndroock1 There are several good reasons, documented here. $\endgroup$
    – Ajasja
    May 30, 2012 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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