python保存文件到桌面_将文件从列表拖到桌面python

奇怪的是,它说AddFile方法在文档中只是Windows,但是我不知道这是否真的是真的。总之,下面是一个简单的例子,基于您给出的原始示例: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()

在Windows7、Python2.6和wxPython 2.8.12.1上测试

你可能感兴趣的:(python保存文件到桌面)