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.

1 Answers1

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

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80