I'm using Tkinter for the code below.
Goal
Use filedialog to input the path of a folder to search and return the list of "mp3" files from the folder and put all the "mp3" in one OptionMenu.
from tkinter import StringVar, filedialog
import os
import tkinter
root = tkinter.Tk()
path = filedialog.askdirectory()
try:
if not path:
print('Canceled')
else:
mp3 = [fn for fn in os.listdir(path) if fn.lower().endswith('.mp3') and os.path.isfile(os.path.join(path, fn))]
for filename in mp3:
clicked = StringVar()
tkinter.OptionMenu(root, clicked, filename).pack()
except:
pass
root.mainloop()
The code works.
But It's giving me more than 1 OptionMenu.