3
$\begingroup$

Please consider the toy code here:

CreatePalette[DynamicModule[{var},

   Button["Generate Random Number", randstuff[var], 
    ImageSize -> Medium, Method -> "Queued"]],

  {Initialization -> (randstuff[var_] := DynamicModule[{},
       Print[RandomReal[]]]);

   WindowTitle -> "toy palette"}

  ];

If you run the code above and press the "Generate Random Number", you will see a random number printed in the Messenges notebook that pops up. Everything seems to be ok.

Then, if you install this notebook, quit the kernel and select the newly installed palette from the Palettes drop-down menu, the Generate Random Number button no longer works.

A. Why does the code fail to execute properly when it is run from an installed palette notebook?

B. Since Mathematica essentially does nothing (from the installed palette), how does one troubleshoot (debug) behavior like this?

Thanks for reading.

$\endgroup$
  • $\begingroup$ What do you mean by "install this notebook"? $\endgroup$ – MarcoB Jun 16 '15 at 2:24
6
$\begingroup$

I see two problems here. First, Initialization is not a known option for CreatePalette:

Options[CreatePalette, Initialization]
Options::optnf: Initialization is not a known option for CreatePalette. >>

Second, even if it were your use of Rule rather than RuleDelayed means that your initalization code will only be evaluated when the palette is created.

Put the initialization within DynamicModule and use :> and it should work:

CreatePalette[
  DynamicModule[{var}, 
   Button["Generate Random Number", randstuff[var], ImageSize -> Medium, 
    Method -> "Queued"], 
   Initialization :> (randstuff[var_] := DynamicModule[{}, Print[RandomReal[]]])],
  WindowTitle -> "toy palette"];

Regarding debugging I would probably start by looking at the code for the Palette Notebook itself to see what is there. In the case of your original Palette for example you will not find an equivalent to the Initialization line in the Box form expressions; that could point one to the general area of the problem at least.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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