You're currently calling show_info()
before creating the button, which is why it prints the results for each iteration of the loop. You can use a lambda expression to pass an argument, and assign a default argument of i
to each command (otherwise only the last known value of i
will be set to each button)
e=tk.Button(sw.window,height=2,width=40,text=text,anchor=W,
command=lambda i=i: show_info(i))
That should fix that problem, but one other thing to mention is that you can remove return result
from the show_info()
function, because it's not returning to anything. Hope that helps.
More about buttons and callbacks: http://effbot.org/zone/tkinter-callbacks.htm