I have a command lets say

f(x):
    print x

I want to create several buttons to print various numbers
so I made a list of the objects to print
when I tried to do

for x in thislist:
    bind_all(x,lambda:f(x))

which bound the buttons properly but all the buttons print the same thing.
any help?

I'm using Python2.7 and TKinter

Use functools.partial:

import functools

def f(x):
    print(x)

thislist = range(10)

actions = [functools.partial(f, x) for x in thislist]

for action in actions:
    action()

functools looks like it could be useful thanks pyTony

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.