0

My code

for i in list_tasks:
    globals()['icon1%s' % i].bind(on_press=lambda x: **print(#here I want to print his name)**

for example globals()['icon1%s' % i] is "icon1clock" I want it to print his own name (icon1clock) on press.

Prophet
  • 32,350
  • 22
  • 54
  • 79

1 Answers1

1

The tricky part is defining a lambda expression that wraps another value, rather than a name. I wouldn't bother trying. Write a function that creates the callback for you; the callback can be a closure over a variable.

def make_callback(name):
    def _(x):
        print(name)
    return _


# *Not* icon1clock = ...
icons = {
    'clock': ..., 
}

for i in list_tasks:
    icons[i].bind(on_press=make_callback(i))
chepner
  • 497,756
  • 71
  • 530
  • 681