progressDialog

9 views
Skip to first unread message

prairie99

unread,
Aug 16, 2009, 1:06:56 PM8/16/09
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()

linux23...@gmail.com

unread,
Aug 24, 2009, 12:41:21 AM8/24/09
to gdal+python+GIS+geosings论坛
一直没时间上网,不好意思

关于ProgressDialog是不能那样弄的,给你个例子:

from wx import *

application = wx.PySimpleApp()

# Create a dialog to show progress

dialog = wx.ProgressDialog ( 'Progress', 'Doing nothing in a large
amount of time.', maximum = 25 )

for x in xrange ( 25 ):

# Spend some time doing nothing

# We'll use wxSleep rather than time.sleep

wx.Sleep ( 2 )

# Update our progress and change the message

dialog.Update ( x + 1, 'On step ' + str ( x + 1 ) + '.' )

dialog不能一直创建又销毁的,要保持住,用Update来更新进度

prairie99

unread,
Aug 25, 2009, 9:35:30 PM8/25/09
to gdal+python+GIS+geosings论坛
好的。 我在CB() class __init__() 弄了个 progressDialog, 并另写一个函数Update
progressDialog。progressDialog 现在很平稳。多谢启发!
Reply all
Reply to author
Forward
0 new messages