Function Repository Resource:

ToggleButton

Source Notebook

A button that cycles through a list of titles and actions when clicked

Contributed by: Michael Sollami

ResourceFunction["ToggleButton"][{label1 action1,label2 action2,}]

represents a cyclically toggling button that executes actioni when the title labeli is pressed.

Details and Options

ResourceFunction["ToggleButton"] accepts all options of Button.
Note the use of RuleDelayed in the argument list; this is required.

Examples

Basic Examples (2) 

Create a button that cycles though values:

In[1]:=
ResourceFunction["ToggleButton"][Thread[Range@3 :> Null]]
Out[1]=

Create a button that switches between two actions:

In[2]:=
ResourceFunction[
 "ToggleButton"][{"a" :> Print["a"], "b" :> Print["b"]}]
Out[2]=

Options (2) 

Use any of the standard Button options:

In[3]:=
Column @ Table[
  ResourceFunction[
   "ToggleButton"][{"A" :> Null, "B" :> Null, "C" :> Null}, Appearance -> a], {a, {"DialogBox", {"DialogBox", "Pressed"}, "Palette", {"Palette", "Pressed"}}}]
Out[3]=

Style your ToggleButton like a normal Button:

In[4]:=
ResourceFunction[
 "ToggleButton"][{"Pause 0.1s" :> Pause@.1, "Pause 1s" :> Pause@1},
 Background -> LightBlue, ImageSize -> {150, 50}, BaseStyle -> {"GenericButton", 16, Bold, Darker@Green}]
Out[4]=

Applications (2) 

Create a play/pause button:

In[5]:=
a = ExampleData[{"Audio", "CelloScale"}];
astream = AudioStream[a];
Row@{ResourceFunction["ToggleButton"][{"\!\(\*
StyleBox[\"\[RightPointer]\",\nFontSize->18]\)" :> AudioPlay@astream, "Ⅱ" :> AudioPause@astream}], Dynamic[astream["Position"]]}
Out[7]=

Create a button that toggles cycles through some images:

In[8]:=
DynamicModule[{$img = Text["Nothing loaded"], btn},
 btn = ResourceFunction["ToggleButton"][{
    "Load \!\(\*
StyleBox[\"Glasses\",\nFontSlant->\"Italic\"]\)" :> ($img = Import@"ExampleData/glasses.png"),
    "Load \!\(\*
StyleBox[\"Turtle\",\nFontSlant->\"Italic\"]\)" :> ($img = Import@"ExampleData/turtle.jpg"),
    "Load \!\(\*
StyleBox[\"Spikey\",\nFontSlant->\"Italic\"]\)" :> ($img = Import@"ExampleData/spikey2.png")
    }];
 Panel@Labeled[Dynamic[$img], btn, Top]
 ]
Out[8]=

Publisher

Michael Sollami

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 22 February 2019

License Information