4
$\begingroup$

It is MWE, only a Disk[] that should print "Click" when clicked.

Also "date... updating" should be printed on the first evaluation and on each update of variable enabled. (which is constant in this MWE)

In the Column there is another Dynamic object which shows current MousePosition. And this element is responsible for printing (flooding) "date... updating" even though enabled is not changing.

DynamicModule[{enabled = True, pos},
 Column[{
        Dynamic[Print[DateString[] <> " updating"];
                EventHandler[Graphics@Disk[], {"MouseClicked" :> Print["Click"]}],
                TrackedSymbols :> {enabled}]
        ,
        Dynamic[pos]
        }]
 , Initialization :> (pos := MousePosition["Graphics"])]

enter image description here

I think I don't understand this and IMO it is not expected or desired behaviour.

When one put another Print next to Graphics you will see that it is in face whole Graphics updating.

So the question is:

If it's not a bug (I missed something) then how to recreate this MWE so it behaves correctly.


I think it may be (but not necessarily) related to Why does a heavy background slow LocatorPane from updating, even if only the locators are Dynamic?

p.s. Dynamic[Refresh[..., TrackedSymbols:>{enabled}]] behaves the same way.


Edit so at the end it seems to be more related to Button evaluation inside DynamicModule

$\endgroup$
2
  • $\begingroup$ Ah I finally understand, somehow I read constant as constantly, which is quite different :P. $\endgroup$ Commented Mar 5, 2014 at 14:50
  • $\begingroup$ Hm see update, maybe it has something to do with DynamicModuleValues. $\endgroup$ Commented Mar 5, 2014 at 15:18

2 Answers 2

4
$\begingroup$

(This is more of an extended comment until a better understanding evolves.)

Finally I've found the relevant MathGroup thread from old - apparently the behaviour is as old as the dynamic interactivity in Mathematica. At that time, Norbert Pozar introduced a really simple example to demonstrate the strange internals of DynamicModule when a symbol in an Initialization assignment sets OwnValues instead of DownValues with SetDelayed (Set is fine). Be aware, this will probably crash your kernel!

DynamicModule[{x}, 1, Initialization :> (x := Print["Should never see this."])]

However, changing the assignment to something[]:=... works as expected. Clearly, DynamicModule evaluates the symbol x internally, even if it is never used; and if the symbol has side effects, that can be catastrophic.

Bottom line: always use DownValues instead of Onwvalues when doing assignments inside Initialization code in DynamicModule. Like that:

DynamicModule[{x}, 1, Initialization :> (x[]:= Print["Should never see this."])]

Spelunking in the definition of DynamicModule, I've found that the problem is even more deeply embedded: it must be in DynamicModuleBox. The following example still crashes a fresh kernel:

CellPrint@Cell@BoxData@DynamicModuleBox[{x}, 1, 
    Initialization :> (x := Print["Should never see this!"])]
$\endgroup$
4
  • $\begingroup$ Great thread! Thanks. $\endgroup$
    – Kuba
    Commented Mar 6, 2014 at 22:51
  • $\begingroup$ @Kuba Unfortunately, no one from WRI replied to that MG thread so the only option we have is to explore the definition of DynamicModule. $\endgroup$ Commented Mar 6, 2014 at 22:55
  • $\begingroup$ I've pined JohnFultz in chat yesterday. I would love to see an explanation. p.s. added example for clarity, hope you don't mind. $\endgroup$
    – Kuba
    Commented Mar 6, 2014 at 22:57
  • 1
    $\begingroup$ p.s. OwnValues are ok with Set, worth adding in bottom line that it is in context of SetDelayed. $\endgroup$
    – Kuba
    Commented Mar 6, 2014 at 23:03
1
$\begingroup$

Maybe this does what you want?

DynamicModule[
 {enabled = True, pos},
 Column[
  {
   Dynamic[
    Print[DateString[] <> " updating"];
    EventHandler[
     Graphics@Disk[], {"MouseClicked" :> Print["Click"]}
     ]
    ]
   ,
   Dynamic[pos[]]
   }
  ]
 ,
 Initialization :> (pos[] := MousePosition["Graphics"])
 ]

Really... trial and error.

$\endgroup$
3
  • $\begingroup$ That seems hopeful, doesn't it? I'm not exactly sure what you mean. $\endgroup$ Commented Mar 5, 2014 at 15:09
  • $\begingroup$ Just talking :P it's something but it will not help me, the main issue is still there. Now testking your answer... $\endgroup$
    – Kuba
    Commented Mar 5, 2014 at 16:16
  • $\begingroup$ So the answer is: never ever use SetDelayed for OwnValues purposes of DynamicModule variables. Why? Everything is crazy then. $\endgroup$
    – Kuba
    Commented Mar 5, 2014 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.