wxpython / wxwizard规范问题

时间:2012-09-27 13:29:49

标签: python wxpython gauge

我正在尝试在我正在编写的wxpython应用程序中为我的一个页面添加一个Gauge。

我的标准python代码读取zip文件的文件夹并写入CSV。

我有两个问题,如何使我的量程范围与找到的文件数相匹配,其次如何将量表的值设置为处理的文件数。

我的整个代码都在下面。

所以我在第3页设置了一个标尺,当convertFiles正在运行时,我希望guage更新为计数值。

我已经尝试了很多教程并获得了大量的

未定义对象进度

我非常感谢任何帮助

由于

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

1 个答案:

答案 0 :(得分:0)

如果你想要计算目录中找到的文件,我喜欢使用Python的glob模块。它会返回一个结果列表,您可以抓住它的长度。如果你想要一个zip文件中的文件数量,我想你可以使用Python的zipfile模块通过namelist()方法将它拉出来。

现在已经不在了,您可以使用文件列表的长度创建wx.Gauge小部件作为Gauge的“range”参数。然后在convertFiles()方法的末尾,您只需调用gauge的SetValue()方法。 wxPython演示有一个例子。