DragNDrop for Python using wxFormBuilder

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

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()

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:Windowsbfsvc.exe
    dnd directory:  C:WindowsWaaS
    dnd directory:  C:WindowsWeb
    dnd directory:  C:WindowsWinSxS
    dnd file: C:Windowsampa.exe
    dnd file: C:WindowsATTODiskBenchmark.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.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật