Re: Dependent dynamic controls
- To: mathgroup at smc.vnet.net
- Subject: [mg100749] Re: Dependent dynamic controls
- From: István <replicatorzed at gmail.com>
- Date: Fri, 12 Jun 2009 05:47:06 -0400 (EDT)
- References: <h02vfq$863$1@smc.vnet.net> <h0l49i$oq2$1@smc.vnet.net>
Thank you all for your answers, the solution - as I now realize - is
pretty simple, I should have fiddled a bit more with the code.
Thanks again.
Istvan
On Jun 9, 12:37 pm, Albert Retey <a... at gmx-topmail.de> wrote:
> Hi,
>
> I think you can understand what happens when using some extra prints:
>
> Module[{sw = True, a, b, c, n, assign},
>
> assign[switch_, x_] := If[switch,
> a = x; c = f@x; n = Position[aSet, x][[1, 1]],
> b = x; c = g@x; n = Position[bSet, x][[1, 1]]
> ];
>
> assign[sw, First@If[sw, aSet, bSet]];
> Print[{a, b, c, n}];
> Panel@Column@{
> Dynamic@Column@{sw, If[sw, a, b], c, n},
> Grid[{{
> "a",
> RadioButton[Dynamic[sw, (sw = #; assign[#, First@aSet]) =
&],
> True],
> PopupMenu[Dynamic[a, assign[sw, #] &], aSet,
> Enabled -> Dynamic[sw]]
> }, {
> "b",
> RadioButton[Dynamic[sw, (sw = #; assign[#, First@bSet]) =
&],
> False],
> PopupMenu[Dynamic[b, (Print[{sw, #}]; assign[sw, #]) &], b=
Set,
> Enabled -> Dynamic[! sw]]
> }}]
> }
>
> ]
>
> so what we learn is that actually everything works as intended, but then
> in the end the initialization of the PopupMenu calls the setter function
> once because b has no value yet. Whether this is what one should expect
> or not I don't know, maybe you can find something in the documentation.
> It is very easy to correct, though, by initializing b (in case sw is
> initialized to False you might want to also initialize a):
>
> Module[{sw = True, a = aSet[[1]], b = bSet[[1]], c, n, assign},
>
> assign[switch_, x_] := If[switch,
> a = x; c = f@x; n = Position[aSet, x][[1, 1]],
> b = x; c = g@x; n = Position[bSet, x][[1, 1]]
> ];
>
> assign[sw, First@If[sw, aSet, bSet]];
>
> Panel@Column@{
> Dynamic@Column@{sw, If[sw, a, b], c, n},
> Grid[{{
> "a",
> RadioButton[Dynamic[sw, (sw = #; assign[#, First@aSet]) =
&],
> True],
> PopupMenu[Dynamic[a, assign[sw, #] &], aSet,
> Enabled -> Dynamic[sw]]
> }, {
> "b",
> RadioButton[Dynamic[sw, (sw = #; assign[#, First@bSet]) =
&],
> False],
> PopupMenu[Dynamic[b, assign[sw, #] &], bSet,
> Enabled -> Dynamic[! sw]]
> }}]
> }
>
> ]
>
> I have also rearranged some Dynamics since the controls need not be
> recreated every time one of the variables changes.
> Another suggestion would be to use DynamicModule instead of just Module,
> but that depends also on what you really try to achieve. With Module you
> will create a set of variables at each call, and they will persist for
> the rest of the session.
>
> hth,
>
> albert