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

Time:12-03

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)

CodePudding user response:

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)
...
  • Related