The documentation of Dynamic
states that it can be used in three ways: not only directly as an expression to display, but also for example as a coordinate in a graphics primitive or an object such as a slider, or as a setting for an option. In our example we have all three ways.
With respect to quitting the kernel, the last situation turns out to be different from the two others. LinkSnooper
shows that we if we evaluate Dynamic[x]
or Slider[Dynamic[x]]
and then quit the kernel, the kernel does not restart. But when we evaluate Style["Some text", 20, Background->Dynamic[Hue[x]]]
and quit the kernel, immediately the kernel is restarted. Let us have a closer look at the LinkSnooper
output.
x=RandomReal[];
Style["Some text", 20, Background->Dynamic[Hue[x]]]
LinkSnooper
shows that two dynamic objects with numbers 39935 and 40175 are created. Very likely, one of these refers to the displayed StyleBox
and the other to the option Background
.
Slider[Dynamic[x]]
LinkSnooper
shows that a dynamic object with number 40257 is created.
Dynamic[x]
LinkSnooper
shows that a dynamic object with number 40337 is created.
Internal`GetTrackedSymbols[]
(* Hold[{MakeBoxes,{40337},False},{x,{39935,40175,40257,40337},False}] *)
Therefore, all three (four) dynamic objects will be updated when the value of the kernel variable x
changes.
Quit[]
Due to the presence of the first dynamic object, the kernel is closed and immediately a new kernel is started. LinkSnooper
does not show any other interaction between the new kernel and the frontend, but during the startup of the new kernel something must have happened:
In[1]:= Internal`GetTrackedSymbols[]
Out[1]= Hold[{x,{26831,40175},False}]
It turns out that the object 40175 is the displayed StyleBox
. When we closed the old kernel, immediately a new kernel was launched in which the StyleBox
was synchronized with the (new) kernel variable x
. When we click on the slider, the kernel variable x
gets the value of the clicked position of the slider. The kernel variable x
is connected to the object 40175, so the StyleBox
will be updated, but not connected to the objects 40257 and 40337, so the slider and the dynamic will not be updated.