prairie99
unread,Aug 16, 2009, 1:06:56 PM8/16/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to gdal+python+GIS+geosings论坛
刚学 wxpython, 并自制了一个zipDir app。 在zipDir 的makeArchive 里嵌入了一个 callback。用起来
很蹩脚。 progressDialog 在不停闪烁。 请问怎样才能让这个程序跑得更平稳或应该说更正确?多谢!
import wx
import zipfile, os
class CB():
def progress(self,total,sofar):
zippercentcomplete=int(100.0*sofar/total)
progressMax = 100
dialog = wx.ProgressDialog("A progress box", "Time remaining",
progressMax, style= wx.PD_ELAPSED_TIME | wx.PD_REMAINING_TIME)
dialog.Update(zippercentcomplete)
dialog.Destroy()
class ZipDir():
def __init__(self, dir_name, archive, callback=None):
self.dir_name=dir_name
self.archive=archive
self._callback=callback
def makeArchive(self):
fileDirList=os.walk(self.dir_name)
fileList=[]
dirList=[]
for root, dirs, files in fileDirList:
if dirs==[] and files==[]:
dirList.append(root+"/")
else:
for fil in files:
#print fil
fileList.append(root+"/"+fil)
print "preparing zipfile process: "
#print dirList
"""
'fileList' is a list of file names - full path each name
'archive' is the file name for the archive with a full path
"""
try:
a = zipfile.ZipFile(self.archive, 'w',
zipfile.ZIP_DEFLATED)
for dirs in [item[3:] for item in dirList]:
zinfo=zipfile.ZipInfo(dirs)
a.writestr(zinfo, '')
i=0
for f in fileList:
print "archiving file %s" % (f)
try:
a.write(f)
except:
print "Warning: file " + f + " is corrupt!"
if self._callback is not None:
self._callback.progress(len(fileList), i)
i=i+1
a.close()
except:
pass
print 'finished zipping files'
class ArchiveFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, None, size=(400, 400), pos=(200, 250),
title="Archive Card", style=wx.STAY_ON_TOP|wx.DEFAULT_DIALOG_STYLE)
self.SetBackgroundColour(wx.LIGHT_GREY)
okay = wx.Button(self, wx.ID_OK)
okay.SetLabel("Start Archive")
okay.SetDefault()
cancel = wx.Button(self, wx.ID_CANCEL, "cancel")
self.Bind(wx.EVT_BUTTON, self.OnOK, okay)
btns = wx.StdDialogButtonSizer()
btns.AddButton(okay)
btns.AddButton(cancel)
btns.Realize()
def OnOK(self, event):
aa=ZipDir("C:/proj", "c:/proj.zip", callback=CB())
#callback=20
aa.makeArchive()
if __name__ == "__main__":
app = wx.App(redirect=False)
frame=ArchiveFrame(None)
frame.Show()
app.MainLoop()