我基本上是 python 的新手,我正在使用tkinter 和 urllib.request在 python 中构建这个下载程序,它已经完成了,但作为最后一点,我想添加一个进度条来显示下载了多少文件。我发现我可以使用TQDM添加进度条,但首先我应该得到用户想要下载的文件的大小,以显示程序下载了多少,但我什么也没找到!谁能帮我吗?我的代码(顺便说一句,如果您知道如何使我的代码更好,我将不胜感激):from tkinter import *from tkinter import font as tkFontimport randomimport urllib.requestdef 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]
1 回答
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
要获取大小,请执行以下HEAD
请求:
file_size = int(requests.head('https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tar.gz', headers={'accept-encoding': ''}).headers['Content-Length'])
或GET
请求(可能不正确):
file size = int(requests.get('https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tar.gz').headers['Content-Length'])
添加回答
举报
0/150
提交
取消