Reputation: 89
My existing wxPython app is based on a wxFormBuilder skeleton. It will accept new image files to work with via a file selector dialog. Now, I would like to make drag-n-drop available, but am stuck on how to go about it. After some review of the properties options for the image panel, I see the 'drop_accept_files' option. When I check the box and generate the Python code, I do see new code generated for this
self.m_panelImage = wx.Panel( self.m_splitterMain, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
self.m_panelImage.DragAcceptFiles( True ) **<<<< new line of added code**
All of this sounds very promising and interesting, but I have not been able to figure out how I would hook up to this facility and use it. Anyone can point me in the right direction or know of a good example to review? TIA
Upvotes: 0
Views: 70
Reputation: 1410
After checking the drag_accept_files property for the panel you need to define the event handler OnDropFiles.
======================================
Now you can generate the code with wxFormBuilder.
In the python source for your form you have to program the logic for the defined event handler. In my example on_panel_dnd_drop_files. It just prints the files and directories received.
def on_panel_dnd_drop_files( self, event ):
for path in event.Files:
if os.path.isfile(path):
print(f'dnd file: {path}')
elif os.path.isdir(path):
print(f'dnd directory: {path}')
You can drag and drop files as well as directories as well as a combination. Here is the result of drag and dropping some files and directories from my windows directory:
dnd file: C:\Windows\bfsvc.exe
dnd directory: C:\Windows\WaaS
dnd directory: C:\Windows\Web
dnd directory: C:\Windows\WinSxS
dnd file: C:\Windows\ampa.exe
dnd file: C:\Windows\ATTODiskBenchmark.INI
As you can see in the example event handler, you can use os.path.isfile and os.path.isdir to distinguish between files and directories.
Upvotes: 0
Reputation: 22448
As soon as you say wxFormBuilder, my eyes glaze over, as I've always coded directly, so this may or may not be of help.
You need to Bind to an EVT_DROP_EVENT
to know that something has been dropped and have set a target
to process what got dropped.
Here is an example, sadly no wxFrameBuilder involved:
import wx
import wx.lib.newevent
drop_event, EVT_DROP_EVENT = wx.lib.newevent.NewEvent()
class FileDropTarget(wx.FileDropTarget):
def __init__(self, obj, multiple_allowed=False):
wx.FileDropTarget.__init__(self)
self.obj = obj
self.multiple_allowed = multiple_allowed
def OnDropFiles(self, x, y, filenames):
filename = filenames[0]
evt = drop_event(data=filename)
wx.PostEvent(self.obj, evt)
return True
class MyFrame(wx.Frame):
def __init__(self, parent, title):
super(MyFrame, self).__init__(parent, style = wx.DEFAULT_FRAME_STYLE,
title=title, size=(500, 500))
self.panel = wx.Panel(self, size = (-1, -1))
self.bmp = wx.Bitmap()
self.bitmap = wx.StaticBitmap(self.panel, -1, self.bmp)
self.file_drop_target = FileDropTarget(self, multiple_allowed=False)
self.SetDropTarget(self.file_drop_target)
self.Bind(EVT_DROP_EVENT, self.OnDropEvent)
def OnDropEvent(self, event):
drop_filename = event.data
self.bitmap.SetBitmap(wx.Bitmap(drop_filename, wx.BITMAP_TYPE_ANY))
self.Refresh()
return
if __name__ == "__main__":
app = wx.App()
MainFrame = MyFrame(None, title = "Drop Image")
MainFrame.Show()
MainFrame.Centre()
app.MainLoop()
Upvotes: 0