progressDialog

Hi, everybody. I am a new wxpython user. I like the good interfaces it
builds. I recently wrote a small app that zip a whole directory of
files. That part works ok. Now I want to show the progress on a
progressDialog during the zipping process. My biggest challenge is
hooking the progress dialog and zipping process (percentage complete)
It's hard for me to completely understand the threading examples (it's
at http://wiki.wxpython.org/LongRunningTasks?action=print). On the
internet, I also found some other ways of handling this. It seems like
it's working. But the Dialog is flashing and jumping. How can I make
the program run more stable or more correct. Any multi-threading
advices are welcome. Please directly modify the program as needed.
Thanks.

#################code starts#############

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):
        fileList=os.walk(self.dir_name)

        fileList=[]
        for root, dirs, files in fileList:
                for fil in files:
                    #print fil
                    fileList.append(root+"/"+fil)
        print "preparing zipfile process: "

        try:
            a = zipfile.ZipFile(self.archive, 'w',
zipfile.ZIP_DEFLATED)

            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()

#################code ends#############

prairie99 wrote:

Hi, everybody. I am a new wxpython user. I like the good interfaces it
builds. I recently wrote a small app that zip a whole directory of
files. That part works ok. Now I want to show the progress on a
progressDialog during the zipping process. My biggest challenge is
hooking the progress dialog and zipping process (percentage complete)
It's hard for me to completely understand the threading examples (it's
at LongRunningTasks - wxPyWiki). On the
internet, I also found some other ways of handling this. It seems like
it's working. But the Dialog is flashing and jumping. How can I make
the program run more stable or more correct. Any multi-threading
advices are welcome. Please directly modify the program as needed.
Thanks.

#################code starts#############

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()

Don't do this. You are creating, showing and then immediately destroying the window, so of course it is going to flash and jump around because there is not just one "it" but many many its that each only live a fraction of a second. Instead create one progress dialog before you start and in the progress callback call its Update method.

···

--
Robin Dunn
Software Craftsman

It works great now. I initilize a progressdialog in class CB() and
update it using the progress function. Thank you for your suggestion.