'Tkinter assign button command in a for loop with lambda [duplicate]

I'm trying to create a few Buttons (with a for loop) like so:

def a(self, name):
    print(name)

users = {"Test": "127.0.0.0", "Test2": "128.0.0.0"}
row = 1
for name in users:
    user_button = Tkinter.Button(self.root, text=name,
                                 command=lambda: self.a(name))
    user_button.grid(row=row, column=0)
    row += 1

and for the buttons to each get their own parameter (Test getting "Test" and Test2 getting "Test2"), but when I press the buttons they both print "Test2" which means they are using the same function with the same parameter.

How can I solve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source