6

Assumption: I'm using Python 3.6 and I'm working on Windows 10

Is possible to create a GUI with tkinter in which dragging a file in the window it returns the path of the file?

If with tkinter it's not possible, is there another solution that can solve the problem without installing additional libraries?

Luca
  • 160
  • 3
  • 14
  • No, that's not possible, you'll need to install TkinterDnD2 as shown in https://stackoverflow.com/a/46865239/4014959 – PM 2Ring Jan 06 '18 at 14:19
  • In the link's answer, he talks about Python 2.6 and OsX, but nothing that could be useful for me – Luca Jan 06 '18 at 14:25
  • Does windows send a broadcast when the user try to use the drag n' drop system? EDIT: Is possible to get it via sys? – Luca Jan 06 '18 at 14:26
  • Sorry, I don't know much about Windows. According to http://tkinterdnd.sourceforge.net/ TkinterDnD2 works with Windows, Unix and Mac OSX, with both Python 2 and Python 3. – PM 2Ring Jan 06 '18 at 14:51

1 Answers1

1

You need to install tkinterdnd2

pip install tkinterdnd2

code:

from tkinter import TOP, Entry, Label, StringVar
from tkinterdnd2 import *


def get_path(event):
    pathLabel.configure(text = event.data)

root = TkinterDnD.Tk()
root.geometry("350x100")
root.title("Get file path")

nameVar = StringVar()

entryWidget = Entry(root)
entryWidget.pack(side=TOP, padx=5, pady=5)

pathLabel = Label(root, text="Drag and drop file in the entry box")
pathLabel.pack(side=TOP)

entryWidget.drop_target_register(DND_ALL)
entryWidget.dnd_bind("<<Drop>>", get_path)

root.mainloop()