开发者

File Downloader with GUI progress display?

I am trying to write a file downloader that has a GUI and displays the progress of the file being downloaded. I would like it to either display a text percentage, a progress bar or both. I am sure this can be done开发者_如何学编程 in Python, but I'm just not sure how.

I am using Python 2.6 on MS Windows XP.


The easiest progress bar dialog would probably be with EasyDialogs for Windows (follows the same api as the EasyDialogs module that is included with the mac version of python)

For determining the progress of the download, use urllib.urlretrieve() with a "reporthook".

Something like this:

import sys
from EasyDialogs import ProgressBar
from urllib import urlretrieve

def download(url, filename):
    bar = ProgressBar(title='Downloading...', label=url)

    def report(block_count, block_size, total_size):
        if block_count == 0:
            bar.set(0, total_size)
        bar.inc(block_size)

    urlretrieve(url, filename, reporthook=report)

if __name__ == '__main__':
    url = sys.argv[1]
    filename = sys.argv[2]
    download(url, filename)

There are of course other libraries available for richer GUI interfaces (but they are larger or more difficult if this is all you need). The same goes for the downloads: there are probably faster things than urllib, but this one is easy and included in the stdlib.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜