I have a DynamicModule with several graphic elements. I would like to create a button which would change a parameter and re-run the entire module.

Say that we have

h := Plot[Sin[k*x], {x, 0, 10}]

with an initial value of k = 1.

I want to create Button["Increase k", k = k + 1; h] that would run the plot (without using Print[h] as h is really a large DynamicModule).

  • $\begingroup$ Actually to get rid of creation of buttons Manipulate is used. As sample add one more parameter(g) to it Manipulate[Plot[Sin[k*x + g], {x, 0, 10}], {k, 1, 10}, {g, 1, 10}] controls for k ,g are created automatically. $\endgroup$ Jul 14, 2013 at 12:17
  • $\begingroup$ If h is really huge I advise You to not put it all inside Dynamic. Add Dynamic only to k wherever it is (pay attention to Heads) or in case of Manipulate include only that Button and maybe append TrackedSymbols:>{ k, ...}. $\endgroup$
    – Kuba
    Jul 14, 2013 at 14:08

2 Answers 2


This is just an addendum to jVincent's answer.

I do this sort of thing all the time when a full-bore Manipulate would be overkill. I recommend adding a Reset button and a dynamic label to make the dynamic behavior more Manipulate-like.

h := Plot[Sin[k x], {x, 0, N[4 Pi]},
  PlotLabel -> Sin[k x],
  ImageSize -> Medium];
btns = Row[{Button["k++", k++], Button["Reset", k = 1]}, Spacer[10]];
k = 1; Column[{Dynamic@h, btns}]



If I understand you correctly, you can simply display h in a Dynamic and then create your button to increase k, so first define your module:

k = 1;
h := Plot[Sin[k*x], {x, 0, 10}]

Then display the plot and button:

Button["Increase k", k = k + 1]

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.