Wxpython implementing file Drag and drop

Source: Internet
Author: User

I want to let wx.grid inside the cell can support file drag and drop, the implementation is very simple, a total of 3 steps:

1. Create a WX. Filedroptarget the object of the subclass and passes the control that is to support the drag to its constructor, which is the grid

2. Call the Setdroptarget function of the grid and set the 1th step to create the WX. Filedroptarget the subclass object to it

3, realize the 1th step to create the WX. Filedroptarget subclasses, and overrides the Ondropfiles function

The post code is as follows:

ImportWXImportWx.gridImportLoggingImportOS#The 3rd step is to realize WX. Filedroptarget sub-classclassFileDrop (WX. Filedroptarget):def __init__(self, grid): WX. Filedroptarget.__init__(self) Self.grid=GriddefOndropfiles (self, x, Y, FilePath):#This method is called when a file is dragged into the gridCellcoords = Self.grid.XYToCell (x, y)#line and column numbers dragged into grid grid based on axis conversionfilename =Os.path.basename (filepath[0]) Self.grid.SetCellValue (Cellcoords.getrow (), Cellcoords.getcol (), filename) #assigns a file name to the cell being dragged into        classMyFrame (WX. Frame):def __init__(self): WX. Frame.__init__(Self, None,-1,'MyFrame', size = (640, 480)) Panel= WX. Panel (self,-1) Vsizer=WX. Boxsizer (WX. VERTICAL) Self.grid= Wx.grid.Grid (Panel, 1) Self.grid.CreateGrid (10, 3) Sizer=WX. Boxsizer (WX. Horizontal) Sizer. ADD (Self.grid,1, WX. All | Wx. EXPAND, 5) Vsizer.add (Sizer,1, WX. All |WX. EXPAND) panel. Setsizer (vsizer) Self.filedrop= FileDrop (Self.grid)#1th Step, create the FileDrop object and pass the grid to the initialization functionSelf.grid.SetDropTarget (Self.filedrop)#2nd step, call the Setdroptarget function of the grid and pass the FileDrop object to itclassMainapp (WX. APP):def __init__(self, redirect = False, filename =None): Wx.app.__init__(self, redirect, filename)defOnInit (self): Self.frame=MyFrame () self.frame.Show () Self.frame.Center ( )returnTrueapp=Mainapp () app. Mainloop ()

Wxpython implementing file Drag and drop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.