Select all the values from a drag and drop Listbox in python

Question:

I am trying to create a gui in tkinter where I will have a Listbox and be able to drag and drop files into it. How can I store all the items inside this listbox in a list with a command on the button?

lb = tk.Listbox(root, height=8)
lb.drop_target_register(DND_FILES)
lb.dnd_bind("<<Drop>>", lambda e: lb.insert(tk.END, e.data))
lb.grid(row=1, column=0, sticky="ew")

btn = ttk.Button(root, text="Submit")
btn.grid(row=2,column=0)
Asked By: DrGenius

||

Answers:

First you need to assign a callback to the command option of the button, then you can use lb.get(0, tk.END) to get the item list inside the callback:

...
def submit():
    # in case you need to access itemlist outside this function
    global itemlist
    # get all the items inside the listbox to itemlist
    itemlist = lb.get(0, tk.END)
    print(itemlist)

# assign a callback via command option
btn = ttk.Button(root, text="Submit", command=submit)
btn.grid(row=2, column=0)
...
Answered By: acw1668
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.