I want to be able to drag and drop URL’s, files and
text into the same window (wx.ListCtrl). I can currently drag and drop each of
these separately by:
instantiating wx.TextDropTarget to get text data
instantiating wx.FileDropTarget to get file data
instantiating wx.PyDropTarget to get URL’s
However I can’t work out how to create a drop target
for all three data types
I tried to use an instance of wx.PyDropTarget as follows:
class dropTarget(wx.PyDropTarget):
def init(self,
window):
wx.PyDropTarget.init(self)
self.window = window
self.comp =
wx.DataObjectComposite()
self.comp.Add(wx.FileDataObject())
self.comp.Add(wx.URLDataObject())
self.SetDataObject(self.comp)
This gives the following ‘Type’ error:
TypeError: in method
‘DataObjectComposite_Add’, expected argument 2 of type ‘wxDataObjectSimple *’
I then tried to Add() wx.FileDataObject() to wx.URLDataObject()
because it is composite data object but that gave the following error:
AttributeError: ‘URLDataObject’ object has
no attribute ‘Add’
I would really appreciate any advice on this matter.
I want to be able to drag and drop URL’s,
files and text into the same window (wx.ListCtrl). I can currently drag and
drop each of these separately by:
instantiating wx.TextDropTarget to get text data
instantiating wx.FileDropTarget to get file data
instantiating wx.PyDropTarget to get URL’s
However I can’t work out how to create a drop
target for all three data types
I tried to use an instance of wx.PyDropTarget as
follows:
class dropTarget(wx.PyDropTarget):
def init(self, window):
wx.PyDropTarget.init(self)
*self*.window = window
*self*.comp = wx.DataObjectComposite()
self.comp.Add(wx.FileDataObject())
self.comp.Add(wx.URLDataObject())
self.SetDataObject(self.comp)
This gives the following ‘Type’ error:
TypeError:
in method ‘DataObjectComposite_Add’, expected argument 2 of type
‘wxDataObjectSimple *’
I then tried to Add() wx.FileDataObject() to
wx.URLDataObject() because it is composite data object but that gave the
following error:
AttributeError:
‘URLDataObject’ object has no attribute ‘Add’
···
This is the right approach, I believe, but Add() takes
two arguments. The first a data object, the second a boolean flag
“preferred”.
So you’d do:
self.comp.Add(wx.FileDataObject(), False)
etc.
–Stephen
Added boolean flags as Stephen suggested, unfortunately I am
still getting the same error:
TypeError: in method
‘DataObjectComposite_Add’, expected argument 2 of type ‘wxDataObjectSimple *’
The Add() method doesn’t seem to be accepting wx.URLDataObject() or wx.FileDataObject() because
they aren’t ‘wxDataObjectSimple’
So I guess you are running on msw, wxURLDataObject is not a subclass of wxDataObjectSimple and thus your code does not work. It seems the msw version of wxURLDataObject is already a composite object itself. So maybe you want to do something like
The docs of wxURLDataObject say this:
Derived from
Under MSW:
wxDataObjectComposite
wxDataObject
Under the other platforms:
wxTextDataObject
wxDataObjectSimple
wxDataObject
So I guess you are running on msw, wxURLDataObject is not a subclass of
wxDataObjectSimple and thus your code does not work. It seems the msw
version of wxURLDataObject is already a composite object itself. So maybe
I've never tried this myself, just a guess from what I gathered from the
docs.
_____________________________________
Thanks Matthais, I remember reading that 'wxURLDataObject is already a
composite object itself' in a post from Robin Dunn and have tried your
approach. However it gives the following error:
AttributeError: 'URLDataObject' object has no attribute 'Add'
The code below works fine when the Add() method is not used:
class dropTarget(wx.PyDropTarget):
def __init__(self, window):
wx.PyDropTarget.__init__(self)
self.window = window
self.data = wx.URLDataObject()
self.data.Add(wx.FileDataObject(),False) <--- doesn't work
self.SetDataObject(self.data)
You are correct in assuming that I am using msw (windows XP). As you stated,
given that wxURLDataObject is derived from wxDataObjectComposite under MSW,
I should be able to Add() to it. Don't suppose there are any other ideas?