Hi,
wxPython is great! But it looks like it doesn't allow drag'ndrop in
wxTreeCtrl. I attempt to handle EVT_TREE_BEGIN_DRAG but
event.IsAllowed() returns 0 and event does not seem to have the Allow()
function.
Run:
C:\>python treectrldnd.py
(see source below)
Then drag an item in the tree ctrl to produce this dump to stdout:
OnTreeBeginDrag: IsAllowed 0
event.Allow() threw exception: 'wxTreeEventPtr' instance has no
attribute 'Allow'
event object: <C wxTreeEvent instance at _63f224_wxTreeEvent_p>
(Running Python20, Windows98, wxPython222)
Is there a better way to do DnD than the way I am trying? Is this in
the TODO list?
TIA,
Jack.
##treectrldnd.py -- butchered version of CustomDragAndDrop.py
from wxPython.wx import *
···
#----------------------------------------------------------------------
class DropSourceTreeCtrlPanel(wxPanel):
def __init__(self, parent, log):
wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS)
EVT_SIZE(self, self.OnSize)
self.log = log
tID = NewId()
self.tree = wxTreeCtrl(self, tID, wxDefaultPosition,
wxDefaultSize,
wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS)
self.root = self.tree.AddRoot("The Root Item")
for x in range(5):
child = self.tree.AppendItem(self.root, "Item %d" % x)
for y in range(5):
last = self.tree.AppendItem(child, "item %d-%s" % (x,
chr(ord("a")+y)))
for z in range(5):
item = self.tree.AppendItem(last, "item %d-%s-%d" %
(x, chr(ord("a")+y), z))
self.tree.Expand(self.root)
EVT_TREE_BEGIN_DRAG(self.tree, tID, self.OnTreeBeginDrag)
def OnSize(self, event):
w,h = self.GetClientSizeTuple()
self.tree.SetDimensions(0, 0, w, h)
def OnTreeBeginDrag(self, event):
self.log.WriteText("OnTreeBeginDrag: IsAllowed %s\n" %
`event.IsAllowed()`)
try:
event.Allow()
except:
print "event.Allow() threw exception: ", sys.exc_value
print "event object:", event
sel = self.tree.GetSelection()
text = self.tree.GetItemText(sel)
data = wxCustomDataObject(wxCustomDataFormat("x"))
data.SetData(text)
dropSource = wxDropSource(self)
dropSource.SetData(data)
result = dropSource.DoDragDrop()
#----------------------------------------------------------------------
class DropTarget(wxPyDropTarget):
def __init__(self, window, log):
wxPyDropTarget.__init__(self)
self.log = log
self.window = window
# specify the type of data we will accept
self.data = wxCustomDataObject(wxCustomDataFormat("x"))
self.SetDataObject(self.data)
def OnDrop(self, x, y):
self.log.WriteText("OnData: %d, %d, %d\n" % (x, y, d))
# copy the data from the drag source to out data object
dc = wxClientDC(self.window)
dc.BeginDrawing()
dc.SetPen(wxPen(wxBLUE, 3))
dc.DrawLine(x,y,x+20,y)
dc.EndDrawing()
return true
class DropTargetWindow(wxWindow):
def __init__(self, parent, log):
wxWindow.__init__(self, parent, -1, style=wxSUNKEN_BORDER)
self.log = log
self.SetBackgroundColour(wxWHITE)
self.lines = []
self.x = self.y = 0
dt = DropTarget(self, log)
self.SetDropTarget(dt)
#----------------------------------------------------------------------
class CustomDnDPanel(wxPanel):
def __init__(self, parent, log):
wxPanel.__init__(self, parent, -1)
sizer = wxBoxSizer(wxHORIZONTAL)
sizer.Add(DropSourceTreeCtrlPanel(self, log), 1, wxEXPAND|wxALL,
5)
sizer.Add(DropTargetWindow(self, log), 1, wxEXPAND|wxALL, 5)
self.SetAutoLayout(true)
self.SetSizer(sizer)
#----------------------------------------------------------------------
if __name__ == '__main__':
import sys
class DummyLog:
def WriteText(self, text):
sys.stdout.write(text)
class TestApp(wxApp):
def OnInit(self):
frame = wxFrame(None, -1, "Custom Drag and Drop",
size=(550,400))
panel = CustomDnDPanel(frame, DummyLog())
frame.Show(true)
self.SetTopWindow(frame)
return true
app = TestApp(0)
app.MainLoop()
#----------------------------------------------------------------------
_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/mailman/listinfo/wxpython-users