0

Im trying to make a File list in Tkinter, i used the code

for Base,dir,Files in os.walk(str(Proj_Selected)):
        for File in enumerate(Files,start=0):
            _File = File[0]
            print(File)

            FileButton[str(_File)] = Button(ToolBarb,text=Files[_File],bg='#383838',fg='white',font=("Bold", 3),highlightthickness=0,borderwidth=0,command= lambda: CodeUpdate(File[1]))

            FileButton[str(_File)].grid(column=1*_File,row=0,padx=1)      

the files are

(0, 'main.py')
(1, 'DateCreated.txt')
(2, 'Password.txt')
(3, 'permissions.conf')

but the output of the function is all the same

Projects/fils/
permissions.conf

Projects/fils/
permissions.conf

Projects/fils/
permissions.conf

Projects/fils/
permissions.conf

The function is

def CodeUpdate(file):
    global textcore,Proj_Selected
    Proj = Proj_Selected
    code = open(Proj+file).read()
    print(Proj)
    print(file)
    print(code)
    textcore.insert(INSERT,code)
    textcore.update_idletasks()
  • I forgot to add, but how would i fix this so when the button is pressed it shows the right file names not just the same one – Roberto E. Torres Aug 07 '22 at 06:30
  • Indeed, the lambda function will only try to access the File variable when the lambda is called. By that time the loop has reached the last File. Compare with the "Late Binding Closures" section of https://docs.python-guide.org/writing/gotchas/ – qrsngky Aug 07 '22 at 07:16

0 Answers0