0

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

Edited by Thropian: n/a

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Thropian
0

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()
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.