2

I found some similar questions like these: Creating lambda inside a loop

But I couldn't figure out how to implement in my case.

Here is the simplified code:

i=0
for directory in next(os.walk(_src))[1]:
   Button[i] = Button(frame, text=directory, command= lambda: GotoDir(_src+directory)
   Button[i].grid()
   i=i+1

The problem is that I can't fix the variable directory in each button, it all links to the current directory variable value.

0

1 Answer 1

0

you must change your lambda as follows:

command=lambda dir=_src+directory: GotoDir(dir)

in order to capture the correct arguments for each lambda function created within the for loop

Sign up to request clarification or add additional context in comments.

1 Comment

Quick and precise, implemented and already working, many thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.