From: Fred Simons on
Rarely, Dynamic does not display the current value. This is the simplest
example I know:

n=0;
Dynamic[n]
Do[Pause[0.2],{n,1,5}]

Recently, I found another example. It has to do a lot with DynamicModule
and the option InheritScope, and I highly appreciate the contributions
of John Fultz in this group. Without his explanations and references I
think I would have been unable to construct this almost working example.

CreateDocument[
DynamicModule[{a=0.5}, Button["Start",
CreateDocument[{DynamicModule[{},Column[{Slider[Dynamic[a]],
Dynamic[a]}], InheritScope->True],
DynamicModule[{b},Button["Secondary
screen",b=a;CreateDocument[{Slider[Dynamic[b]], Dynamic[b]},
WindowTitle->"secondary screen",
NotebookEventActions->{"WindowClose":>(a=b)}]], InheritScope->True],
DynamicModule[{},Button["Print a",Print[a]], InheritScope->True]},
Saveable->False, WindowTitle->"main screen", WindowSize->Scaled[1.0]]] ],
WindowTitle->"start screen"];

In this example, there is a main screen, in which I can manipulate the
variable a. There is a secondary screen, in which I also want to
manipulate the variable a, but in such a way that there is no effect in
the main screen. This is done by using a local variable b, initially set
to a, that can be manipulated in the secondary screen, and on closing
the secondary screen, the variable a is set to b.

This almost works. Evaluate the command, press Start, use the slider,
press Secondary screen, use the slider for manipulating b and close the
secondary screen. Everything works fine, apart from the fact that the
slider and Dynamic[a] in the main screen now do not display the latest
value for b. Nevertheless, thecurrent value of a IS the latest value of
b, as can be seen by pressing 'Print a'. So it is only the display in
the main screen that is incorrect.

There is a trivial workaround for these problems: simply add the option
UpdateInterval->0.5 to the Dynamic commands in the main screen. But that
is very unelegant and my feeling is that, in particular in the second
example, there must be a better way.

Any comment is welcome.

Fred Simons
Eindhoven University of Technology

From: David Park on

Hmm, interesting. I'm not exactly certain what you are observing, but for me
the following DOES update the display of n from the Do statement.

n = 0;
Dynamic[n]
Do[Pause[0.2], {n, 1, 5}]

But then the display is not reset with:

n = 0;

I'm not certain why the Do statement updates the value because n is not
being explicitly set by the Do. The following statement also updates the
value with the Do:

n = 0;
Dynamic[n]
Do[Pause[0.2]; n = i, {i, 1, 5}]

And the displayed value now also updates with:

n = 0;

So, maybe some guru familiar with the internals of Mathematica can explain.


David Park
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/


From: Fred Simons [mailto:f.h.simons(a)tue.nl]

Rarely, Dynamic does not display the current value. This is the simplest
example I know:

n=0;
Dynamic[n]
Do[Pause[0.2],{n,1,5}]


Fred Simons
Eindhoven University of Technology



From: Leonid Shifrin on
Hi Fred,

The following modification will work:

CreateDocument[DynamicModule[{a = 0.5, f},
f[x_] := x;
Button["Start",
CreateDocument[{
DynamicModule[{},
Column[{Slider[Dynamic[f[a]]], Dynamic[f[a]]}],
InheritScope -> True],
DynamicModule[{b},
Button["Secondary screen",
b = a; CreateDocument[{
Slider[Dynamic[b]], Dynamic[b]},
WindowTitle -> "secondary screen",
NotebookEventActions -> {"WindowClose" :> (a = b)}]],
InheritScope -> True],
DynamicModule[{},
Button["Print a", Print[a]], InheritScope -> True]},
Saveable -> False, WindowTitle -> "main screen",
WindowSize -> Scaled[1.0]]]], WindowTitle -> "start screen"];

By using the idle function <f>, we fool Dynamic. This is a bug that has been
discussed very recently,
where John Fultz has unofficially "endorsed" this kind of workarounds until
the bug is fixed, for anyone who
finds that they work for him / her (John, sorry if I misunderstood). There
seems to be no track of this discussion on Google newsgroup or Wolfram
forum, but I will forward the entire discussion to you in a separate
message.

Best,
Leonid




On Mon, May 31, 2010 at 7:44 AM, Fred Simons <f.h.simons(a)tue.nl> wrote:

> Rarely, Dynamic does not display the current value. This is the simplest
> example I know:
>
> n=0;
> Dynamic[n]
> Do[Pause[0.2],{n,1,5}]
>
> Recently, I found another example. It has to do a lot with DynamicModule
> and the option InheritScope, and I highly appreciate the contributions
> of John Fultz in this group. Without his explanations and references I
> think I would have been unable to construct this almost working example.
>
> CreateDocument[
> DynamicModule[{a=0.5}, Button["Start",
> CreateDocument[{DynamicModule[{},Column[{Slider[Dynamic[a]],
> Dynamic[a]}], InheritScope->True],
> DynamicModule[{b},Button["Secondary
> screen",b=a;CreateDocument[{Slider[Dynamic[b]], Dynamic[b]},
> WindowTitle->"secondary screen",
> NotebookEventActions->{"WindowClose":>(a=b)}]], InheritScope->True],
> DynamicModule[{},Button["Print a",Print[a]], InheritScope->True]},
> Saveable->False, WindowTitle->"main screen", WindowSize->Scaled[1.0]]] ],
> WindowTitle->"start screen"];
>
> In this example, there is a main screen, in which I can manipulate the
> variable a. There is a secondary screen, in which I also want to
> manipulate the variable a, but in such a way that there is no effect in
> the main screen. This is done by using a local variable b, initially set
> to a, that can be manipulated in the secondary screen, and on closing
> the secondary screen, the variable a is set to b.
>
> This almost works. Evaluate the command, press Start, use the slider,
> press Secondary screen, use the slider for manipulating b and close the
> secondary screen. Everything works fine, apart from the fact that the
> slider and Dynamic[a] in the main screen now do not display the latest
> value for b. Nevertheless, thecurrent value of a IS the latest value of
> b, as can be seen by pressing 'Print a'. So it is only the display in
> the main screen that is incorrect.
>
> There is a trivial workaround for these problems: simply add the option
> UpdateInterval->0.5 to the Dynamic commands in the main screen. But that
> is very unelegant and my feeling is that, in particular in the second
> example, there must be a better way.
>
> Any comment is welcome.
>
> Fred Simons
> Eindhoven University of Technology
>
>