python(練手-案頭應用) — 下載器

來源:互聯網
上載者:User

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 />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.