2

First of all, I have to tell you that I'm French. So forgive me if I made some mistakes.

I'm trying to create un simply program in which I have an input, a button and a progressBar (and some labels indicating the output in KB/s, the weight of the file the user attempts to download...)

I saw several topics on it but as I'm a newbie I don't know how to do to see my ProgressBar evolve and my program not to freeze until the file is downloaded.

I see this topic in particular but when I execute the GUI script I have this error : "Out of Stack Space..." Apparently is due to the Event().

I think I have to use Thread but I don't know how. Can someone explain me, please.

Here is what I wrote :


    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()

Have a nice day Thank

Community
  • 1
  • 1
priseJack
  • 396
  • 2
  • 14

0 Answers0