Button Programming

A hyperlink is a special type of a button.  We can also make buttons with predefined ButtonStyles, and most other buttons perform some sort of manipulation of a notebook. Such buttons that manipulate a notebook can usually be made by following the form in the next cell.  All you need to do is change "Button Name" and "A Pure Function" to suit you needs.

Cell[BoxData[ButtonBox[RowBox[{"Button Name"}], ButtonFunctionᢃ ... tonEvaluatorAutomatic, ActiveTrue]], "Input"]//CellPrint

The next cell shows the form needed to make a palette of three buttons.

Cell[BoxData[GridBox[{ {ButtonBox[RowBox[{"Name 1"}], ButtonFunction ... ic, ActiveTrue]} }, RowSpacings0]] , "Input"]//CellPrint

Working example of a notebook manipulating button (made from scratch)

Suppose you want to make a button that will delete all the messages in the InputNotebook[].  The non-trivial part is writing and debuging the lines of code that are used for the ButtonFunction. This can be solved by having two notebooks open at once.  As an example open an empty notebook and evaluate in the new notebook ( nb=SelectedNotebook[] ).  Next in the new notebook evaluate an erronerous input expression such as  (3=t).  Then come back to this notebook and evaluate the next cell.  The next cell will deletes all messages in the notebook assigned to (nb).  This is most of the code for our ButtonFunction.  The only other thing we need is for our ButtonFunction to make the assignment ( nb = InputNotebook[] ) before evaluating the code in the next cell.

NotebookFind[nb, "Message", All, CellStyle] ; NotebookDelete[nb] ;

The next cell makes the desired button.  However, the button that is created should be converted to a palette by selecting the CellBracket for the button and making the menu selection (File |> Generate Palette From Selection).  Notice  the ButtonFunction setting must be a function.  If you aren't familar with pure functions read my section on Function.  Notice if you use '&' in the ButtonFunction then you have to wrap the button function in parentheses.  As I said earlier we need to use (ButtonEvaluator→Automatic) and (Active→True).

Cell[BoxData[ButtonBox[RowBox[{"Delete Messages"}], ButtonFunction&# ... tonEvaluatorAutomatic, ActiveTrue]], "Input"]//CellPrint

Other Button Tutorials

Links to all the tutorials about button programming that I know off are provided below.

See a tutorial by Paul Hinton at

See several technical support pages on button programming at

You might also find some helpful examples in this notebook under Button Styles or under Hyperlinks.

See How To Create Buttons, by Paul Hinton at

See Programming the Front End, by John Novak at

See Turning an Application into an Interface, Dale Horton at  

Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page