cpython/Doc/library/tkinter.dnd.rst
Serhiy Storchaka 93beea7e5a
gh-153550: Modernize the older prose sections of the tkinter documentation (GH-153647)
Refresh the narrative "Tkinter life preserver" and "Handy reference"
sections to match the overhauled reference (gh-86726 and gh-153549).

* Rework "The packer" into a general "Geometry management" section
  covering grid, pack and place, and cross-link it with the Grid, Pack
  and Place reference classes.
* Rework "The window manager" and "Coupling widget variables", replacing
  the dated App(Frame) examples with direct Tk() and ttk examples.
* Replace page-number citations to Ousterhout's book with links to the
  relevant Tk man pages.
* Modernize the tkinter.ttk front matter and its Tcl/Tk links, and add
  cross-references between the two documents.
* Reconcile the "Packer options" and "Tk option data types" lists with
  the reference, and fill in the empty Entry index list.
* Fix the malformed target-selection list in tkinter.dnd.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-15 19:19:00 +03:00

68 lines
2.2 KiB
ReStructuredText

:mod:`!tkinter.dnd` --- Drag and drop support
=============================================
.. module:: tkinter.dnd
:synopsis: Tkinter drag-and-drop interface
**Source code:** :source:`Lib/tkinter/dnd.py`
--------------
.. note:: This is experimental and due to be deprecated when it is replaced
with the Tk DND.
The :mod:`!tkinter.dnd` module provides drag-and-drop support for objects within
a single application, within the same window or between windows. To enable an
object to be dragged, you must create an event binding for it that starts the
drag-and-drop process. Typically, you bind a ButtonPress event to a callback
function that you write (see :ref:`Bindings-and-Events`). The function should
call :func:`dnd_start`, where *source* is the object to be dragged, and *event*
is the event that invoked the call (the argument to your callback function).
Selection of a target object occurs as follows:
#. Top-down search of the area under the mouse for a target widget:
* the target widget should have a callable *dnd_accept* attribute;
* if *dnd_accept* is not present or returns ``None``,
the search moves to the parent widget;
* if no target widget is found, the target object is ``None``.
#. Call to ``<old_target>.dnd_leave(source, event)``.
#. Call to ``<new_target>.dnd_enter(source, event)``.
#. Call to ``<target>.dnd_commit(source, event)`` to notify of the drop.
#. Call to ``<source>.dnd_end(target, event)`` to signal the end of drag-and-drop.
.. class:: DndHandler(source, event)
The *DndHandler* class handles drag-and-drop events tracking Motion and
ButtonRelease events on the root of the event widget.
.. method:: cancel(event=None)
Cancel the drag-and-drop process.
.. method:: finish(event, commit=0)
Execute end of drag-and-drop functions.
.. method:: on_motion(event)
Inspect area below mouse for target objects while a drag
is performed.
.. method:: on_release(event)
Signal end of drag when the release pattern is triggered.
.. function:: dnd_start(source, event)
Factory function for the drag-and-drop process.
Return the :class:`DndHandler` instance managing the drag, or ``None`` if a
drag could not be started.
.. seealso::
:ref:`Bindings-and-Events`