0

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

rmore911
  • 195
  • 2
  • 13
  • 1
    Does this answer your question? [Updating OptionMenu from List](https://stackoverflow.com/questions/28412496/updating-optionmenu-from-list) – stovfl Dec 17 '19 at 23:27
  • This does not answer my question. I have a feeling that my question somehow deals with incorrect use of Global variable. But I am not sure. Also, I noticed, for some reason, items of filelist appear in curly brackets in the Label , but on printing flielist, there are no curly brackets – rmore911 Dec 18 '19 at 04:32
  • The **first sentence** from the linked answer: ***"The options in an OptionMenu are not bound to the list from which they are created. Therefore, changing the list does not change the OptionMenu, you'll have to update it yourself."*** – stovfl Dec 18 '19 at 08:10
  • ***"items of filelist appear in curly brackets in the Label"***: This is contradict to your ***"My current code does not display the list of files"*** and indicates your `filelist` is of a type `tkinter` can't handle. [Edit] your question and show the output of `print(filelist)` – stovfl Dec 18 '19 at 08:13
  • ***"But the OptionMenu does not see this updated filelist."***: What did you not understand from my **Second** comment? – stovfl Dec 18 '19 at 20:46
  • Stovfl, thanks for the help. I saw your second comment and it implies that dynamically updating OptionMenu options is not possible. I thought there would be a way in Python to achieve this. – rmore911 Dec 18 '19 at 22:00
  • ***"implies that ... updating ... is not possible"***: That's not the case, follow the answer from the given link and implement *you have to update it yourself*. – stovfl Dec 18 '19 at 22:17

0 Answers0