我正在尝试创建一个小型应用程序wxpython
,用户可以在其中从listctrl
对象(源)中拖动一些文本并将其放到另一个listctrl
对象(目标)中。
我想以这样一种方式编写这个应用程序,即只有当光标位于目标区域时,才会将文本字符串放入目标对象中。listctrl
listctrl
即使光标从未移动到目标区域,我的代码(如下所示)现在也会删除一个文本字符串。任何指针将不胜感激!
import wx
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin
class TextDropTargetListCtrl(wx.TextDropTarget):
def __init__(self, object):
wx.TextDropTarget.__init__(self)
self.object = object
def OnDropText(self, x, y, data):
self.object.InsertStringItem(0, data)
def OnDragOver(self, x, y, d):
return wx.DragCopy
class AutoWidthListCtrl(wx.ListCtrl, ListCtrlAutoWidthMixin):
def __init__(self, parent, style):
wx.ListCtrl.__init__(self, parent, -1, style=style)
ListCtrlAutoWidthMixin.__init__(self)
class MainApp(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="", size=(500, 800))
self.SetBackgroundColour('white')
self.GridBagSizer = wx.GridBagSizer()
self.listctrl_left = AutoWidthListCtrl(self, style = wx.LC_REPORT|wx.LC_VRULES)
self.listctrl_left.InsertColumn(0, "Source")
self.listctrl_left.InsertStringItem(0, "apple")
self.listctrl_left.InsertStringItem(1, "pear")
self.listctrl_left.InsertStringItem(2, "watermelon")
self.listctrl_right = AutoWidthListCtrl(self, style = wx.LC_REPORT)
self.listctrl_right.InsertColumn(0, "Destination")
self.GridBagSizer.Add(self.listctrl_left, pos=(0, 0),span = (1, 1),
flag = wx.EXPAND|wx.ALL, border = 15)
self.GridBagSizer.Add(self.listctrl_right, pos=(0, 1),span = (1, 1),
flag = wx.EXPAND|wx.ALL, border = 15)
self.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDragInit)
self.DropTarget = TextDropTargetListCtrl(self.listctrl_right)
self.GridBagSizer.AddGrowableCol(0)
self.GridBagSizer.AddGrowableCol(1)
self.GridBagSizer.AddGrowableRow(0)
self.SetSizer(self.GridBagSizer)
def OnDragInit(self, evt):
text = self.listctrl_left.GetItemText(evt.GetIndex())
tdo = wx.TextDataObject(text)
tds = wx.DropSource(self.listctrl_left)
tds.SetData(tdo)
tds.DoDragDrop(True)
if __name__ == "__main__":
app = wx.App()
MainFrame = MainApp()
MainFrame.Show()
MainFrame.Centre()
app.MainLoop()