I have a dynamic module that contains some basic PopupMenu's and Buttons but also need to use the variables returned from these buttons and PopupMenu's in my nb functions outside of this DynamicModule. Here is the code:
DynamicModule[{var = 1, var2 = 3},
Dynamic[Column[Join[
{PopupMenu[pass2Fun1 = Dynamic[var], {1 -> "Return 1", 2 -> "Return 2",
3 -> "Return 3"}, ContinuousAction -> True]},
Switch[var, 1,
{Button["One", Module[{data = SystemDialogInput["FileOpen"]}, {}],
Method -> "Queued"]}, 2,
{Button["Two", Module[{data2 = SystemDialogInput["FileOpen"]}, {}],
Method -> "Queued"],
Button["Three",Module[{data3 = SystemDialogInput["FileOpen"]}, {}],
Method -> "Queued"]}, 3,
{Button["Four", Module[{data4 = SystemDialogInput["FileOpen"]}, {}],
Method -> "Queued"],
PopupMenu[pass2Fun2 = Dynamic[var2], {3 -> "choice 1", 4 -> "choice 2",
5 -> "choice 3"}, ContinuousAction -> True]}
]]], TrackedSymbols :> {var, var2}]]
pass2Fun1 and pass2Fun2 are variables that are needed outside the Dynamic Module, and of course so are the SystemDialogInput variables.
Take for example the If statement code where we can test to see if the variable pass2Fun1 ==1 To run this test place the above Dynamic Module code in a cell and check to see that the variable pass2Fun1 is filled as Global` Then run the If statement code in another cell to see if you get a good printed result.
If[pass2Fun1 == 1,
Print["This works", pass2Fun1],
Print["Broken", pass2Fun1]]
The If statement can not see the variable pass2Fun1==1 but If I print that variable it prints. If I check the context and the context path both are Global`.
Now for the fun Part: In a separate cell evaluate:
pass2Fun1=1;
Then run the If statement again. Now the code runs fine. I thought we had a solution but further checking revealed this strange behavior. After trying Rojo's code below you will see that this If statement will still not complete. So in both the example code above and Rojo's solution below the result in the If statement test is the same. Rojo's code in the above function:
DynamicModule[{pass2Fun1 = 1, pass2Fun2 = 3},
Dynamic[Column[
Join[{PopupMenu[
Dynamic[pass2Fun1], {1 -> "Return 1", 2 -> "Return 2",
3 -> "Return 3"}, ContinuousAction -> True]},
Switch[pass2Fun1,
1, {Button["One",
Module[{data = SystemDialogInput["FileOpen"]}, {}],
Method -> "Queued"]},
2, {Button["Two",
Module[{data2 = SystemDialogInput["FileOpen"]}, {}],
Method -> "Queued"],
Button["Three",
Module[{data3 = SystemDialogInput["FileOpen"]}, {}],
Method -> "Queued"]},
3, {Button["Four",
Module[{data4 = SystemDialogInput["FileOpen"]}, {}],
Method -> "Queued"],
PopupMenu[
Dynamic[pass2Fun2], {3 -> "choice 1", 4 -> "choice 2",
5 -> "choice 3"}, ContinuousAction -> True]}]]],
TrackedSymbols :> {pass2Fun1, pass2Fun2}]]
My question: What is the best method to get the variable data from another nb a Button or PopupMenu to the functions in a different nb? These nb functions control the generation of results based on the input data and the variables pass2Fun1 & 2 and the SystemDialogInputs. Should all the functions be called inside the DynamicModule or made Global? Even if it's made Global as in the case above, how do I make sure the variable is filled in such a way as to be useful to other functions?
Thanks in advance for the help!
pass2Fun2
and uses numbers 3,4 and 5 for the choices, so you could never expect it to assignpass2Fun1
the number 1 $\endgroup$