0

So i've found this HTTP downloader in python, and I wanted to modify it. I've been trying to add a ttk progressbar, to no avail, but I have no idea why it isn't working!
Here is my code:

import urllib2
import Tkinter
import ttk 


url = 'http://kharg.czystybeton.pl/pendulum%20-%20%5B2005%5D%20hold%20your%20colour/05.%20through%20the%20loop.mp3'


file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)

global file_size_dl

file_size_dl = 0
block_sz = 8192
while True:
    buffer = u.read(block_sz)
    if not buffer:
        break

    file_size_dl += len(buffer)
    f.write(buffer)

f.close()

root = Tk()
root.title('Downloading... ' + file_name)
progressbar = ttk.Progressbar(orient=HORIZONTAL, length=500, mode='determinate', variable=file_size_dl, maximum=file_size)
progressbar.pack(side="top")
progressbar.start()

root.mainloop()

Any ideas? Thanks!

2
Contributors
3
Replies
11
Views
4 Years
Discussion Span
Last Post by pyTony
0

I do not understand why you have global declaration even you have not any functions, and why are you starting progress bar after finishing transfer and closing the transfered file?

0

Thanks Tony, I'm pretty new to python, as you can probably tell.
Here is my new code:

import urllib2
from Tkinter import *
import ttk 


url = 'http://kharg.czystybeton.pl/pendulum%20-%20%5B2005%5D%20hold%20your%20colour/05.%20through%20the%20loop.mp3'


file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)

def file_size_dl():
    global file_size_dl
    file_size_dl = 0
    block_sz = 8192
    while True:
        buffer = u.read(block_sz)
        if not buffer:
            break

        file_size_dl += len(buffer)
        f.write(buffer)

root = Tk()
root.title('Downloading... ' + file_name)
progressbar = ttk.Progressbar(orient=HORIZONTAL, length=1000, mode='determinate', value=file_size_dl, variable=file_size_dl, maximum=file_size)
progressbar.pack(side="top")
progressbar.start()

root.mainloop()

f.close()

But now I get the error:
TclError: expected floating-point number but got "35273104file_size_dl"
I don't know how to resolve this... Help?

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.