Typically when a button uses a variable in its action, it uses the value of that variable at the time the button is triggered. I would like to create a button that stores the information in a variable at the time of its creation for later use in its action. How can this be done?
As a very simple example, If I write the Mathematica code:
Do[Print[Button["press", Print[i]]], {i, 1, 5}]
I get 5 buttons, but rather than printing the numbers 1 to 5 respectively, they print just i, the value of i outside the loop rather than the value of i when the button was created.
I am neither a beginner nor a particularly experienced Mathematica user, but I have already encountered this issue a few times.
Sorry for the pre-mature post β I thought I was really stuck but I found a solution with a command I had previously not understood well β the command With. For example
This does now print numbers 1 through 5 depending upon the button pressed. I should be able to extend this solution to the more complex situations where I really needed it.