Button[] beguiles me. A simple example follows:

b1 = Button["Evaluate", Cos[Pi/6], Method -> "Queued"]
b2 = Button["Evaluate", Print[Cos[Pi/6]], Method -> "Queued"]

Both of the above statements produce buttons. When one clicks on b1, it appears to evaluate something, but does not output or display anything. Clicking on b2 displays the expected output.

But clicking a second time on b2 prints the expected result a second time. Continuing to click on b2 just increases the list of printed results. This uselessly clutters the notebook I want to distribute.

I want a Button[] to execute a function and simply display its results as near as possible to what pressing Shift + Return in an input cell would do.

I further want it to overwrite previous output.

Ideally I would place the Button[] expression in a hidden initialization cell so only the actual button appears in the deployed notebook.

Does anyway exist to get a Button to do what I've described? How?

  • $\begingroup$ Good question. In normal evaluation CellPrint overwrites the previous output but it does not work from within Button, at least as I tried it. $\endgroup$
    – Mr.Wizard
    Commented Jan 25, 2013 at 23:37
  • $\begingroup$ @Mr.Wizard -- Any thoughts on why the difference? $\endgroup$
    – Jagra
    Commented Jan 26, 2013 at 2:49

4 Answers 4


You could try something like:

y = 0; 
b1 = Button["Evaluate", y = Cos[Pi/6] (++y), Method -> "Queued"]

Mathematica graphics

  • $\begingroup$ I must not have made described what I need to do clearly. When I click the Button a 2nd or 3rd time I need it to completely overwrite the output, neither adding to a list of printed results nor nesting the evaluation as you show above. In use I'll have some input variables that may get changed between the times someone clicks the button. I want a Button that will simply evaluate an expression and give me the same output I'd get from evaluating an input cell. Still, I wonder if you've pointed me in a productive direction. I'll try some things. Thx. $\endgroup$
    – Jagra
    Commented Jan 25, 2013 at 19:01
  • 2
    $\begingroup$ @Jagra My evaluation is recursive just because I wanted it.You may evaluate whatever you want $\endgroup$ Commented Jan 25, 2013 at 19:04
  • $\begingroup$ Got it working. Many thanks. $\endgroup$
    – Jagra
    Commented Jan 25, 2013 at 19:32

Sometimes Dynamic is not really needed. In Version 9 you can use the nice new Cells[] construct. So the button could look like

   NotebookDelete[Cells[CellTags -> "calculationbutton"]];
     "Output", CellTags -> "calculationbutton"], Method -> "Queued"], 

And I would also just close the instruction cell to hide it, e.g.:

 RowBox[{"CellPrint", "@", 
  RowBox[{"ExpressionCell", "[", 
    RowBox[{"Button", "[", 
     RowBox[{"\"\<Evaluate\>\"", ",", "\[IndentingNewLine]", 
       RowBox[{"NotebookDelete", "[", 
        RowBox[{"Cells", "[", 
         RowBox[{"CellTags", "->", "\"\<calculationbutton\>\""}], 
         "]"}], "]"}], ";", "\[IndentingNewLine]", 
       RowBox[{"CellPrint", "@", 
        RowBox[{"ExpressionCell", "[", 
          RowBox[{"Cos", "[", 
           RowBox[{"Pi", "/", "6"}], "]"}], ",", 
          "\[IndentingNewLine]", "\"\<Output\>\"", ",", 
          "CellTags", "\[Rule]", "\"\<calculationbutton\>\""}]}], 
         "]"}]}]}], ",", 
      RowBox[{"Method", "\[Rule]", "\"\<Queued\>\""}]}], "]"}], ",", 
    "\"\<Text\>\""}], "]"}]}]], "Input",
 CellGroupingRules->{GroupTogetherGrouping, 10000.}],

     Cells[CellTags -> "calculationbutton"]]; CellPrint[
      Cos[Pi/6], "Output", CellTags -> "calculationbutton"]]),
  Method->"Queued"]], "Text",
 CellGroupingRules->{GroupTogetherGrouping, 10000.},
}, {2}]]

Another approach is to make the button disable itself.

DynamicModule[{enabled = True}, 
  Dynamic@Button["Evaluate", enabled = False; Print[Cos[Pi/6]], 
  Method -> "Queued", Enabled -> enabled]]

Before clicking on the button


After clicking



I realise this question is old, but in my quest to find a solution to a similar problem to OP I have come up with a solution using CellTags and FrontEndExecute (like Rolf M). I try to avoid Dynamic.

Create a cell tag for the cell you want to evaluate with a button. This approach will Evaluate all cells with the same CellTag and depending on the scenario this could be advantageous.

In my example I used the CellTag "cellToEvaluate". (Select CellTag from front-end Cell menu to add cell tags.)

Create a Button that finds and evaluates cells with that CellTag.




Your Answer

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

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