Dynamically setting callbacks with arguments in python
<p>I'm trying to dynamically build some buttons in tkinter from a set of data stored in list form. The thing I can't work out is how to put an argument in the callback function. As the code in the callback isn't executed until the time of the callback, the variable used in the callback has changed value by this point.</p>

<p>Here's a (very) simplified version of the code:</p>

<pre><code>from Tkinter import *
from ttk import *

mylist = ['a','b','c','d','e']


class App:

    def __init__(self, master):

        self.frame = Frame(master)
        self.frame.pack()

        for i in range (0,len(mylist)):
            setattr(self, 'button' + str(i), Button(self.frame, text=mylist[i], command= lambda: self.buttoncall(i)))
            getattr(self, 'button' + str(i)).pack(side=LEFT)

    def buttoncall(self,input):
        print mylist[input]

root = Tk()

app = App(root)

root.mainloop()
</code></pre>
