I have a code as the following from this link:
from tkinter import *
from tkinter.filedialog import askdirectory
import os
def onEnterDir(dropdown, var):
path = askdirectory()
if not path:
return
filenames = os.listdir(path)
dropdown.configure(state='active') # Enable drop down
menu = dropdown['menu']
# Clear the menu.
menu.delete(0, 'end')
for name in filenames:
# Add menu items.
menu.add_command(label=name, command=lambda: var.set(name))
root = Tk()
dropdownVar = StringVar()
dropdown = OptionMenu(root, dropdownVar, "Select SED...")
dropdown.grid(column=0, row=1)
dropdown.configure(state="disabled")
b = Button(root, text='Change directory',
command=lambda: onEnterDir(dropdown, dropdownVar))
b.grid(column=1, row=1)
root.mainloop()
After running the program we get a GUI where we can select a directory then its contents are shown in the option menu. When we choose one of the items, just the last item is selected.
Would anybody help me to figure out what is the issue?