wx.TreeCtrl drag and drop, copy and move

∥☆過路亽.° 提交于 2019-12-22 18:20:09

问题


I'm trying to implement drag and drop on a wx.TreeCtrl and I need to handle both "copy" and "move" operations (if the user keeps CTRL pressed).
First of all, I searched the wiki for an example and I'm confused as to which method to use.. Should I use DropSource/DropTarget or just handle EVT_TREE_BEGIN_DRAG and EVT_TREE_END_DRAG?
If the latter, how can I tell if the user is requesting a "move" operation?

(wxPython 2.8.9.1 on Ubuntu Jaunty)


回答1:


Reading the relevant paragraph from Cross-Platform GUI Programming with wxWidgets gave me the necessary insight to solve the issue :)
In the end I went for the first solution (DropSource/DropTarget), so:

tree.SetDropTarget(MyDropTarget())
tree.Bind(wx.EVT_TREE_BEGIN_DRAG, self.on_drag)
tree.GetMainWindow().Bind(wx.EVT_MOUSE_CAPTURE_LOST, lambda x: None)

(The second bind avoids a mysterious "window that captured the mouse didn't process wxEVT_MOUSE_CAPTURE_LOST" on dragging)

def on_drag(self, evt):
   # No evt.Allow() here, I won't use TreeCtrl's internal DND support
   item = evt.GetItem()
   if item == self.tree.GetRootItem():
      return
   dropsrc = wx.DropSource(self)
   # Populate dropsource
   # ...
   dropsrc.DoDragDrop(wx.Drag_AllowMove)


来源:https://stackoverflow.com/questions/3803386/wx-treectrl-drag-and-drop-copy-and-move

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!