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