wxpython/wxwizard仪表问题

2024-09-26 17:47:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我正在编写的wxpython应用程序的一个页面中添加一个量规。在

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

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

我的全部代码如下。在

所以我在我的第三页上设置了一个量规,当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()

Tags: theimportselfdeffileszipfilenamewizard
1条回答
网友
1楼 · 发布于 2024-09-26 17:47:26

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

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

相关问题 更多 >

    热门问题