DynamicModule

DynamicModule[{x,y,},expr]

represents an object which maintains the same local instance of the symbols x, y, in the course of all evaluations of Dynamic objects in expr. Symbols specified in a DynamicModule will by default have their values maintained even across Wolfram System sessions.

DynamicModule[{x=x0,y=y0,},expr]

specifies initial values for x, y, .

Details and Options

Examples

open allclose all

Basic Examples  (1)

Create a Slider with a dynamically updating localized variable:

Copy and paste the output above to get a standalone object:

Scope  (6)

DynamicModule Variables  (3)

Use DynamicModule to localize variables and prevent conflicts with their global counterparts:

Local variables are unique to the generated output:

Assign initial values to local variables, just like Module:

Define a function as a local variable:

DynamicModule State  (3)

Use DynamicModule to save the displayed state across different Wolfram System sessions:

Module only gives unique states in the current Wolfram System session:

Use Initialization to evaluate expressions before displaying the contents:

Initialization is evaluated after the local variable assignments:

Generalizations & Extensions  (1)

Define an arbitrary number of local variables by assigning down values to a variable:

Options  (16)

BaseStyle  (1)

Set the base style:

Deinitialization  (1)

Specify expressions to be evaluated when output is no longer displayed:

DynamicModuleValues  (1)

Function definitions of local symbols are automatically inserted into DynamicModuleValues:

ExcludedContexts  (1)

By default, certain system-internal contexts are not saved in the initialization option:

Use ExcludedContexts{} to save definitions of all non-protected symbols:

InheritScope  (1)

Create a dialog box with a DynamicModule that inherits variables from a parent DynamicModule:

Initialization  (4)

By default, external definitions are lost between kernel sessions:

Use Initialization to save evaluations necessary for the content:

Local variables can also be initialized:

The initialization only runs if the output is displayed:

The first DynamicModule was suppressed by the semicolon, so it never ran its assignment:

IncludedContexts  (1)

Limit recursive inclusion of symbol definitions to the contexts "c1`" and "c2`" only:

SaveDefinitions  (2)

By default, external definitions are lost between kernel sessions:

By setting SaveDefinitions to True, the external definitions are saved with the output:

SynchronousInitialization  (2)

By default, the initializations are evaluated before the output is displayed (synchronously):

Force the output to display while the initializations are evaluated asynchronously:

UndoTrackedVariables  (1)

Create an interface that responds to the Undo menu command:

UnsavedVariables  (1)

Specify symbols whose state should not be saved:

Evaluate the following and move the two sliders, then click Close:

Evaluate the following and note that the second slider does not remember its previous state:

Applications  (3)

Construct a dynamic calculating interface:

Interactive curve fit plot:

An angular slider with range :

Connect the slider to a dynamic variable:

Demonstrate a sinusoidal relationship:

Use PolarPlot to demonstrate a spiral curve:

Properties & Relations  (2)

DynamicModule and Module are fundamentally different although they seem similar:

In this case both sliders will move, and will not work without evaluation when reopened:

Manipulate relies on DynamicModule to create the output:

Possible Issues  (1)

By default, definitions attached to "System`" symbols are not pulled in:

Use ExcludedContexts{} to pull in definitions from all contexts:

Alternatively, attach definitions to your own symbols:

Neat Examples  (2)

Independent state sliders:

Click in the framed area to see bouncing balls:

Use dynamic objects as input; click in the different frames to create additional balls:

Wolfram Research (2007), DynamicModule, Wolfram Language function, https://reference.wolfram.com/language/ref/DynamicModule.html (updated 2021).

Text

Wolfram Research (2007), DynamicModule, Wolfram Language function, https://reference.wolfram.com/language/ref/DynamicModule.html (updated 2021).

CMS

Wolfram Language. 2007. "DynamicModule." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/DynamicModule.html.

APA

Wolfram Language. (2007). DynamicModule. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DynamicModule.html

BibTeX

@misc{reference.wolfram_2024_dynamicmodule, author="Wolfram Research", title="{DynamicModule}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/DynamicModule.html}", note=[Accessed: 12-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_dynamicmodule, organization={Wolfram Research}, title={DynamicModule}, year={2021}, url={https://reference.wolfram.com/language/ref/DynamicModule.html}, note=[Accessed: 12-December-2024 ]}