Skip to main content
added 56 characters in body
Source Link
Nasser
  • 150.4k
  • 12
  • 161
  • 374

I've been trying to learn more about DynamicModule (I do not use them, as I used Manipulate all the time).

After some time struggling with this, and reading everything I can about Dynamics, and lots of trials and errors, I gave up.

Any one can figure why this DynamicModule will initialize correctly and run ok, ONLY after I hit ENTER on the cell an extra time?

This is a problem, since when I savesaved it to a CDF and openopened the CDF, it does not run, since it needs an ENTER to initialize, which is not possible to do in CDF.

DynamicModule[{max = Pi/4, plot = plotClass[Pi/4]},
 
 Row[{
   Dynamic[plot@set[max]],
   Framed@Grid[{
      {Slider[Dynamic[max], {.1, 2 Pi, .1}], Dynamic@max},
      {Dynamic[plot@make[]], SpanFromLeft}
      }
     ]
   }
  ],
 Initialization :> {
   plotClass[$max_] := Module[{self, mymax},
     self@set[v_] := mymax = v; 
     self@make[] := Module[{x},
       Plot[Sin[x], {x, 0, mymax}, PlotRange -> {{0, 2 Pi}, {-1, 1}}, 
        ImageSize -> 200]
       ];
     mymax = $max;
     self
     ]
   }
 ]

Mathematica graphics

To see the problem, simply do

  1. Paste the above into an empty cell (but do not hit ENTER yet)
  2. close the kernel Evalution->Quit kernel
  3. Hit ENTER on the cell. Now you'll see the plot
  4. Now move the slider. You'll see that the plot does not update
  5. Now hit ENTER one more time into the cell. Now moving the slider will update the plot
  6. To reproduce, delete the output cell and go back to step 2.

So it needs one more ENTER to work. Another way to see this, is to save the notebook as CDF and open the CDF. You'll see it does not work.

I am sure I am doing something silly. But as I said, I just started learning DynamicModule.

The problem is with the evaluation with the Module in the Initialization section. It does not get defined without an extra ENTER for some reason. I tried many different way to define this Module, and they are failed. One extra ENTER is needed.

Mathematica 9.0.1, on windows 7.

I've been trying to learn more about DynamicModule (I do not use them, as I used Manipulate all the time)

After some time struggling with this, and reading everything I can about Dynamics, and lots of trials and errors, I gave up.

Any one can figure why this DynamicModule will initialize correctly and run ok, ONLY after I hit ENTER on the cell an extra time?

This is a problem, since when I save it to a CDF and open the CDF, it does not run, since it needs an ENTER which is not possible to do in CDF.

DynamicModule[{max = Pi/4, plot = plotClass[Pi/4]},
 
 Row[{
   Dynamic[plot@set[max]],
   Framed@Grid[{
      {Slider[Dynamic[max], {.1, 2 Pi, .1}], Dynamic@max},
      {Dynamic[plot@make[]], SpanFromLeft}
      }
     ]
   }
  ],
 Initialization :> {
   plotClass[$max_] := Module[{self, mymax},
     self@set[v_] := mymax = v; 
     self@make[] := Module[{x},
       Plot[Sin[x], {x, 0, mymax}, PlotRange -> {{0, 2 Pi}, {-1, 1}}, 
        ImageSize -> 200]
       ];
     mymax = $max;
     self
     ]
   }
 ]

Mathematica graphics

To see the problem, simply do

  1. Paste the above into an empty cell (but do not hit ENTER yet)
  2. close the kernel Evalution->Quit kernel
  3. Hit ENTER on the cell. Now you'll see the plot
  4. Now move the slider. You'll see that the plot does not update
  5. Now hit ENTER one more time into the cell. Now moving the slider will update the plot
  6. To reproduce, delete the output cell and go back to step 2.

So it needs one more ENTER to work. Another way to see this, is to save the notebook as CDF and open the CDF. You'll see it does not work.

I am sure I am doing something silly. But as I said, I just started learning DynamicModule.

The problem is with the evaluation with the Module in the Initialization section. It does not get defined without an extra ENTER for some reason. I tried many different way to define this Module, and they are failed. One extra ENTER is needed.

I've been trying to learn more about DynamicModule (I do not use them, as I used Manipulate all the time).

After some time struggling with this, and reading everything I can about Dynamics, and lots of trials and errors, I gave up.

Any one can figure why this DynamicModule will initialize correctly and run ok, ONLY after I hit ENTER on the cell an extra time?

This is a problem, since when I saved it to a CDF and opened the CDF, it does not run, since it needs an ENTER to initialize, which is not possible to do in CDF.

DynamicModule[{max = Pi/4, plot = plotClass[Pi/4]},
 
 Row[{
   Dynamic[plot@set[max]],
   Framed@Grid[{
      {Slider[Dynamic[max], {.1, 2 Pi, .1}], Dynamic@max},
      {Dynamic[plot@make[]], SpanFromLeft}
      }
     ]
   }
  ],
 Initialization :> {
   plotClass[$max_] := Module[{self, mymax},
     self@set[v_] := mymax = v; 
     self@make[] := Module[{x},
       Plot[Sin[x], {x, 0, mymax}, PlotRange -> {{0, 2 Pi}, {-1, 1}}, 
        ImageSize -> 200]
       ];
     mymax = $max;
     self
     ]
   }
 ]

Mathematica graphics

To see the problem, simply do

  1. Paste the above into an empty cell (but do not hit ENTER yet)
  2. close the kernel Evalution->Quit kernel
  3. Hit ENTER on the cell. Now you'll see the plot
  4. Now move the slider. You'll see that the plot does not update
  5. Now hit ENTER one more time into the cell. Now moving the slider will update the plot
  6. To reproduce, delete the output cell and go back to step 2.

So it needs one more ENTER to work. Another way to see this, is to save the notebook as CDF and open the CDF. You'll see it does not work.

I am sure I am doing something silly. But as I said, I just started learning DynamicModule.

The problem is with the evaluation with the Module in the Initialization section. It does not get defined without an extra ENTER for some reason. I tried many different way to define this Module, and they are failed. One extra ENTER is needed.

Mathematica 9.0.1, on windows 7.

Source Link
Nasser
  • 150.4k
  • 12
  • 161
  • 374

How to make DynamicModule work without an extra ENTER

I've been trying to learn more about DynamicModule (I do not use them, as I used Manipulate all the time)

After some time struggling with this, and reading everything I can about Dynamics, and lots of trials and errors, I gave up.

Any one can figure why this DynamicModule will initialize correctly and run ok, ONLY after I hit ENTER on the cell an extra time?

This is a problem, since when I save it to a CDF and open the CDF, it does not run, since it needs an ENTER which is not possible to do in CDF.

DynamicModule[{max = Pi/4, plot = plotClass[Pi/4]},
 
 Row[{
   Dynamic[plot@set[max]],
   Framed@Grid[{
      {Slider[Dynamic[max], {.1, 2 Pi, .1}], Dynamic@max},
      {Dynamic[plot@make[]], SpanFromLeft}
      }
     ]
   }
  ],
 Initialization :> {
   plotClass[$max_] := Module[{self, mymax},
     self@set[v_] := mymax = v; 
     self@make[] := Module[{x},
       Plot[Sin[x], {x, 0, mymax}, PlotRange -> {{0, 2 Pi}, {-1, 1}}, 
        ImageSize -> 200]
       ];
     mymax = $max;
     self
     ]
   }
 ]

Mathematica graphics

To see the problem, simply do

  1. Paste the above into an empty cell (but do not hit ENTER yet)
  2. close the kernel Evalution->Quit kernel
  3. Hit ENTER on the cell. Now you'll see the plot
  4. Now move the slider. You'll see that the plot does not update
  5. Now hit ENTER one more time into the cell. Now moving the slider will update the plot
  6. To reproduce, delete the output cell and go back to step 2.

So it needs one more ENTER to work. Another way to see this, is to save the notebook as CDF and open the CDF. You'll see it does not work.

I am sure I am doing something silly. But as I said, I just started learning DynamicModule.

The problem is with the evaluation with the Module in the Initialization section. It does not get defined without an extra ENTER for some reason. I tried many different way to define this Module, and they are failed. One extra ENTER is needed.