0

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

我的标准 python 代码会读取一个包含 zip 文件的文件夹并写入 CSV。

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

我的整个代码如下。

所以我在我的第 3 页上设置了一个仪表,当 convertFiles 运行时,我希望仪表更新为计数值。

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

未定义对象进度

我真的很感激任何帮助

谢谢

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

1 回答 1

0

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

现在已经不碍事了,您可以创建 wx.Gauge 小部件,其中文件列表的长度作为 Gauge 的“范围”参数。然后在您的 convertFiles() 方法结束时,您只需调用仪表的 SetValue() 方法。wxPython 演示有一个示例。

于 2012-10-02T13:42:24.667 回答