11
$\begingroup$

I would like to have a command "RedoButton" which would make a button (or whatever control is suitable) that when clicked would print the value of a certain function over and over in the same place. So for example RedoButton["do it", RandomInteger[{1, 10}] would produce a button that, when clicked, would produce a random integer of the sort required, clicking it again would produce a possibly different random integer where the old one was, and so on. I have tried Toggle, Button, and numerous other commands but can't make any of them work correctly.

$\endgroup$

2 Answers 2

17
$\begingroup$

Maybe you could do something like this

SetAttributes[redoButton, HoldRest]
redoButton[str_, fun_] := DynamicModule[{result = Null}, 
   Column[{Button[str, result = fun], Dynamic[result]}]]

redoButton["press", RandomInteger[20]]
$\endgroup$
1
  • $\begingroup$ That works -- thanks. $\endgroup$ Commented May 2, 2012 at 14:11
8
$\begingroup$

If you want to use the button itself to show the current value, this may help:

Button[Dynamic[x], x = RandomInteger[{1, 20}]]

Mathematica graphics

You can then use x or Dynamic[x] etc. for further computation/display.

$\endgroup$

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.