Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How can I call a function that has a for loop inside a button? If I just write

button[{"name", function[]}]

nothing happens.

share|improve this question
    
I wonder why... What exactly is the function[]? And skip the curly brackets, I don't think they're necessary here. –  Wojciech Dec 16 '13 at 23:17
    
Basically it's a function with a for loop. I'm trying to put a simples for with a print, and it seems that only the first 40s numbers are being executed. –  Zhozer Dec 16 '13 at 23:32
    
Please put all relevant information in the question, rather than in comments. Your posted code contains obvious syntax errors (lowercase "b" and button arguments in a list), and the question states that "nothing happens" when in fact you say in a comment that the loop runs for the first 40 iterations. Most likely your code is timing out and you need to use the Method -> "Queued" option. –  Simon Woods Dec 17 '13 at 14:28

1 Answer 1

up vote 4 down vote accepted

It's hard to tell exactly what the problem is, but here is one way:

f[n_] := Print[Range[n]];
Button["name", f[7]]

Now when you press the button, it prints from 1 to 7 (or whatever number you place in the f[ ]. The action of the function f is invoked whenever you press the button, so it could set values, print, graph things, define variables, and generally do anything that a function does.

If you really want a For loop in the function, that's easy too:

g[n_] := For[i = 0, i < n, i++, Print[i]];
Button["name", g[7]]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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