Reputation: 166
I am basically a newbie in python and I am building this downloader program in python using tkinter and urllib.request and it is quite done but as a final touch I wanted to add a progress bar to show how much the file is downloaded. I found out that I can add a progress bar using TQDM but first I should have gotten the size of the file that the user wants to downloaded to show how much the program has downloaded it and I didn't find anything! Can anyone help me out? My code(BTW if you have any idea how to make my code better I would appreciate it):
from tkinter import *
from tkinter import font as tkFont
import random
import urllib.request
def printsth():
print("Yay it works! ")
def main_menu():
root = Tk()
# the top menu
num = IntVar()
var = IntVar()
menu = Menu(root)
root.config(menu=menu)
submenu = Menu(menu)
menu.add_cascade(label="File", menu=submenu)
submenu.add_command(label="New project...", command=printsth)
submenu.add_command(label="New ", command=printsth)
submenu.add_separator()
submenu.add_command(label="Exit", command=root.destroy)
# the edit menu
editmenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editmenu)
editmenu.add_command(label="Redo...", command=printsth)
# the tool bar
toolbar = Frame(root, bg="light gray")
insert_button = Button(toolbar, text="Insert an image", command=printsth)
insert_button.pack(side=LEFT, padx=2, pady=2)
print_button = Button(toolbar, text="Print", command=printsth)
print_button.pack(side=LEFT, padx=2, pady=2)
toolbar.pack(side=TOP, fill=X)
# the status bar
status_bar = Label(root, text="This is only an example...", bd=1, relief=SUNKEN, anchor=W)
status_bar.pack(side=BOTTOM, fill=X)
# the download frame
def download_image():
global formatname
if num.get() == 1:
name = random.randrange(1, 10000)
else:
name = str(name_entry.get())
formatname = str(format_entry.get())
'''if var.get() == 1:
operator = str(url_entry.get())
formatname = '.' + operator[-3] + operator[-2] + operator[-1]
else:
pass'''
fullname = str(name) + formatname
url = str(url_entry.get())
urllib.request.urlretrieve(url, fullname)
body_frame = Frame(root, bg="light blue")
download_button = Button(body_frame, text="Download! ", command=download_image, border=3, width=20, height=5)
download_design = tkFont.Font(size=12, slant='italic')
download_button['font'] = download_design
download_button.pack(side=LEFT, pady=5, padx=5)
body_frame.pack(side=LEFT, fill=Y)
# the main interaction menu
inter_frame = Frame(root)
url_entry = Entry(inter_frame)
label = Label(inter_frame, text="Enter the image URL: ")
file_format = Label(inter_frame, text="Choose your file format: ")
format_entry = Entry(inter_frame)
file_name = Label(inter_frame, text="File's name: ")
name_entry = Entry(inter_frame)
check_name = Checkbutton(inter_frame, text="Give a random name", variable=num)
check_format = Checkbutton(inter_frame, text="Download with default format", variable=var)
file_name.pack(anchor=CENTER, expand=1)
name_entry.pack(anchor=CENTER, expand=1)
check_name.pack(anchor=CENTER, expand=1)
label.pack(anchor=CENTER, expand=1)
url_entry.pack(anchor=CENTER, expand=1)
file_format.pack(anchor=CENTER, expand=1)
format_entry.pack(anchor=CENTER, expand=1)
# check_format.pack(anchor=CENTER)
inter_frame.pack(expand=1)
root.mainloop()
# the end!
main_menu()
Upvotes: 2
Views: 79
Reputation: 5202
To get the size do a HEAD
requests:
file_size = int(requests.head('https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tar.gz', headers={'accept-encoding': ''}).headers['Content-Length'])
Or a GET
requests (may be incorrect):
file size = int(requests.get('https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tar.gz').headers['Content-Length'])
Upvotes: 2