Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Dependent dynamic controls

  • To: mathgroup at smc.vnet.net
  • Subject: [mg100628] Re: Dependent dynamic controls
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Tue, 9 Jun 2009 06:37:56 -0400 (EDT)
  • References: <h02vfq$863$1@smc.vnet.net> <h0l49i$oq2$1@smc.vnet.net>

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, #]) &], bSet,
        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


  • Prev by Date: Re: directionfields from StreamPlot looks different from solution
  • Next by Date: problem with Sum
  • Previous by thread: Re: Dependent dynamic controls
  • Next by thread: Re: Dependent dynamic controls