How to create a drop target (Phoenix)

Keywords : Drop, Target, File, Folder.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


First example

img_sample_one.png

   1 # sample_one.py
   2 
   3 import os
   4 import wx
   5 
   6 # class FileDropEvent
   7 # class SingleFileDropTarget
   8 # class MultiFilesDropTarget
   9 # class MyFrame
  10 # class MyApp
  11 
  12 wxEVT_SINGLEFILE_DROPPED = wx.NewEventType()
  13 EVT_SINGLEFILE_DROPPED = wx.PyEventBinder(wxEVT_SINGLEFILE_DROPPED, 1)
  14 wxEVT_MULTIFILES_DROPPED = wx.NewEventType()
  15 EVT_MULTIFILES_DROPPED = wx.PyEventBinder(wxEVT_MULTIFILES_DROPPED, 1)
  16 
  17 #---------------------------------------------------------------------------
  18 
  19 class FileDropEvent(wx.PyCommandEvent):
  20     def __init__(self, evtType):
  21         wx.PyCommandEvent.__init__(self, evtType)
  22         self._items = []
  23 
  24     def SetSingleItem(self, item):
  25         self._items = [item]
  26 
  27     def GetSingleItem(self):
  28         if not self._items:
  29             return ''
  30         return self._items[0]
  31 
  32     def AddItem(self, item):
  33         self._items.append(item)
  34 
  35     def AddItems(self, items):
  36         self._items += items
  37 
  38     def GetItems(self):
  39         return self._items
  40 
  41 #---------------------------------------------------------------------------
  42 
  43 class SingleFileDropTarget(wx.FileDropTarget):
  44     def __init__(self, dstHandler):
  45         wx.FileDropTarget.__init__(self)
  46         self._target = dstHandler
  47 
  48     def OnDropFiles(self, x, y, filenames):
  49         # As we can only accept one file at a time
  50             # We take only the first element of the provided list
  51             # And tell the destination handler that a new file has been dropped
  52         evt = FileDropEvent(wxEVT_SINGLEFILE_DROPPED)
  53         evt.SetSingleItem(filenames[0])
  54         self._target.GetEventHandler().AddPendingEvent(evt)
  55         return True
  56 
  57 #---------------------------------------------------------------------------
  58 
  59 class MultiFilesDropTarget(wx.FileDropTarget):
  60     def __init__(self, dstHandler):
  61         wx.FileDropTarget.__init__(self)
  62         self._target = dstHandler
  63 
  64     def OnDropFiles(self, x, y, filenames):
  65         # We can accept all the given files
  66             # And tell the destination handler that a new file has been dropped
  67         evt = FileDropEvent(wxEVT_MULTIFILES_DROPPED)
  68         evt.AddItems(filenames)
  69         self._target.GetEventHandler().AddPendingEvent(evt)
  70         return True
  71 
  72 #---------------------------------------------------------------------------
  73 
  74 class MyFrame(wx.Frame):
  75     def __init__(self):
  76         wx.Frame.__init__(self, None, title=wx.GetApp().GetAppName())
  77         self.SetIcon(wx.Icon('icons/wxwin.ico'))
  78 
  79         self._createControls()
  80 
  81         self._connectControls()
  82 
  83     def _createControls(self):
  84         # A Statusbar in the bottom of the window
  85         self.CreateStatusBar(1)
  86         sMsg = 'wxPython ' + wx.version()
  87         self.SetStatusText(sMsg)
  88 
  89         # Add a panel to the frame (needed under Windows to have a nice background)
  90         pnl = wx.Panel(self, wx.ID_ANY)
  91 
  92         szrMain = wx.BoxSizer(wx.VERTICAL)
  93         szrMain.AddSpacer(5)
  94 
  95         stbSzr = wx.StaticBoxSizer(wx.VERTICAL, pnl, 'Single file or directory:')
  96         stBox = stbSzr.GetStaticBox()
  97         label = wx.StaticText(stBox, wx.ID_STATIC, 'Drop any file/folder from the files manager to the wxTextCtrl below')
  98         stbSzr.Add(label, 0, wx.LEFT|wx.RIGHT|wx.TOP, 5)
  99         stbSzr.AddSpacer(2)
 100         label = wx.StaticText(stBox, wx.ID_STATIC, 'to see its full path. Even if you drop more than one item, only the')
 101         stbSzr.Add(label, 0, wx.LEFT|wx.RIGHT, 5)
 102         stbSzr.AddSpacer(2)
 103         label = wx.StaticText(stBox, wx.ID_STATIC, 'path of the first one will be shown.')
 104         stbSzr.Add(label, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM, 5)
 105         self._txtSingleFile = wx.TextCtrl(stBox, -1, wx.EmptyString)
 106         stbSzr.Add(self._txtSingleFile, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 5)
 107 
 108         szrMain.Add(stbSzr, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 5)
 109 
 110         stbSzr = wx.StaticBoxSizer(wx.VERTICAL, pnl, 'Multiple files or directories:')
 111         stBox = stbSzr.GetStaticBox()
 112         label = wx.StaticText(stBox, wx.ID_STATIC, 'Drop some files/folders from the files manager to the wxListBox below')
 113         stbSzr.Add(label, 0, wx.LEFT|wx.RIGHT|wx.TOP, 5)
 114         stbSzr.AddSpacer(2)
 115         label = wx.StaticText(stBox, wx.ID_STATIC, 'Their full paths will be added to the list.')
 116         stbSzr.Add(label, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM, 5)
 117         self._lstMultiFiles = wx.ListBox(stBox, -1)
 118         stbSzr.Add(self._lstMultiFiles, 1, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 5)
 119 
 120         szrMain.Add(stbSzr, 1, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 5)
 121 
 122         pnl.SetSizer(szrMain)
 123         szrMain.SetSizeHints(self)
 124 
 125         dt = SingleFileDropTarget(self)
 126         self._txtSingleFile.SetDropTarget(dt)
 127 
 128         dt = MultiFilesDropTarget(self)
 129         self._lstMultiFiles.SetDropTarget(dt)
 130 
 131     def _connectControls(self):
 132         self.Bind(EVT_SINGLEFILE_DROPPED, self._onSingleFileDropped)
 133         self.Bind(EVT_MULTIFILES_DROPPED, self._onMultiFilesDropped)
 134 
 135     def _onSingleFileDropped(self, evt):
 136         self._txtSingleFile.ChangeValue(evt.GetSingleItem())
 137 
 138     def _onMultiFilesDropped(self, evt):
 139         self._lstMultiFiles.Append(evt.GetItems())
 140         self._lstMultiFiles.EnsureVisible(self._lstMultiFiles.GetCount()-1)
 141 
 142 #---------------------------------------------------------------------------
 143 
 144 class MyApp(wx.App):
 145     def OnInit(self):
 146         print('Running wxPython ' + wx.version())
 147         # Set Current directory to the   one containing this file
 148         os.chdir(os.path.dirname(os.path.abspath(__file__)))
 149 
 150         self.SetAppName('DropTarget')
 151 
 152         # Create the main window
 153         frm = MyFrame()
 154         self.SetTopWindow(frm)
 155 
 156         frm.Show()
 157         return True
 158 
 159 #---------------------------------------------------------------------------
 160 
 161 if __name__ == '__main__':
 162     app = MyApp()
 163     app.MainLoop()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Xaviou (sample_one.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

28/10/19 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a drop target (Phoenix) (last edited 2020-12-13 16:52:12 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.