This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Button

Updated In 8 Graphic
Button
represents a button that is labeled with label, and evaluates action whenever it is clicked.
  • label can be any expression, including a dynamic one. »
  • Button by default displays label in style, which typically uses the system button font.
  • Button maintains action in unevaluated form, evaluating it each time the button is clicked.
  • The following options can be given:
AlignmentAutomatichow to align contents within the button
AppearanceAutomaticthe overall appearance of the button
AutoActionFalsewhether to click the button automatically when the mouse is over it
BackgroundAutomaticbutton background color
BaselinePositionAutomaticalignment relative to surrounding text
BaseStyle"GenericButton"base style specifications for the button
ContentPaddingTruewhether to shrink the margins tightly around the contents
EnabledAutomaticwhether the button is enabled, or grayed out
EvaluatorAutomaticthe kernel in which to evaluate expr
FrameMarginsAutomaticminimum margins to leave inside the frame
ImageMargins0margins around the image of the displayed button
ImageSizeFullthe overall image size of the displayed button
Method"Preemptive"the evaluation method to use
  • With the default setting ImageSize->Full, a button will be sized to fill out its enclosing region in a Grid or related construct.
  • The setting ImageSize->Automatic specifies that the button will be sized to fit its contents, but will not expand to fill out an enclosing region.
  • Typical possible settings for the Appearance option include , , , and . In some cases, , , etc. are also supported.
  • Appearance gives a button with a pressed appearance. Appearance gives a button of a certain type with a pressed appearance.
  • With Appearance->None, label is displayed literally, without being placed in a button. »
  • Typical possible settings for the Method option include and .
  • With the default setting Method, button actions are performed immediately, preempting any other evaluation, but are allocated only a limited time to complete.
  • With the setting Method, button actions are added to the current queue of evaluations, and are performed when other evaluations are complete. No time limit is applied.
  • The settings for BaseStyle are appended to the default style typically given by the style in the current stylesheet.
  • Button[label] displays as a button labeled with label, but performs no action.
  • Button can be used within graphics objects to specify that action should be evaluated whenever the graphics primitives prims are clicked. »
  • Button[label, None, BaseStyle->stylename] inherits button functionality from the definition of stylename in the stylesheet. Such styles typically allow \[SelectionPlaceholder] to be used in label to represent the currently selected text.
  • Some style names defined in default stylesheets that provide Button functionality:
"Paste"effectively use NotebookApply upon the current selection using the button label as the applied data
"Evaluate"use NotebookApply and SelectionEvaluate in sequence upon the current selection
"EvaluateCell"use NotebookApply upon the current selection, then evaluate the entire cell containing the selection
"CopyEvaluate"equivalent to , but paste the result into a new cell instead of changing the currently selected cell
"CopyEvaluateCell"equivalent to , but apply changes to a newly created copy of the input cell instead of changing the currently selected cell
Create a button that prints a number:
Create a button that prints a number:
In[1]:=
Click for copyable input
Out[1]=
Use any expression as a label:
Use a dynamic label:
Use a graphics primitive as a button:
Create a list of buttons; use With to ensure evaluation of :
Another way to get the contents of a button evaluated:
Display the label literally, without the appearance of a button:
Display a button that sorts the selected expression:
Select the list below, and click the button to sort it:
Display a button that wraps Factor around an expression, then evaluates the entire cell:
Select the first three terms below, then click the button to factor them:
Get the mouse position in screen coordinates:
Get the mouse position in graphics coordinates:
Different predefined alignment options:
Predefined button appearances:
On some platforms, the following appearances are also defined:
Use a second element to get the appearance of a pressed button:
Use a special appearance for the button:
By default, the button function does not evaluate until you click it:
By setting AutoAction, the button function evaluates as you mouseover the button area:
Change the background colors:
Change the background color on every click:
Align with the surrounding text:
Change the baseline position on every button click:
Add styles to a button:
Select the behavior of a button:
Select the expression below and click the above button to expand inline:
Remove extra whitespace around a label with no ascenders or descenders:
By default, Button is enabled:
By setting Enabled->False, the button is disabled but visible in its current state:
By default, the button function is sent to the kernel for evaluation:
By setting Evaluator, the button function is evaluated in the front end:
Not all functionalities are available in the front end:
By setting FrameMargins, you make the button content area larger:
By setting ImageMargins, you make the button area larger:
Use preset values:
Or use any values:
By setting the second element, you can also control the height:
A fully customized image size:
By default, button functions are evaluated on a preemptive link, which times out after 5 seconds:
Use Method to evaluate button functions on the main link, which never times out:
Specify that the stop button event needs to preemptively interrupt an evaluation:
Make a button that prints the current date:
Open the system color selector:
Create a button that deletes itself:
Create your own hyperlink:
Change color on mouseover:
Create a grid of tightly spaced buttons:
Create a button that makes the button the current selection:
Some functions require the kernel in order to be evaluated:
Specifying the Background option forces a generic appearance for the button:
Use ImageSize to control buttons in a Grid:
Use Method to avoid evaluation timeout on the preemptive link:
This button changes color each time you click it:
This button changes position each time you click it:
Use a graphics primitive as the button:
New in 6 | Last modified in 8