| 1 |
:mod:`!tkinter.dnd` --- Drag and drop support |
| 2 |
============================================= |
| 3 |
|
| 4 |
.. module:: tkinter.dnd |
| 5 |
:synopsis: Tkinter drag-and-drop interface |
| 6 |
|
| 7 |
**Source code:** :source:`Lib/tkinter/dnd.py` |
| 8 |
|
| 9 |
-------------- |
| 10 |
|
| 11 |
.. note:: This is experimental and due to be deprecated when it is replaced |
| 12 |
with the Tk DND. |
| 13 |
|
| 14 |
The :mod:`!tkinter.dnd` module provides drag-and-drop support for objects within |
| 15 |
a single application, within the same window or between windows. To enable an |
| 16 |
object to be dragged, you must create an event binding for it that starts the |
| 17 |
drag-and-drop process. Typically, you bind a ButtonPress event to a callback |
| 18 |
function that you write (see :ref:`Bindings-and-Events`). The function should |
| 19 |
call :func:`dnd_start`, where 'source' is the object to be dragged, and 'event' |
| 20 |
is the event that invoked the call (the argument to your callback function). |
| 21 |
|
| 22 |
Selection of a target object occurs as follows: |
| 23 |
|
| 24 |
#. Top-down search of area under mouse for target widget |
| 25 |
|
| 26 |
* Target widget should have a callable *dnd_accept* attribute |
| 27 |
* If *dnd_accept* is not present or returns ``None``, search moves to parent widget |
| 28 |
* If no target widget is found, then the target object is ``None`` |
| 29 |
|
| 30 |
2. Call to *<old_target>.dnd_leave(source, event)* |
| 31 |
#. Call to *<new_target>.dnd_enter(source, event)* |
| 32 |
#. Call to *<target>.dnd_commit(source, event)* to notify of drop |
| 33 |
#. Call to *<source>.dnd_end(target, event)* to signal end of drag-and-drop |
| 34 |
|
| 35 |
|
| 36 |
.. class:: DndHandler(source, event) |
| 37 |
|
| 38 |
The *DndHandler* class handles drag-and-drop events tracking Motion and |
| 39 |
ButtonRelease events on the root of the event widget. |
| 40 |
|
| 41 |
.. method:: cancel(event=None) |
| 42 |
|
| 43 |
Cancel the drag-and-drop process. |
| 44 |
|
| 45 |
.. method:: finish(event, commit=0) |
| 46 |
|
| 47 |
Execute end of drag-and-drop functions. |
| 48 |
|
| 49 |
.. method:: on_motion(event) |
| 50 |
|
| 51 |
Inspect area below mouse for target objects while drag is performed. |
| 52 |
|
| 53 |
.. method:: on_release(event) |
| 54 |
|
| 55 |
Signal end of drag when the release pattern is triggered. |
| 56 |
|
| 57 |
.. function:: dnd_start(source, event) |
| 58 |
|
| 59 |
Factory function for drag-and-drop process. |
| 60 |
|
| 61 |
.. seealso:: |
| 62 |
|
| 63 |
:ref:`Bindings-and-Events` |