用于保存文件的wxpython进度条

时间:2017-10-02 05:16:55

标签: python wxpython

我一直想弄清楚如何在保存文件对话框中显示进度对话框。

现在它显示了,但是在文件对话框被破坏之前,条形图不会移动。

dlg = wx.FileDialog(
        self, message="Save file as ...",
        defaultFile="", wildcard="Excel Files (*.xlsx)|*.xlsx", style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT
        )
    if dlg.ShowModal() == wx.ID_OK:
        path = dlg.GetPath()

        try:
            self.progress = wx.GenericProgressDialog("Saving file ...", "", 100, self, wx.PD_AUTO_HIDE|wx.PD_APP_MODAL|wx.PD_SMOOTH)
            self.progress.Pulse()
            #do long running task and save file
            self.timer.Stop()
            self.progress.Destroy()
            wx.MessageBox('Saved at ' + path)
        except:
            print "Error saving:", sys.exc_info()[0]
            raise
    dlg.Destroy()

def OnTimer(self, event):
        if self.progress != None and self.progress_cnt < 100:
            self.progress_cnt += 5
            self.progress.Update(self.progress_cnt)

1 个答案:

答案 0 :(得分:0)

在每次迭代中使用wx.Yield()或(n)“长时间运行的任务”的多次迭代。

import wx
import sys
import time
class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Statusbar',size=(300,200))
        panel = wx.Panel(self)
        self.Show()
        dlg = wx.FileDialog(
            self, message="Save file as ...",
            defaultFile="", wildcard="Excel Files (*.xlsx)|*.xlsx", style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            try:
                self.progress = wx.GenericProgressDialog("Saving file ...", "", 100, self, wx.PD_AUTO_HIDE|wx.PD_APP_MODAL|wx.PD_SMOOTH)
                x=0
                while x < 20:       #do long running task and save file
                    x+=1            #do long running task and save file
                    time.sleep(0.5) #do long running task and save file
                    self.progress.Pulse("Hamster running like fury")
                    wx.Yield()      #use Yield to allow update of progress bar
                self.progress.Destroy()
                wx.MessageBox('Saved at ' + path)
            except:
                print "Error saving:", sys.exc_info()[0]
                raise
        dlg.Destroy()

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame()
    app.MainLoop()

我的漫长过程是while x < 20