|
From: Tom A. <TA...@OR...> - 2008-11-13 13:06:54
|
I've written a program to where you enter a URL of a file to download.
When downloads the file and displays the progress with the Gauge widget
and displays the download speed. When the file is being downloaded I am
unable to use any buttons or type any text in the window. One the
download is complete the program responds the way it should. How do you
place the download process in the background and still have the progress
and speed still displayed? I may be in a brain lock, but I can not
figure it out.
import urllib2, sre, sys, time, urllib
from PythonCard import model
class Main(model.Background):
global timer
global speed
timer = 0
speed = ''
def on_initialize(self, event):
self.components.progressBar.value = timer
self.components.speedField.text = speed
def on_progressBar_timer(self, event, timer):
self.components.progressBar.value = timer
def on_getBtn_mouseClick(self,event):
file_url = self.components.fileURLInput.text
lastUpdate = 0.0
progressSize = 0
def reportHook(count, blocksize, totalsize):
global lastUpdate
global progressSize
global timer
global speed
if count == 0:
lastUpdate = time.time()
progressSize = 0
return
progressSize += blocksize
deltaTime = time.time() - lastUpdate
if deltaTime > .1:
percent = int(float(count * blocksize * 100) /
totalsize)
speed = int(progressSize / (1024 * deltaTime))
timer = int(percent)
speed = str(speed)
speed = speed + ' kbs'
self.components.progressBar.value = int(timer)
self.components.speedField.text = str(speed)
lastUpdate = time.time()
progressSize = 0
def getName(file_url):
# Get file name from URL
farray = file_url.split('/')
flen = len(farray)
flen = flen - 1
return farray[flen]
def dlFile(file_url, file_name):
urllib.urlretrieve(file_url,file_name,reportHook)
print 'Done'
timer = 0
speed = ''
self.components.progressBar.value = timer
self.components.speedField.text = speed
def mainDL(file_url):
file_name = getName(file_url)
dlFile(file_url,file_name)
mainDL(file_url)
def on_exitBtn_mouseClick(self, event):
sys.exit(0)
if __name__ == '__main__':
app = model.Application(Main)
app.MainLoop()
|