Being the target of a drag and drop

The steps required to be a drag and drop target roughly mirror the steps involved in being a source. The biggest difference is that on the source side you can use the class wx.DropSource directly; on the target side, you must first write your own custom subclass of wx.DropTarget. Once you have your target class, you'll need to create an instance of it and associate that instance with any wx.Window instance by using the wx.Window method SetDropTarget(target). With a target set, that wx.Window object, whether it's a frame, a button, a text area, or other widget, becomes a valid drop target. In order to receive data on your drop target you must also create a wx.DataObject instance of the desired type and associate it with the drop target with the drop target method SetDataObject(data). You need to predefine the data object before the actual drop operation so that the drop target will be able to negotiate formats correctly. To retrieve the data object from the target, there's a getter method GetDataObject(). The boilerplate code displayed here allows the drop target to receive text, and only text. This is because the data object has been set to an instance of wx.TextDataObject.

class MyDropTarget(wx.DropTarget): = wx.TextDataObject() self.SetDataObject(data)

target = MyDataTarget()


Was this article helpful?

0 0

Post a comment