In the code below I intend to have two buttons, and when each is pressed '0' and '1' are to be printed to stdout, respectively. However when the program is run, they both print '1', which is the last value i had in the for iteration. Why?
import Tkinter as tk
import sys
root = tk.Tk()
for i in range(0,2):
cmd = lambda: sys.stdout.write(str(i))
tk.Button(text="print '%d'" % i,command=cmd).pack()
root.mainloop()