from tkinter import *
def fun(nm):
msg= 'hello {}'.format(nm)
print(msg)
master=Tk(className="testing")
list=[1,2,3,4]
for x in list:
para=x
Button(master, text=x, command=lambda:fun(x)).pack(side='left')
master.mainloop()
getting output hello 4 every time
Closures in Python use late-binding, so the value of x isn’t looked up inside the closure until the function is called. But default values use early binding, so you can use a default value to get the effect you want.
https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result