python(练手-桌面应用) -- 下载器

demo: http://www.sandy1219.com/python/download.rar

 

downloadfile.py

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

 

download.py

<textarea cols="50" rows="15" name="code" class="python:collapse:showcolumns"># coding=UTF-8 import urllib; import wx; def download(url,filename=&quot;&quot; , fun=&quot;&quot; ,completeF=&quot;&quot;): def myreporthook(block_count,block_size,file_size): if file_size == -1 : print &quot;can't download file&quot;; else: percentage = int( (block_count*block_size*100)/file_size ); fun(percentage); if percentage &gt; 100 : print &quot;100%&quot;; else: print &quot;%d%%&quot; % (percentage); filehandler,m = urllib.urlretrieve(url,filename,reporthook = myreporthook ); print &quot;done&quot;; completeF(); return filehandler; #if __name__ == &quot;__main__&quot;: # http = download(&quot;http://archive.ibobar.com/files/huana/audio/elva/elva05.mp3&quot; , &quot;e:/1.mp3&quot;); </textarea>

你可能感兴趣的:(python(练手-桌面应用) -- 下载器)