demo: http://www.sandy1219.com/python/download.rar
downloadfile.py
# coding=UTF-8<br />import wx;<br />from com.download import download;<br />import os;<br />class Downloadfile(wx.Frame):<br /> def __init__(self):<br /> #save file url;<br /> self.saveURL = ""<br /> wx.Frame.__init__(self,None,-1,"download",size=(400,150));<br /> panel = wx.Panel(self,-1);<br /> self.topLabel = wx.StaticText(panel,-1,"1,enter url 2, click save 3, start down")<br /> self.topLabel.SetFont(wx.Font(18,wx.SWISS,wx.NORMAL,wx.BOLD))<br /> self.label1 = wx.StaticText(panel,-1," url: ");<br /> self.label1.SetFont(wx.Font(14,wx.SWISS,wx.NORMAL,wx.BOLD))<br /> self.input1 = wx.TextCtrl(panel,-1);<br /> #self.input1.SetInsertionPoint(0);<br /> self.input1.Value = "http://archive.ibobar.com/files/huana/audio/elva/elva05.mp3"<br /> self.saveBtn = wx.Button(panel,-1,"save");<br /> self.Bind(wx.EVT_BUTTON,self.save,self.saveBtn)<br /> self.button = wx.Button(panel,-1,"down");<br /> self.Bind(wx.EVT_BUTTON,self.startDown,self.button);<br /> self.gauge = wx.Gauge(panel,-1,100,(20,90),(350,25));<br /> self.gauge.SetBezelFace(3);<br /> self.gauge.SetShadowWidth(3);<br /> self.gaugeLB = wx.StaticText(panel,-1,"0%");<br /> self.gaugeLB.SetFont(wx.Font(14,wx.SWISS,wx.NORMAL,wx.BOLD))<br /> self.multText = wx.TextCtrl(panel,-1,"",size=(400,100),style=wx.TE_MULTILINE)<br /> #Add(window, proportion=0, flag=0, border=0, userData=None)<br /> mainSize = wx.BoxSizer(wx.VERTICAL);<br /> mainSize.Add(wx.StaticLine(panel),0,wx.EXPAND);<br /> mainSize.Add(self.topLabel);<br /> mainSize.Add(wx.StaticLine(panel),0,wx.EXPAND);<br /> urlSizer = wx.FlexGridSizer(cols=2,hgap=5,vgap=5);<br /> urlSizer.AddGrowableCol(1);<br /> urlSizer.Add(self.label1,0,wx.Left);<br /> urlSizer.Add(self.input1,0,wx.EXPAND);<br /> btnSizer = wx.BoxSizer(wx.HORIZONTAL)<br /> btnSizer.Add((20,20), 1)<br /> btnSizer.Add(self.saveBtn)<br /> btnSizer.Add((20,20), 1)<br /> btnSizer.Add(self.button)<br /> btnSizer.Add((20,20), 1)<br /> progressSizer = wx.BoxSizer(wx.HORIZONTAL);<br /> progressSizer.Add(self.gauge);<br /> progressSizer.Add((20,20), 0)<br /> progressSizer.Add(self.gaugeLB);<br /> textSizer = wx.FlexGridSizer(cols=2,hgap=5,vgap=5);<br /> textSizer.AddGrowableCol(0);<br /> textSizer.Add(self.multText,0,wx.EXPAND);<br /> mainSize.Add(urlSizer,0,wx.EXPAND|wx.ALL,10);<br /> mainSize.Add(btnSizer,0,wx.EXPAND|wx.ALL,10);<br /> mainSize.Add(progressSizer,0,wx.EXPAND|wx.ALL,10);<br /> mainSize.Add(textSizer,0,wx.EXPAND|wx.ALL,10);<br /> panel.SetSizer(mainSize);<br /> mainSize.Fit(self);<br /> mainSize.SetSizeHints(self);<br /> self.addLog("---------please select save file-------");<br /> def addLog(self,t):<br /> self.multText.AppendText(t+'/n');<br /> def startDown(self,event):<br /> self.addLog("---start download file");<br /> down = download(self.input1.Value,self.saveURL,self.changeProgress,self.downComplete)<br /> def getNameFromURL(self,url = ""):<br /> ls = str(url).split("/");<br /> return ls[len(ls)-1];<br /> def save(self,event):<br /> self.addLog("---start select save file");<br /> woldcard= "All files(*.*)|*.*";<br /> dialog = wx.FileDialog( None , "save file" , os.getcwd() , self.getNameFromURL(self.input1.Value) , woldcard , wx.SAVE);<br /> if dialog.ShowModal() == wx.ID_OK:<br /> self.saveURL = dialog.GetPath();<br /> self.addLog("---selected file: " + self.saveURL )<br /> dialog.Destroy();<br /> def changeProgress(self,n=0):<br /> self.gauge.SetValue(n);<br /> self.gaugeLB.SetLabel( str(n)+'%')<br /> def downComplete(self,event):<br /> self.addLog("---------down complete ! ---------");<br /> dlg = wx.MessageDialog(None,"download complete! " , "complete" , style = wx.OK)<br /> code = dlg.ShowModal();<br /> dlg.Destroy();<br />if __name__ == "__main__":<br /> app = wx.PySimpleApp();<br /> frame = Downloadfile();<br /> frame.Show();<br /> app.MainLoop();<br />
download.py
# coding=UTF-8<br />import urllib;<br />import wx;<br />def download(url,filename="" , fun="" ,completeF=""):<br /> def myreporthook(block_count,block_size,file_size):<br /> if file_size == -1 :<br /> print "can't download file";<br /> else:<br /> percentage = int( (block_count*block_size*100)/file_size );<br /> fun(percentage);<br /> if percentage > 100 :<br /> print "100%";<br /> else:<br /> print "%d%%" % (percentage);<br /> filehandler,m = urllib.urlretrieve(url,filename,reporthook = myreporthook );<br /> print "done";<br /> completeF();<br /> return filehandler;<br />#if __name__ == "__main__":<br /> # http = download("http://archive.ibobar.com/files/huana/audio/elva/elva05.mp3" , "e:/1.mp3");<br />