Drag and drop explorer files to tkinter entry widget?

前端 未结 2 1815
时光取名叫无心
时光取名叫无心 2020-11-27 15:51

I\'m fairly new to Python. I\'m trying to input a file name (complete with full path) to a TKinter entry widget. Since the path to the file name can be very long I would l

2条回答
  •  醉话见心
    2020-11-27 16:36

    Things have progressed since this question was originally posted, and tkdnd2.8 (Tcl extensions) and TkinterDnD2 (Python wrapper for tkdnd2.8) are readily available on SourceForge.net.

    Here's minimal sample code to do exactly what you've asked.

    import Tkinter
    from TkinterDnD2 import *
    
    def drop(event):
        entry_sv.set(event.data)
    
    root = TkinterDnD.Tk()
    entry_sv = Tkinter.StringVar()
    entry = Tkinter.Entry(root, textvar=entry_sv, width=80)
    entry.pack(fill=Tkinter.X)
    entry.drop_target_register(DND_FILES)
    entry.dnd_bind('<>', drop)
    root.mainloop()
    

    You can see How to Install and Use TkDnD with Python 2.7 Tkinter on OSX for download and installation info for both Windows and Mac on Python 2.7 and 3.6.

提交回复
热议问题