[Fwd: RE: [wxPython-users] Drag and Drop with custom data -
example required]
Jon
Jon at digital-explosion.co.uk
Wed Feb 28 19:36:55 PST 2007
Fantastic!!
> Potential solution...
>
> -------- Original Message --------
> Subject: RE: [wxPython-users] Drag and Drop with custom data -
> example required
> Date: Wed, 28 Feb 2007 13:18:36 -0000
> From: Gavana, Andrea <gavana at kpo.kz>
> Reply-To: wxPython-users at lists.wxwidgets.org
> To: <wxPython-users at lists.wxwidgets.org>
>
>
> Hi Jon,
>
> > As the subject suggests; I've been banging my head against a
> > brick wall for a couple of days now trying to get a simple
> > drag and drop app working. I want to be able to drag'n'drop
> > some data from a list control to another panel within the
> > same app, but all the tutorials I've found talk about using
> > the standard text versions. If anyone has any experience in
> > dragging and dropping custom data, I'd be extremely grateful!
>
> You could try something like this:
>
> import cPickle
> import weakref
>
> class DragInfo:
>
> _map = weakref.WeakValueDictionary()
>
> def __init__(self, data):
> """ Default class constructor. """
>
> self._id = id(data)
> DragInfo._map[self._id] = data
>
>
> def GetMyData(self):
> """ Returns the data. """
>
> return DragInfo._map.get(self._id, None)
>
>
> class DropTarget(wx.DropTarget):
>
> def __init__(self, parent):
> """ Default class constructor. """
>
> wx.DropTarget.__init__(self)
>
> self._parent = parent
> self._dataobject = wx.CustomDataObject(wx.CustomDataFormat("MySpecialData"))
> self.SetDataObject(self._dataobject)
>
>
> def OnData(self, x, y, dragres):
> """ Handles the OnData() method to call the real DnD routine. """
>
> if not self.GetData():
> return wx.DragNone
>
> draginfo = self._dataobject.GetData()
> drginfo = cPickle.loads(draginfo)
>
> if not drginfo.GetMyData():
> return wx.DragNone
>
> return self._parent.OnDropTarget(x, y, drginfo.GetMyData())
>
>
> # If you defined an event like OnBeginDrag in your ListCtrl
> def OnBeginDrag(self, event):
>
> draginfo = cPickle.dumps(DragInfo(item))
> dataobject = wx.CustomDataObject(wx.CustomDataFormat("MySpecialData"))
> dataobject.SetData(draginfo)
> dragSource = wx.DropSource(self)
> dragSource.SetData(dataobject)
> dragSource.DoDragDrop(wx.Drag_DefaultMove)
> event.Skip()
>
> # Then, in your panel (drop target) you can do:
>
> def OnDropTarget(self, x, y, myData):
> """ Handles the drop action from a DND operation. """
>
> # do whatever you like here with myData
> return wx.DragMove
>
>
> I hope this helps...
>
> Andrea.
I think I've almost got it. You can see the result here:
http://www.hyperlight.co.uk/hypersequencer/HyperGUI_v0_3.py
The only problem left to fix is that when I try to start to drag the
text, I get this:
Started dragging
Traceback (most recent call last):
File "D:\HyperLight Online\v4.0\HyperSequencer_Py\HyperGUI_v3.py",
line 142, in OnBeginDrag
dndData = DnD(text)
TypeError: this constructor takes no arguments
The DnD class will be a holder for the data that I'll be passing across.
I'm quite new to Python so I've probably just overlooked something.. ?
Can anyone spot it?
Thanks very much,
Jon
More information about the wxpython-users
mailing list