wxpython/wxwizard gauge issue
سؤال
I am trying to add a Gauge to one of my pages in a wxpython app I am writing.
My standard python code reads through a folder of zip files and writes out to a CSV.
I have two questions, how can I make the range of my gauge match the number of files found and secondly how to set the value of the gauge to the count of files processed.
My entire code is below.
So I have setup a gauge on my 3rd page and as the convertFiles is operating I want the guage to update to the count value.
I have tried lots of the tutorials and getting lots of
object progress not defined
I really appreciate any help
Thanks
import wx
import wx.wizard as wiz
import sys
import glob
import os
import csv
import zipfile
import StringIO
from datetime import datetime
fileList = []
fileCount = 0
chosepath = ''
filecounter = 0
totalfiles = 0
count=0
saveout = sys.stdout
#############################
class TitledPage(wiz.WizardPageSimple):
#----------------------------------------
def __init__(self, parent, title):
wiz.WizardPageSimple.__init__(self, parent)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.sizer)
title = wx.StaticText(self, -1, title)
title.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.BOLD))
self.sizer.Add(title, 0, wx.ALIGN_LEFT|wx.ALL, 5)
self.sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
def main():
wizard = wx.wizard.Wizard(None, -1, "Translator")
page1 = TitledPage(wizard, "Welcome to the Translator")
introText = wx.StaticText(page1, -1, label="This application will help translate from the \nmany ZIP files into seperate CSV files of the individul feature \nrecord types.\n\n\nPlease follow the instrcutions on each page carefully.\n\n\nClick NEXT to Begin")
introText.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
page1.sizer.Add(introText)
page2 = TitledPage(wizard, "Find Data")
browseText = wx.StaticText(page2, -1, label="Click the Browse Button below to find the folder of ZIP files")
browseText.SetFont(wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
page2.sizer.Add(browseText)
zip = wx.Bitmap("zip.png", wx.BITMAP_TYPE_PNG)#.ConvertToBitmap()
browseButton = wx.BitmapButton(page2, -1, zip, pos=(50,100))
wizard.Bind(wx.EVT_BUTTON, buttonClick, browseButton)
filelist = wx.TextCtrl(page2, size=(250,138), pos=(225, 100), style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
redir=RedirectText(filelist)
sys.stdout=redir
page3 = TitledPage(wizard, "Convert ZIP Files to CSV files")
listfilesText = wx.StaticText(page3, -1, label="Click the following button to process the ZIP files")
listfilesText.SetFont(wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
page3.sizer.Add(listfilesText)
convert = wx.Image("convert.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap()
convertButton = wx.BitmapButton(page3, -1, convert, pos=(50,100))
wizard.Bind(wx.EVT_BUTTON, convertFiles, convertButton)
wizard.progress = wx.Gauge(page3, range=totalfiles, size=(250,25), pos=(225,150))
wizard.progress.SetValue(0)
wizard.FitToPage(page1)
wx.wizard.WizardPageSimple.Chain(page1, page2)
wx.wizard.WizardPageSimple.Chain(page2, page3)
wizard.RunWizard(page1)
wizard.Destroy()
def convertFiles(wizard, count=0):
for name in fileList:
base = os.path.basename(name)
filename = os.path.splitext(base)[0]
dataFile = filename
dataDirectory = os.path.abspath(chosepath)
archive = '.'.join([filename, 'zip'])
fullpath = ''.join([dataDirectory,"\\",archive])
csv_file = '.'.join([dataFile, 'csv'])
filehandle = open(fullpath, 'rb')
zfile = zipfile.ZipFile(filehandle)
data = StringIO.StringIO(zfile.read(csv_file))
reader = csv.reader(data)
for row in reader:
type = row[0]
if "10" in type:
write10.writerow(row)
elif "11" in type:
write11.writerow(row)
data.close()
count = count +1
print ("Processing file number %s out of %s")%(count,totalfiles)
wizard.progress.SetValue(count)#this is where I thought the SetValue would work
print ("Processing files completed, time taken: ")
print(datetime.now()-startTime)
print "Please now close the appplication"
def folderdialog():
dialog = wx.DirDialog(None, "Choose a directory", style=1 )
if dialog.ShowModal() == wx.ID_OK:
chosepath = dialog.GetPath()
dialog.Destroy()
return chosepath
def buttonClick(self):
global chosepath
chosepath = folderdialog()
print ("The folder picked was")
print chosepath
for dirname, dirnames, filenames in os.walk(chosepath):
for filename in filenames:
if filename.endswith((".zip")):
fileList.append(filename)
print (filename)
global totalfiles
totalfiles = len(fileList)
print "Total Number of files found :", totalfiles
class RedirectText:
def __init__(self,textDisplay):
self.out=textDisplay
def write(self,string):
self.out.WriteText(string)
if __name__ == "__main__":
app = wx.App(False)
main()
app.MainLoop()
المحلول
If you want a count of the files found in a directory, I like using Python's glob module. It returns a list of results that you can just grab a length of. If you want the number of files inside a zip file, I think you can use Python's zipfile module to pull it out via the namelist() method.
Now that that is out of the way, you can create the wx.Gauge widget with the length of the file list as the "range" parameter for the Gauge. Then at the end of your convertFiles() method, you just call the gauge's SetValue() method. The wxPython demo has an example.