beetlebug

Drag and drop

This guide shows drops of files and text on a widget, drags from a widget to other apps, and drags between widgets of one app. A widget accepts drops with tkdnd::drop_target, and a drag starts with ztk::dnd drag.

Accept files from a file manager

register puts a widget on the list of drop targets. types is a list of DND_Files, DND_Text, or * (both). The default is *. unregister removes the widget from the list.

When the user drops files on the widget, it gets <<Drop:DND_Files>>. %D is a Tcl list of the full paths of the files, with / between the parts on each platform. ztk::dnd read path gives the bytes of a file as a binary string. It also works in a browser, where a dropped file has no path on the disk (In a browser).

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>> {
    set lines {}
    foreach path %D {
        set name [lindex [split $path /] end]
        lappend lines "$name: [string length [ztk::dnd read $path]] bytes"
    }
    .drop configure -text [join $lines \n]
}

A dropped image shows with image create photo -data [ztk::dnd read $path].

A drop goes to the widget under the pointer. When that widget is not a target for the type of the data, the drop goes to the nearest parent that is. So a target frame also gets the drops on its children.

Accept text

A target of DND_Text gets <<Drop:DND_Text>>, and %D is the text. This text widget inserts the dropped text at the end:

text .notes -width 40 -height 8
pack .notes -padx 10 -pady 10
tkdnd::drop_target register .notes DND_Text
bind .notes <<Drop:DND_Text>> {.notes insert end %D}

One target can take both types. The name of the typed event gives the type. <<Drop>> comes after the typed event of each drop.

label .any -text "Drop files or text" -width 30 -height 4 -relief groove
pack .any -padx 20 -pady 20
tkdnd::drop_target register .any {DND_Files DND_Text}
bind .any <<Drop:DND_Files>> {.any configure -text "[llength %D] files"}
bind .any <<Drop:DND_Text>> {.any configure -text "[string length %D] characters"}

To try a binding with no drag, send the event to the widget. -data gives %D:

event generate .any <<Drop:DND_Files>> -data [list /tmp/a.txt /tmp/b.png]

Show where a drop goes

A target gets <<DropEnter>> when a drag comes over it, <<DropPosition>> as the drag moves over it, and <<DropLeave>> when the drag goes off it, the user cancels the drag, or the user drops. Use them to mark the target:

label .bin -text "Drop here" -width 30 -height 6 -relief groove -background white
pack .bin -padx 20 -pady 20
tkdnd::drop_target register .bin
bind .bin <<DropEnter>> {.bin configure -background lightyellow -relief sunken}
bind .bin <<DropLeave>> {.bin configure -background white -relief groove}
bind .bin <<Drop>> {.bin configure -text "Got it"}

At a drop, <<DropLeave>> comes first, so the look is back before <<Drop>> runs.

Drag files or text from a widget

ztk::dnd drag starts a drag from a binding of <B1-Motion>. The drag ends when the user releases the button over a target, or cancels the drag. Then -command runs with the action of the target appended: copy, move, or none.

Option Value Default
-files list Full host paths of files to drag. none
-text string Text to drag. none
-move bool 1 offers a move to the target as well as a copy. 0
-command script Runs at the end of the drag, with the action appended. none

On Windows, macOS, and Wayland, the drag is a drag of the system, and it can go to other apps. ztk::dnd drag then runs until the drag ends, and it also gives the action as its result. On the other platforms, the drag goes only to the targets of the app. ztk::dnd drag then returns at once with an empty result, and the drag follows the pointer until the release. Escape cancels it. ztk::has drag-out is 1 when a drag can go to other apps. Use -command for the action, so that one script works on each platform.

This label drags a file to a file manager. The drag starts after the pointer moves 6 pixels with the button down, so a click stays a click:

set path [file normalize notes.txt]
label .file -text [file tail $path] -relief raised -padx 8 -pady 4
pack .file -padx 20 -pady 20

bind .file <ButtonPress-1> {set ::start [list %x %y]}
bind .file <B1-Motion> {dragFile %x %y}
proc dragFile {x y} {
    lassign $::start x0 y0
    if {abs($x - $x0) + abs($y - $y0) < 6} return
    ztk::dnd drag .file -files [list $::path] -move 1 -command dragEnded
}
proc dragEnded {action} {
    if {$action eq "move"} { .file configure -text "Moved" }
}

For text, give -text. In an entry, <B1-Motion> selects text, so this entry starts the drag with the Alt key:

entry .quote -width 30
.quote insert 0 "Text to drag"
pack .quote -padx 20 -pady 20
bind .quote <Alt-B1-Motion> {ztk::dnd drag .quote -text [.quote get]}

Drag between widgets of one app

A drop target of the app gets a drag of ztk::dnd drag from another widget of the app. The events and the data are the same as for a drop from another app. This pair of lists moves a task from one list to the other. The list under the pointer changes its color during the drag:

listbox .todo -height 6 -exportselection 0
listbox .done -height 6 -exportselection 0
.todo insert end "Write the report" "Call the bank" "Water the plants"
pack .todo .done -side left -padx 10 -pady 10

foreach lb {.todo .done} {
    tkdnd::drop_target register $lb DND_Text
    bind $lb <ButtonPress-1> {set ::from [list %W [%W nearest %y]]}
    bind $lb <B1-Motion> {startDrag}
    bind $lb <<DropEnter>> {%W configure -background lightyellow}
    bind $lb <<DropLeave>> {%W configure -background white}
    bind $lb <<Drop:DND_Text>> {
        if {"%W" ne [lindex $::from 0]} { %W insert end %D; set ::moved 1 }
    }
}
proc startDrag {} {
    lassign $::from lb index
    if {[$lb size] == 0} return
    set ::moved 0
    ztk::dnd drag $lb -text [$lb get $index] -move 1 -command [list endDrag $lb $index]
}
proc endDrag {lb index action} {
    if {$action eq "move" && $::moved} { $lb delete $index }
}

On a touch screen, a finger is the pointer with button 1, so the same bindings drag with a finger.

A drag inside one widget can use the bindings of the mouse only. The widget of the press gets each <B1-Motion> and the <ButtonRelease-1>, also when the pointer is over another widget. winfo containing %X %Y gives the widget under the pointer.

This list changes the order of its items as the user drags an item up or down:

listbox .list -height 5
.list insert end Apples Bread Cheese Dates Eggs
pack .list -padx 10 -pady 10

bind .list <ButtonPress-1> {set ::from [.list nearest %y]}
bind .list <B1-Motion> {
    set to [.list nearest %y]
    if {$to != $::from} {
        set item [.list get $::from]
        .list delete $::from
        .list insert $to $item
        .list selection clear 0 end
        .list selection set $to
        set ::from $to
    }
}

In a browser

In the web build, the canvas of the app accepts drops of files and text from the desktop and from other pages, when a widget of the app is a drop target.

A drag of ztk::dnd drag goes to the drop targets of the app, by mouse and by touch. A drag out of the page to the desktop or to another page is not available. A browser starts such a drag only from its own drag gesture on an element of the page, and the canvas draws the widgets as pixels, so the browser has no element to drag.

See also