Hi, I would like to drag an image from a wx app to a target control in the same app. Ideally the following code would allow me to drag the StaticBitmap to the TextCtrl and print “On Data” when finished:
import wx
class MyDropTarget(wx.DropTarget):
def __init__(self, source):
super().__init__()
self.source = source
self.SetDataObject(self.source)
def OnData(self, x, y, d):
print('On Data')
if self.GetData():
img = self.source.GetBitmap()
return d
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(None, title='a2')
p = wx.Panel(self, size=(240,240))
sz = wx.Size(64,64)
ic = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, size=sz)
sb = wx.StaticBitmap(p, -1, bitmap=ic, size=sz)
bdo = wx.BitmapDataObject(ic)
tex = wx.TextCtrl(p, -1, "DRAG_SOURCE", style = wx.TE_MULTILINE|wx.HSCROLL)
dt1 = MyDropTarget(bdo)
tex.SetDropTarget(dt1)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(tex, 1, wx.SHAPED, 10)
sizer.AddSpacer(10)
sizer.Add(sb, 1, wx.SHAPED, 10)
p.SetSizer(sizer)
self.Show()
if __name__ == '__main__':
app = wx.App()
frm = MyFrame()
app.MainLoop()
Unfortunately. nothing happens when I drag the image. Any help will be appreciated.