Reputation: 361
I am writing a file management and one of the UI functionality is to allow user dragging an item in the list to the desktop folder or just desktop. After googling, I found there is only dragging into the list supported in wxpython but no related articles about my issue. Is there any possible solution to this or any additional library supporting this function?
Upvotes: 0
Views: 541
Reputation: 33071
It took me a little Googling, but I found the answer: http://wxpython-users.1045709.n5.nabble.com/Creating-file-DropSource-programatically-td2344103.html
Oddly enough, it says the AddFile method is Windows only in the documentation, but I couldn't figure out if that was really true or not. Anyway, here's a simple example based on the original example you gave:
import wx
import os
import time
class MyListCtrl(wx.ListCtrl):
def __init__(self, parent, id):
wx.ListCtrl.__init__(self, parent, id, style=wx.LC_REPORT)
files = os.listdir('.')
self.InsertColumn(0, 'Name')
self.InsertColumn(1, 'Ext')
self.InsertColumn(2, 'Size', wx.LIST_FORMAT_RIGHT)
self.InsertColumn(3, 'Modified')
self.SetColumnWidth(0, 220)
self.SetColumnWidth(1, 70)
self.SetColumnWidth(2, 100)
self.SetColumnWidth(3, 420)
j = 0
for i in files:
(name, ext) = os.path.splitext(i)
ex = ext[1:]
size = os.path.getsize(i)
sec = os.path.getmtime(i)
self.InsertStringItem(j, "%s%s" % (name, ext))
self.SetStringItem(j, 1, ex)
self.SetStringItem(j, 2, str(size) + ' B')
self.SetStringItem(j, 3, time.strftime('%Y-%m-%d %H:%M',
time.localtime(sec)))
if os.path.isdir(i):
self.SetItemImage(j, 1)
elif ex == 'py':
self.SetItemImage(j, 2)
elif ex == 'jpg':
self.SetItemImage(j, 3)
elif ex == 'pdf':
self.SetItemImage(j, 4)
else:
self.SetItemImage(j, 0)
if (j % 2) == 0:
self.SetItemBackgroundColour(j, '#e6f1f5')
j = j + 1
class FileHunter(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, -1, title)
panel = wx.Panel(self)
p1 = MyListCtrl(panel, -1)
p1.Bind(wx.EVT_LIST_BEGIN_DRAG, self.onDrag)
sizer = wx.BoxSizer()
sizer.Add(p1, 1, wx.EXPAND)
panel.SetSizer(sizer)
self.Center()
self.Show(True)
#----------------------------------------------------------------------
def onDrag(self, event):
""""""
data = wx.FileDataObject()
obj = event.GetEventObject()
id = event.GetIndex()
filename = obj.GetItem(id).GetText()
dirname = os.path.dirname(os.path.abspath(os.listdir(".")[0]))
fullpath = str(os.path.join(dirname, filename))
data.AddFile(fullpath)
dropSource = wx.DropSource(obj)
dropSource.SetData(data)
result = dropSource.DoDragDrop()
print fullpath
app = wx.App(0)
FileHunter(None, -1, 'File Hunter')
app.MainLoop()
Tested on Windows 7, Python 2.6 and wxPython 2.8.12.1
Upvotes: 2