Skip to content

Events

Every drag and drop handler receives a pz.DragEvent (or nothing, if the function takes no argument). Handlers run when something important happens (start, enter, a new landing place, leave, drop, end), never every frame.

Field What it is
type "start", "move", "enter", "over", "leave", "drop", "end" or "cancel" ("check" in can_drop).
source The id of the dragged item (a sortable item’s key).
data Its data (event.item is the same).
types Its types (a sortable item’s are the list’s group).
from_list, from_index The sortable it came from and its index there (None for a lone draggable).
copy True when it is a copy: the list copies (pull="clone") or Alt is held.
target The id of the target under it (None: none).
index The landing index in a sortable (counted in the final list).
combine The key of the item it was dropped onto (combine=True), or None.
x, y The pointer, relative to the target (event.position).
item_x, item_y Where the item’s top-left lands in the target, snapped (event.item_position).
window_x, window_y The pointer in the window.
dx, dy How far it moved since the drag started.
width, height The item’s size.
shift, ctrl, alt, logo The modifier keys held.
dropped In on_drag_end: whether a target took it (the type is then "end"; "cancel" otherwise).
within True when it stays in its own list.

Python keeps your data; the event says what changed. Two helpers apply it:

pz.dnd.reorder(items, e.from_index, e.index) # within one list
pz.dnd.move(lists, e) # between lists, by id

pz.dnd.move(lists, event, make=None) takes a dict that maps each sortable’s id to its list (or the one list of a single sortable). The item leaves lists[event.from_list] and goes into lists[event.target] at event.index. Copies (pull="clone") and lone draggables are made with make(event.data) when given, otherwise a deep copy. It returns the item that landed.

def on_reorder(self, e):
if e.from_list == "palette":
self.fields.insert(e.index, new_field(e.data)) # a template becomes a new field
else:
pz.dnd.move({"form": self.fields}, e)

Drops with event.combine are not moved by pz.dnd.move (it returns None): decide what merging means for your app.

pz.dnd.current() # the drag right now, as an event, or None
pz.dnd.cancel() # cancel it, as Esc does