tkdnd::drop_target
Makes a widget a target for drops of files and text, with the names of the tkdnd package of Tk.
Synopsis
tkdnd::drop_target register WINDOW ?TYPES?
tkdnd::drop_target unregister WINDOW
Arguments
| Name | Type | Default | Meaning |
|---|---|---|---|
register WINDOW |
subcommand | Puts the widget on the list of drop targets, or changes its types. | |
TYPES |
list | * |
The kinds of data that the widget accepts: DND_Files, DND_Text, or * for both. |
unregister WINDOW |
subcommand | Removes the widget from the list. A widget not on the list gives no error. |
A drop goes to the widget under the pointer, or to the nearest parent that accepts the type of the data. The target gets these virtual events:
| Event | When | %D |
|---|---|---|
<<DropEnter>> |
A drag comes over the target. | empty |
<<DropPosition>> |
The drag moves over the target. | empty |
<<DropLeave>> |
The drag goes off the target, the user cancels it, or the user drops. It comes before the drop events. | empty |
<<Drop:DND_Files>> |
The user drops files. | A list of the full paths, with / between the parts. |
<<Drop:DND_Text>> |
The user drops text. | The text. |
<<Drop>> |
After the typed event of a drop. | As in the typed event. |
%d is the same as %D. %x and %y are the place of the pointer in the target, and %X and %Y are the place in the toplevel.
Return value
An empty string.
Errors
| Error | When |
|---|---|
bad window path name "W" |
No widget has the name. |
bad type "T": must be DND_Files, DND_Text, or * |
An unknown type. |
bad option "X": must be register or unregister |
An unknown subcommand. |
wrong # args: should be "tkdnd::drop_target register|unregister window ?types?" |
No window. |
Platforms
| Platform | Works |
|---|---|
| Windows | Yes: drops from the app and from other apps, through OLE. |
| macOS | Yes: drops from the app, and file URLs and plain text from other apps. |
| Linux, Wayland | Yes: drops from the app and from other apps, through the data device of the compositor. |
| Linux, X11 | Yes: drops from the app only. |
| Web | Yes: drops from the app, and files and text from the desktop and from other pages. |
| iOS, Android | Yes: drops from the app only. |
| Headless | Yes: drops from the app and from event generate. |
Example
label .drop -text "Drop files here" -width 30 -height 6 -relief groove
pack .drop -padx 20 -pady 20
tkdnd::drop_target register .drop DND_Files
bind .drop <<Drop:DND_Files>> {.drop configure -text "[llength %D] files"}
event generate .drop <<Drop:DND_Files>> -data [list /tmp/a.txt /tmp/b.png]
puts [.drop cget -text]