1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| 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() |
Partager