python - Binding events to widget not working -


i have function, foo , it's supposed bind event

    def foo:          x in widget_list:               widget.config(command = lambda: self.function(widget_list.index(x))      def function(index):          here....          print index 

the widget list contains buttons, , whenever click corresponding buttons, they're supposed print idle index, happens buttons click end printing last index of widget_list. i'm guessing while iterates, argument of function changes last index preserved. there anyway bind previous indexes button?

the simplest fix make sure lambda using current value of x:

widget.config(command = lambda x=x: self.function(widget_list.index(x)) 

this forces x default parameter function, , default value value of x @ time lambda defined.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -