使用python和ttk下载ProgressBar

时间:2013-08-14 16:24:48

标签: python download progress-bar

首先,我必须告诉你我是法国人。 如果我犯了一些错误,请原谅我。

我正在尝试创建一个简单的程序,其中我有一个输入,一个按钮 和progressBar(和一些标签指示输出以KB / s为单位,重量为 文件用户试图下载...)

我看到了几个主题,但由于我是新手,我不知道怎么做才能看到我的 ProgressBar演变,我的程序在文件下载之前不会冻结。

我特别看到topic,但是当我执行GUI脚本时,我就有了这个 错误:“超出堆栈空间... ”显然是由于事件()。

我想我必须使用Thread但我不知道怎么做。有人可以解释一下。

这是我写的:


    from tkinter import *
    import urllib.request
    import time
    import sys
    import ttk
    from threading import Event, Thread

    class Telecharger(object):
        """Evalue l'expression (url ou code) et télécharge le fichier du site concerné"""
        def __init__(self):
            sd = str(entree1.get() )
            urllib.request.urlretrieve(sd, "mymovie.mp4", Telecharger.reporthook)


        def reporthook(count, block_size, total_size):
            global start_time
            if count == 0:
                start_time = time.time()
                time.sleep(1)
                return
            def guiloop():
                # Information appear in console
                duration = time.time() - start_time
                progress_size = int(count * block_size)
                speed = int(progress_size / (1024 * duration))
                percent = int(count * block_size * 100 / total_size)
                sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" %
                                (percent, total_size / (1024 * 1024), speed, duration))
                sys.stdout.flush()

                # ProgressBar don't evolve :(
                barreProgression["value"] = percent
                chaine.configure(text = str(speed) + "KB/s")

            Thread(target=guiloop).start()

    fen = Tk()
    fen.withdraw()
    fen.title("Test")

    cadre1 = Frame(fen, width = 400, height = 80).pack(side = TOP)

    Label(cadre1, text='Downloader').place(y = 25, width = 400)
    Label(cadre1, text='URL : ').place(y = 50, width = 120)

    entree1 = Entry(cadre1, bd='5')
    entree1.bind("", Telecharger)
    entree1.place(x = 120, y = 50, width = 200)

    Button(cadre1, text='Go', command = Telecharger).place(x = 330, y = 50, width = 50)

    cadrProgress = Frame(fen, width = 400, height = 60).pack()
    barreProgression = ttk.Progressbar(fen, length=260, orient="horizontal", mode="determinate")
    barreProgression.place(y = 100, x = 10, width = 200)
    chaine = Label(fen, text="poids : 0 MB, vitesse = 0 KB/s")
    chaine.place(y = 100, x = 220, width = 170)
    fen.after(500, fen.deiconify)

    fen.mainloop()

祝你有个愉快的一天 感谢

0 个答案:

没有答案