0

I am currently trying to enable drag and drop files in my tkinter application. This is my code :

# Python 3.10
from tkinter import *
from tkinter.dnd import *


root = Tk()
root.title("DnD Test")
root.geometry("400x400")


path_entry = Entry(root)
path_entry.pack()

def on_dnd(event):
    path_entry.delete(0, END)
    path_entry.insert(0, event.data)

dnd_start(root, on_dnd) # Error here

root.mainloop()

The problem is that this code return me the error AttributeError: 'function' object has no attribute 'num'

So I guess it's not a function that should be put in the second argument of dnd_start but I haven't found what it should be

crazycat256
  • 348
  • 2
  • 11
  • According to the documentation, the second argument to `dnd_start` should be an event, not a function. – Bryan Oakley Sep 21 '22 at 15:32
  • @BryanOakley And do you know how I can get an event? – crazycat256 Sep 21 '22 at 15:37
  • You would bind to an event, such as a ``. The callback will receive the event object. This is mentioned in [the documentation](https://docs.python.org/3/library/tkinter.dnd.html), though I don't this module is designed for dropping files from the OS. I've never used it, but the documentation implies it's for dragging items between windows or within a window. – Bryan Oakley Sep 21 '22 at 15:58
  • That documentation is terrible – Matiiss Sep 21 '22 at 16:04
  • @crazycat256 If you look a the actual `tkinter/dnd.py` file, you will find an [example](https://github.com/python/cpython/blob/4b81139aac3fa11779f6eedb6e621bde29e64b30/Lib/tkinter/dnd.py#L304). It isn't doesn't have anything to do with files. If you want to drag and drop files, look at [this](https://stackoverflow.com/q/25427347/11106801) but I only used it once (3 years ago) so I can't really help. – TheLizzard Sep 21 '22 at 16:12
  • So to activate drag and drop it is necessary to use an external module? – crazycat256 Sep 21 '22 at 20:18
  • If you want to drag external objects onto a tkinter window, yes, you have to use an external module. Tkinter itself doesn't not have to ability to accept files dropped on it from the OS. – Bryan Oakley Sep 21 '22 at 21:49

0 Answers0