wxPython Drag & Drop - wxPython

How Drag & Drop works in wxPython?

Drag and drop option is very useful to users and it is found in many desktop applications. It allows to copy or move objects from one window to another just by dragging it with the mouse and dropping on another window.

Drag and drop operation involves below steps

  • Declare a drop target
  • Create data object
  • Create wx.DropSource
  • Execute drag operation
  • Cancel or accept drop

In wxPython, there are two predefined drop targets

  • wx.TextDropTarget
  • wx.FileDropTarget

Many wxPython widgets support drag and drop activity. Source control should have dragging enabled whereas target control should be in a position to accept (or reject) drag.

Source Data that the user is dragging will be placed on the the target object. OnDropText() of target object will consume the data. If required, data from the source object can be deleted.


In below example, two ListCrl objects are placed horizontally in a Box Sizer. List on the left is populated with a languages[] data and is selected as the source of drag one on the right is the target.

Second list control is empty and is an argument for object of TextDropTarget class.

OnDropText() method will add source data in the target list control.

Drag operation is initialized by the event binder.

OnDragInit() function puts drag data on the target and deletes from the source.

Complete code is as follows

Above code produces the following output


All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

wxPython Topics