From: Michael on
On 7/22/2010 2:42 AM, John Fultz wrote:
> [...]
> I didn't look at your original code, but I think I probably know why your use of
> Block and Dynamic together failed. The principal point is that Block introduces
> an environment where variables can be temporarily redefined, but that
> environment evaporates the moment Block is finished evaluating. The contents of
> a Dynamic never even begin evaluating until well after the Shift-Enter
> evaluation is over.
> [...]

Hi again John,

I'm seeing a similar thing happen when I use DynamicModule:

xyz = "A";
f[] := DynamicModule[{tmp = "A"},
CreateDialog[{
PopupMenu[Dynamic[tmp], {"A", "B", "C", "D"}],
PopupMenu[Dynamic[xyz], {"A", "B", "C", "D"}]
}]
];
Button["Test", f[]]

If I click on the "Test" button, I don't see the value of "tmp" being
changed when I pick different options from the pop up menu, but I *do*
see the value of "xyz" being changed when I pick different options from
its pop up menu.

However, if I execute f[] directly, it works for both cases. Shouldn't
clicking the button produce identical results to calling whatever is
defined for its action? There seems to be something related to using a
dynamic module variable, perhaps it loses its definition during a Button
call or something.

> Sincerely,
>
> John Fultz
> jfultz(a)wolfram.com
> User Interface Group
> Wolfram Research, Inc.


Best Regards,

Michael

From: Michael on
On 7/26/2010 11:34 AM, John Fultz wrote:
> On Mon, 26 Jul 2010 11:06:56 -0700, Michael wrote:
>> I'm seeing a similar thing happen when I use DynamicModule:
>>
>> xyz = "A";
>> f[] := DynamicModule[{tmp = "A"},
>> CreateDialog[{
>> PopupMenu[Dynamic[tmp], {"A", "B", "C", "D"}],
>> PopupMenu[Dynamic[xyz], {"A", "B", "C", "D"}]
>> }]
>> ];
>> Button["Test", f[]]
>>
>> If I click on the "Test" button, I don't see the value of "tmp" being
>> changed when I pick different options from the pop up menu, but I *do*
>> see the value of "xyz" being changed when I pick different options from
>> its pop up menu.
>>
>> However, if I execute f[] directly, it works for both cases. Shouldn't
>> clicking the button produce identical results to calling whatever is
>> defined for its action? There seems to be something related to using a
>> dynamic module variable, perhaps it loses its definition during a Button
>> call or something.
>
> You need to read up on DynamicModule wormholes.
> [...]

Sorry, guess I wasn't specific enough about the problem. I don't care
about sharing the value of tmp outside the scope of the dialog box, I
just want the dialog box's popup menu to reflect the value I select.

Turns out the solution was simple and does not involve dynamic
wormholes: I apparently put the DynamicModule in the wrong place:
outside the CreateDialog instead of *inside*. Switching the two gets this:

xyz = "A";
f[] := CreateDialog[
DynamicModule[{tmp = "A"},
Column[{
PopupMenu[Dynamic[tmp], {"A", "B", "C", "D"}],
PopupMenu[Dynamic[xyz], {"A", "B", "C", "D"}]
}]
]];
Button["Test", f[]]

Now both pop-up menus behave the same.


> Sincerely,
>
> John Fultz
> jfultz(a)wolfram.com
> User Interface Group
> Wolfram Research, Inc.


Best Regards,

Michael