【问题标题】:Select all the values from a drag and drop Listbox in pythonSelect all the values from a drag and drop Listbox in python
【发布时间】:2022-12-02 04:15:29
【问题描述】:

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)

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    Without using button.

    Try this:

    from tkinter import *
    from TkinterDnD2 import *
    
    def path_listbox(event):
        listbox.insert("end", event.data)
    
    window = TkinterDnD.Tk()
    window.title('Delftstack')
    window.geometry('400x300')
    window.config(bg='gold')
    frame = Frame(window)
    frame.pack()
    
    listbox = Listbox(
        frame,
        width=50,
        height=15,
        selectmode=SINGLE,
        )
    listbox.pack(fill=X, side=LEFT)
    listbox.drop_target_register(DND_FILES)
    listbox.dnd_bind('<<Drop>>', path_listbox)
    
    scrolbar= Scrollbar(
        frame,
        orient=VERTICAL
        )
    scrolbar.pack(side=RIGHT, fill=Y)
    # displays the content in listbox
    listbox.configure(yscrollcommand=scrolbar.set)
    # view the content vertically using scrollbar
    scrolbar.config(command=listbox.yview)
    
    window.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2022-12-02
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 2021-12-27
      • 2022-12-02
      • 2022-12-02
      相关资源
      最近更新 更多