I am trying to make a simple window which I can drag a file or a piece
of text to. So I wrote a new class inherited from wx.TextDropTarget
and wx.FileDropTarget and tried to use its instance as the drop target
for my window. But seems it can't work properly. See my codes below,
it only accepts either a text target or a file target.
Is there any way to make it support text targets and file targets
together so that it will response whatever I drag to it?
Thanks!
import wx
class DropTarget(wx.TextDropTarget, wx.FileDropTarget):
def __init__(self):
wx.FileDropTarget.__init__(self)
wx.TextDropTarget.__init__(self)
def OnDropFiles(self, x, y, filename):
print filename
def OnDropText(self, x, y, text):
print text
class MyTextDropTarget(wx.TextDropTarget):
def __init__(self):
wx.TextDropTarget.__init__(self)
def OnDropText(self, x, y, text):
print text
class MyFileDropTarget(wx.FileDropTarget):
def __init__(self):
wx.FileDropTarget.__init__(self)
def OnDropFiles(self, x, y, filename):
print filename
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, size = (150, 150),
style = wx.CLIP_CHILDREN | wx.FRAME_EX_METAL)
self.panel = wx.Panel(self, size = (150, 150))
#1. can print the file name when i drag a file to it.
self.panel.SetDropTarget(MyFileDropTarget())
#2. can print the text when i drag some text to it.
#self.panel.SetDropTarget(MyTextDropTarget())
#3. can't work. why?
#self.panel.SetDropTarget(DropTarget())
class App(wx.App):
def OnInit(self):
self.frame = Frame()
self.frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop()