tkinter.dnd --- Drag and drop support — Python 3.15.2 documentation
Navigation index modules | next | previous | Python » 3.15.2 documentation » The Python standard library » Graphical user interfaces with Tk » tkinter.dnd --- Drag and drop support | Theme | tkinter.dnd --- Drag and drop support ¶ Source code: Lib/tkinter/dnd.py Note This is experimental and due to be deprecated when it is replaced with the Tk DND. The 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 Bindings and events ). The function should call 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 tkinter.dnd. DndHandler ( source , event ) ¶ The DndHandler class handles drag-and-drop events tracking Motion and ButtonRelease events on the root of the event widget. cancel ( event = None ) ¶ Cancel the drag-and-drop process. finish ( event , commit = 0 ) ¶ Execute end of drag-and-drop functions. on_motion ( event ) ¶ Inspect area below mouse for target objects while a drag is performed. on_release ( event ) ¶ Signal end of drag when the release pattern is triggered. tkinter.dnd. dnd_start ( source , event ) ¶ Factory function for the drag-and-drop process.
Return the DndHandler instance managing the drag, or None if a drag could not be started. See also Bindings and events Previous topic tkinter.scrolledtext --- Scrolled text widget Next topic tkinter.ttk --- Tk themed widgets This page Report a bug Improve this page Show source Show translation source « Navigation index modules | next | previous | Python » 3.15.2 documentation » The Python standard library » Graphical user interfaces with Tk » tkinter.dnd --- Drag and drop support | Theme | © Copyright 2001 Python Software Foundation.
This page is licensed under the Python Software Foundation License Version 2. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License. See History and License for more information. The Python Software Foundation is a non-profit corporation. Please donate. Last updated on ਨਵੰ 19, 2026 (09:42 UTC). Found a bug ? Created using Sphinx 9.1.1.