I am using wx.TextDropTarget to implement a drag and drop app where users can drag some text strings from a ListCtrl
widget and drop them into TextCtrl
widgets. One problem that I haven’t been able to figure out how to solve is that sometimes text strings dropped in TextCtrl
widgets have random characters appended to the end (see screenshot below). This problem does not arise every single time – as you can see, “East of Eden” is displayed fine
I am on OSX 10.10.5 and using wxPython 2.9.2.4-1. I am not sure if this problem can be reproduced on other platforms. A sample code is below.
import wx
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin
class AutoWidthListCtrl(wx.ListCtrl, ListCtrlAutoWidthMixin):
def init(self, parent):
wx.ListCtrl.init(self, parent, -1, style=wx.LC_REPORT)
ListCtrlAutoWidthMixin.init(self)
class TextDropTarget(wx.TextDropTarget):
def init(self, obj):
wx.TextDropTarget.init(self)
self.obj = obj
def OnDropText(self, x, y, text):
self.obj.WriteText(text)
def OnDragOver(self, x, y, d):
return wx.DragCopy
class MyApp(wx.Frame):
def init(self, parent, title):
super(MyApp, self).init(parent, style = wx.DEFAULT_FRAME_STYLE,
title=title, size=(500, 500))
self.Panel = wx.Panel(self, size = (-1, -1))
GridBagSizer = wx.GridBagSizer(5,5)
#ListCtrl widget
self.Source = AutoWidthListCtrl(self.Panel)
self.Source.Show()
self.Source.InsertColumn(0, 'Book Titles')
self.Source.InsertStringItem(0,'War and Peace')
self.Source.InsertStringItem(0,'East of Eden')
self.Source.InsertStringItem(0,'Pride and Prejudice')
GridBagSizer.Add(self.Source, pos = (0, 0), span = (6, 1),
flag = wx.EXPAND|wx.ALL, border = 15)
self.Panel.SetSizer(GridBagSizer)
#Label for each TextCtrl widget
AmericanNovel = wx.StaticText(self.Panel, label ="American Novel:")
EnglishNovel = wx.StaticText(self.Panel, label = "British Novel:")
RussianNovel = wx.StaticText(self.Panel, label = "Russian Novel:")
GridBagSizer.Add(AmericanNovel, pos = (0, 1), span = (1, 1),
flag = wx.TOP, border = 10)
GridBagSizer.Add(EnglishNovel, pos = (2, 1), span = (1, 1))
GridBagSizer.Add(RussianNovel, pos = (4, 1), span = (1, 1))
#TextCtrl widgets
self.Target1 = wx.TextCtrl(self.Panel, size = (240, -1),
style = wx.TE_READONLY)
self.Target2 = wx.TextCtrl(self.Panel, size = (240, -1),
style = wx.TE_READONLY)
self.Target3 = wx.TextCtrl(self.Panel, size = (240, -1),
style = wx.TE_READONLY)
GridBagSizer.Add(self.Target1, pos = (1, 1), span = (1, 1))
GridBagSizer.Add(self.Target2, pos = (3, 1), span = (1, 1))
GridBagSizer.Add(self.Target3, pos = (5, 1), span = (1, 1))
dt1 = TextDropTarget(self.Target1)
dt2 = TextDropTarget(self.Target2)
dt3 = TextDropTarget(self.Target3)
self.Target1.SetDropTarget(dt1)
self.Target2.SetDropTarget(dt2)
self.Target3.SetDropTarget(dt3)
self.Bind(wx.EVT_LIST_BEGIN_DRAG, self.OnDragInit)
GridBagSizer.AddGrowableCol(0)
GridBagSizer.AddGrowableCol(1)
def OnDragInit(self, evt):
text = self.Source.GetItemText(evt.GetIndex())
tdo = wx.TextDataObject(text)
tds = wx.DropSource(self.Source)
tds.SetData(tdo)
tds.DoDragDrop(True)
if name == “main”:
app = wx.App()
MainFrame = MyApp(None, title = “My App”)
MainFrame.Show()
MainFrame.Centre()
app.MainLoop()
``
Any pointer would be appreciated!