0

My program searches files from a directory and list those which have a keyword. Now the search result is displayed using a tkinter Label. Need to open the said file on click. However whichever file you click it opens only the last file in the directory. Where is the error?I have attached screenshot of the final output.

Code is:-

if found:
         Filecount=Filecount+1
         if Filecount % 2 == 0:
             bgc="#CCE5FF"
         else:
             bgc="#FFE5CC"
         succ_filename.append(filename)
             
         label4= Label(main,text=(str(Filecount)+'. '+filename),font=("Helvetica 12"),bg=bgc)
         label4.grid(row =5+Filecount,column=1,sticky=W)
         label4.bind("<Button-1>", lambda e:callback(succ_filename[Filecount-1]))

Final output

  • 2
    Does this answer your question? [Tkinter assign button command in loop with lambda](https://stackoverflow.com/questions/17677649/tkinter-assign-button-command-in-loop-with-lambda) – acw1668 Sep 14 '21 at 07:10
  • Get the values of `succ_filename` and `Filecount` not when lambda function defined, but when your lambda function called. Try use default arguments in your lambda function. – Jason Yang Sep 15 '21 at 08:47

0 Answers0