Message Boards Message Boards


Button not working with Plot?

Posted 1 month ago
4 Replies
0 Total Likes

Hi everyone, I am totally new to Mathematica, so please forgive me my probably stupid question.

I am trying to do some Plots of functions with RandomReals in them. So every run it will plot a bit different. I want to run the script from a button, but it does not work and in the docs and examples is nothing that has illuminated me. Also on youTube I find tons of movies, but not about how to implement buttons.

This works (from the examples) :

Button["Click Here", Print[10!]]

A simple

Plot[Sin[x], {x, 0, 6 Pi}]

also works,

But why does this NOT work :

Button["Click Here", Plot[Sin[x], {x, 0, 6 Pi}]]

It creates the button, which does not do anything when clicked. I do not get an error, so I have no clue.

It would be great to also get some pointers as to where I can find more info about how to pack more elaborate functions into a button. Also how to save to disc , by Button script, an Image / Graphics that has been created with Mathematica.

Thanks a lot,

4 Replies
Posted 1 month ago

The reason is that Button effectively returns nothing. Here is one way to do it.

plot =.;
Button["Click Here", plot = Plot[Sin[x], {x, 0, 6 Pi*RandomReal[{1, 10}]}]]

Each time the button is clicked the plot is different because of the RandomReal.

You can use Export to save an Image or Graphic. How that is linked to the Button depends on how the Image or Graphic is created.

This also works:

Button["Click Here", 
 Print[ Plot[Sin[x], {x, 0, 6 Pi*RandomReal[{1, 10}]}] ]
Posted 1 month ago

A free on-line Wolfram training course is starting next week covering that subject :

Training course

Posted 1 month ago

Thanks a lot, I am getting it :-)

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract