# How can I make controls have Button-like behaviour?

The Button control is very easy to understand

Button["Press Me", Print["Pressed"]]


I press the button, I execute the function. No Dynamic tom-foolery (I'll be honest, I struggle with Dynamics when they get above a certain level of complexity).

I would like to be able to do the same with other controls. For example.

SetterBar[1, Range[5]]


gives 5 buttons for use in Dynamic settings. I'd like something simpler. I'm thinking of something like

SetterBar[1, Range[5],Foo]


Where Foo is a function that gets called when I click on a button on the SetterBar, ideally with an argument corresponding to the button I pressed. So, if I press '5', Foo[5] gets called.

Any ideas?

-
Is ButtonBar what you are looking for, or I missed the point? Well, SetterBar is for setting things, no need to expect more than that. Even if there is no ButtonBar I would create my own with Botton and Row. –  Kuba Oct 2 '13 at 18:35
And to give an example of usage: ButtonBar[# :> Print[#] & /@ Range[5]] –  Kuba Oct 2 '13 at 18:38
I chose setter bar at random to be honest. Others interest me too such as drop down box or whatever mma calls it. –  WalkingRandomly Oct 2 '13 at 20:57

There is really a general method build into Dynamics in Mathematica meant for these things. It is the second argument of dynamics.

One can think of the second argument of Dynamics as an event callback. In GUI, this acts exactly as an event callback in traditional GUI programming, where when one changes state of a UI control, a callback is fired, where one can do any needed work to take care of the change of state for that one GUI control.

This method is very general and very flexible. It can be used to make very complex GUI much easier than otherwise. This is how Matlab GUI works also btw. Each control has a callback function. Other GUI frameworks also have this idea. OpenGL has the callback event and callback registration, etc... In Mathematica, the callback is simply the second argument of Dynamics. Same concept, different name.

It all has this pattern same pattern:

 control_object [ Dynamic[x, {x=#, callback_code_goes_here }&], reset_of_control ]


In the above, the control_object is your SetterBar, PopupMenu, etc.....

So, in your case, your call to foo[] will go where the callback code is. It can be as complex as you want it. It is all part of the Dynamic updating processes.

Here is couple of examples: (I do not have the Initialization code in here to keep it simple)

foo[y_, d_] := Row[{" Hello, you pressed ", y, " on ", d}];
y = 1;
Row[{SetterBar[Dynamic[y, {y = #, r = foo[y, Date[]]} &], Range[5]],Dynamic@r}]


foo2[y_, d_] := Row[{" Hello, your menu selection is ", y, " on ", d}];
y2 = "one";
Dynamic[y2, {y2 = #, r2 = foo2[y2, Date[]]} &], {"one", "two","three"}],Dynamic@r2}]


The point is, there is no need to approach things by looking at each case, and trying to solve each problem in some special way. The above approach is general and can be used to solve all these problems.

-
You beat me to it! +1, esp. for the explanation. I used CompoundExpression (;) instead of List, by the way. It seemed more natural to me. –  Michael E2 Oct 3 '13 at 0:48

Inside a Manipulate it's pretty straightforward:

this[x_] := x;
that[x_] := x^2;
Manipulate[p[3], {p, {this, that}}]


Here p takes on value of one of the function names, and then the chosen function is executed when the button is pressed.

-

Here are a couple variations, including Kuba's ButtonBar, which has yet to be posted as an answer.

buttonFunctionBar[lbls_, func_, opts : OptionsPattern[ButtonBar]] :=
ButtonBar[# :> func[#] & /@ lbls, opts];
ActionMenu[title, # :> func[#] & /@ lbls, opts];


Examples

buttonFunctionBar[Range@5, Print]


actionFunctionMenu["Print", Range@5, Print]


At the risk of drawing criticism for offering a Dynamic solution ;), here is a variation on halirutan's SetterBar. I agree with him that to get the button functionality, one has to resort to using Dynamic. To do much programming with controls, one usually has to use Dynamic.

setterFunctionBar[lbls_, func_, opts : OptionsPattern[ButtonBar]] :=
DynamicModule[{state = First@lbls},
SetterBar[Dynamic[state, (func[#]; state = #) &], lbls, opts]]

setterFunctionBar[Range@5, Print]


One can replace SetterBar with PopupMenu or RadioButtonBar.

-

So you want a SetterBar which provides the argument values to a function Foo? Isn't the most simple way to do this

{SetterBar[Dynamic[y], Range[5]], Dynamic[Foo[y]]}


Without explicit Dynamic there is maybe really only the solution which was already pointed out by bill s.

Manipulate[Foo[v], {v, {1, 2, 3, 4}}]


Or if you want it packed into a function and a bit more fancy:

mySetterBar[func_, values_List] :=
DynamicModule[{y},
Panel[SetterBar[Dynamic[y], values], Dynamic[Panel@func[y]]]
]

mySetterBar[#^2 &, {1, 2, a, b}]


-
How about with no dynamics? –  WalkingRandomly Oct 2 '13 at 20:58
This is not possible. Even your most simple example SetterBar[1, Range[5]] is under the hood a DynamicModuleBox. Just because you don't see it doesn't mean that it is not there ;-) But I'll update my answer.. –  halirutan Oct 2 '13 at 21:01