3
$\begingroup$

I wish to use Buttons to trigger alternate Manipulates.

According to the documentation: "The first argument to Button is the label" ....and the "second argument is the function that will evaluate when you click the button." (source: https://reference.wolfram.com/language/tutorial/IntroductionToControlObjects.html

Here is simple code which works and runs a Manipulate

Remove[a, b, ma, mb]
ma[a_, b_] := a^2 + b
Manipulate[ma[a, b], {{a, 1}, 0, 5}, {{b, 1}, 0, 5}]      

And here is modified example which SHOULD work via button according to the documentation (unless Manipulate is not in fact a function!). This code does NOT work.

Remove[a, b, ma, mb]
ma[a_, b_] := a^2 + b
Button["Crazy Dead Button",Manipulate[ma[a, b], {{a, 1}, 0, 5}, {{b, 1}, 0, 5}]]  

The documentation is leading me astray. Only some functions can be triggered this way I guess. But why not, and what is easiest way to fix the code?

$\endgroup$
2
  • 1
    $\begingroup$ Try Button["Crazy Dead Button", Print@Manipulate[ma[a, b], {{a, 1}, 0, 5}, {{b, 1}, 0, 5}], Method -> "Queued"]? $\endgroup$
    – kglr
    May 11, 2015 at 21:12
  • $\begingroup$ Or Button["Crazy Dead Button", CellPrint[Manipulate[ma[a, b], {{a, 1}, 0, 5, Appearance -> "Labeled"}, {{b, 1}, 0, 5, Appearance -> "Labeled"}]]] $\endgroup$
    – Bob Hanlon
    May 11, 2015 at 22:22

1 Answer 1

3
$\begingroup$

There are two issues here. The first one can be explained easily because you simply forgot that your button needs to do something. Just evaluating your Manipulate is not going to do what you like, because you need to see it on screen when you want to interact with it. Therefore, you need to Print or CellPrint your Manipulate.

The second issue arises when you use a button, which is a dynamic object to create another dynamic object. Both rely on the same infrastructure and you need to be careful. The whole story behind this is too long to be explained here but a good start is the advanced dynamic tutorial in the documentation. Nevertheless, two solutions as posted in the comments by kguler and Bob

Button["Crazy Dead Button", Print@Manipulate[ma[a, b], {{a, 1}, 0, 5},
  {{b, 1}, 0, 5}], Method -> "Queued"]

Button["Crazy Dead Button", CellPrint[Manipulate[ma[a, b], 
  {{a, 1}, 0, 5, Appearance -> "Labeled"}, {{b, 1}, 0, 5, 
  Appearance -> "Labeled"}]]]
$\endgroup$
1
  • $\begingroup$ Manipulate is a function, though a complex one. If a properly formed Manipulate function is encountered in an Input cell, it will generate an Output cell. You do not have to hit it with Print, or CellPrint. But if it is encountered inside a properly formed Button function, it will not execute, it will not generate the Output cell. It seems to me this is a bug in Mathematica. Either Button[] will execute some functions and not others, or Manipulate will execute if it is encountered in some properly formed functions but not others. I don't know which. What do you think? $\endgroup$
    – GeofAndron
    May 14, 2015 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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