I am using Tkinter in Python to get user input (Tkinter.Entry
) for a folder-path. When the user clicks a button on the GUI, this path is then accepted (get.Entry
) and a list of files in that path is created (os.listdir
)
I want a drop-down menu OptionMenu
to display this list.
My current code does not display the list of files even when the filelist
variable is non-empty. Why would this be happening?
After the code has completely run, I checked filelist
and found that it is not empty.
Why does the OptionMenu
see it as empty then?
Following is my code:
import os
import tkinter as tk
from tkinter import ttk
from IPython.core.debugger import set_trace
filepath = ""
filename = ""
filelist = [""]
root = tk.Tk()
def click():
global filepath, filename, filelist
filepath = e.get()
filelist = os.listdir(filepath)
myLabel = tk.Label(root,text = filelist).pack()
path = tk.StringVar()
e = tk.Entry(root,textvariable = path)
e.pack()
myButton = tk.Button(root, text = "click", command = click).pack()
optionVar = tk.StringVar()
op = tk.OptionMenu(root,optionVar,*filelist)
op.pack()
root.mainloop()
Pic1 shows the contents of my folder. enter image description here I figured out that the curly brackets seen around some items in the filelist array are because of the spaces in the filenames.
Pic2 shows the output of the script. Print(filelist) by itself shows empty filelist. When the 'click' button is pressed, the Label prints the filelist with three items. But the OptionMenu does not see this updated filelist. enter image description here