I have made a text-file management system in Python3 using Tkinter and SQLite3. Every user's files are stored in a database. On the main screen when a user is logged in, a for-loop iterates over the file-names and creates a button for every file. Each button has to open up the file corresponding to its name.
The problem is, all the buttons open the file corresponding to the last button, instead of the file name corresponding to them.
To be more clear, here's my code:
for i in viewdata:
doc=Button( mainframe, image=docimg, bd=0,cursor='hand1', command=lambda: openexistingfile(str(i[0])))
doc.place(x=cnt, y=70)
doc.image=docimg
Label(mainframe, text=str(i[0]), font='Times 12 italic', bg='white').place(x=cnt, y=130)
cnt+=120
print(str(i[0]))
Suppose a user has 3 files, sample1, sample2 and sample3. So here, viewdata = [(sample1, ), (sample2,), (sample3,)]. Now, three buttons would be created, each opening a file. However, all the buttons are opening sample3 for some reason.
The last print statement prints the file names correctly and in the correct format, it's only the Button command I'm having an issue with.
Any help would be appreciated. Thank you.