Python +;50 PySimpleGUI:链接到下载文件的进度条(这次我实际上得到了代码)
从链接开始,我有一些什么代码 (感谢其中一位回应者,@Jason Yang) 我已将他们的代码编辑为:Python +;50 PySimpleGUI:链接到下载文件的进度条(这次我实际上得到了代码),python,python-3.x,Python,Python 3.x,从链接开始,我有一些什么代码 (感谢其中一位回应者,@Jason Yang) 我已将他们的代码编辑为: 导入线程 从时间上导入睡眠 从随机导入randint 将PySimpleGUI导入为sg 导入urllib.request 从clint.textui导入进度 导入请求 url=”http://mirrors.evowise.com/linuxmint/debian/lmde-4-cinnamon-64bit.iso" path='/Users/me/test.iso' def下载_文件(窗口
导入线程
从时间上导入睡眠
从随机导入randint
将PySimpleGUI导入为sg
导入urllib.request
从clint.textui导入进度
导入请求
url=”http://mirrors.evowise.com/linuxmint/debian/lmde-4-cinnamon-64bit.iso"
path='/Users/me/test.iso'
def下载_文件(窗口):
r=requests.get(url,stream=True)
将open(路径“wb”)作为f:
total_length=int(r.headers.get('content-length'))
对于chunk in progress.bar(r.iter\u content(chunk\u size=1024),预期的\u size=(总长度/1024)+1):
如果区块:
f、 写入(块)
f、 刷新()
窗口。写入事件值(“下一步”,计数)
sg.主题(“暗蓝色”)
进度条=[
[sg.ProgressBar(100,大小=(40,20),pad=(0,0),key='ProgressBar'),
sg.Text(“0%”,size=(4,1),key='Percent'),],
]
布局=[
[sg.按钮(“下载”)],
[sg.pin(sg.Column(进度条,key='progress',visible=False)),
]
window=sg.window('Title',layout,size=(520,80),finalize=True,
使用默认值(焦点=False)
下载=窗口[“下载”]
进度条=窗口[“进度条”]
百分比=窗口['percent']
progressB=窗口['Progress']
尽管如此:
事件,值=window.read()
如果事件==sg.WINDOW\u关闭:
打破
elif事件==“下载”:
计数=0
download.update(disabled=True)
进度条更新(当前计数=0,最大值=100)
progressB.update(可见=真)
thread=threading.thread(target=download_文件,args=(窗口),daemon=True)
thread.start()
elif事件==“下一步”:
计数=值[事件]
进度条更新(当前计数=计数)
更新百分比(值=f'{count:>3d}%')
window.refresh()
如果计数=100:
睡眠(1)
download.update(disabled=False)
progressB.update(可见=假)
window.close()
所以我做了下载功能,但我似乎找不到一种方法来获取百分比并将其链接到进度条+百分比文本
(再次感谢@Jason Yang,感谢他们的原始代码,我通过编辑从URL下载了)
更新,到目前为止我有以下代码:
导入线程
从时间上导入睡眠
从随机导入randint
将PySimpleGUI导入为sg
导入urllib.request
从clint.textui导入进度
导入请求
url=”http://mirrors.evowise.com/linuxmint/debian/lmde-4-cinnamon-64bit.iso"
path='/Users/I讨厌这个/test.iso'
百分比=无
def下载_文件(窗口):
r=requests.get(url,stream=True)
将open(路径“wb”)作为f:
total_length=int(r.headers.get('content-length'))
对于chunk in progress.bar(r.iter\u content(chunk\u size=1024),预期的\u size=(总长度/1024)+1):
如果区块:
预期大小=(总长度/1024)
f、 写入(块)
f、 刷新()
百分比=1024*(总长度+1)/预期大小
打印(整数(百分比))
窗口。写入事件值(“下一步”,计数)
sg.主题(“暗蓝色”)
进度条=[
[sg.ProgressBar(100,大小=(40,20),pad=(0,0),key='ProgressBar'),
sg.Text(“0%”,size=(4,1),key='Percent'),],
]
布局=[
[sg.按钮(“下载”)],
[sg.pin(sg.Column(进度条,key='progress',visible=False)),
]
window=sg.window('Title',layout,size=(520,80),finalize=True,
使用默认值(焦点=False)
下载=窗口[“下载”]
进度条=窗口[“进度条”]
百分比=窗口['percent']
progressB=窗口['Progress']
尽管如此:
事件,值=window.read()
如果事件==sg.WINDOW\u关闭:
打破
elif事件==“下载”:
计数=0
download.update(disabled=True)
进度条更新(当前计数=0,最大值=100)
progressB.update(可见=真)
thread=threading.thread(target=download_文件,args=(窗口),daemon=True)
thread.start()
elif事件==“下一步”:
计数=值[事件]
进度条更新(当前计数=计数)
更新百分比(值=f'{count:>3d}%')
window.refresh()
如果计数=100:
睡眠(1)
download.update(disabled=False)
progressB.update(可见=假)
window.close()
但它似乎没有打印出一个百分比。还有@Jason Yang,这里没有i
,所以请为您编辑该评论示例
import threading
from time import sleep
from random import randint
import PySimpleGUI as sg
import urllib.request
import requests
path = 'd:/selenium-server-standalone-3.141.59.jar'
percentE = None
def download_file(window):
url = "https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar"
with requests.get(url, stream=True) as r:
chunk_size = 64*1024
total_length = int(r.headers.get('content-length'))
total = total_length//chunk_size if total_length % chunk_size == 0 else total_length//chunk_size + 1
with open(path, 'wb') as f:
for i, chunk in enumerate(r.iter_content(chunk_size=chunk_size)):
f.write(chunk)
percentE = int((i+1)/total*100)
window.write_event_value('Next', percentE)
sg.theme("DarkBlue")
progress_bar = [
[sg.ProgressBar(100, size=(40, 20), pad=(0, 0), key='Progress Bar'),
sg.Text(" 0%", size=(4, 1), key='Percent'),],
]
layout = [
[sg.Button('Download')],
[sg.pin(sg.Column(progress_bar, key='Progress', visible=False))],
]
window = sg.Window('Title', layout, size=(520, 80), finalize=True,
use_default_focus=False)
download = window['Download']
progress_bar = window['Progress Bar']
percent = window['Percent']
progressB = window['Progress']
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'Download':
count = 0
download.update(disabled=True)
progress_bar.update(current_count=0, max=100)
progressB.update(visible=True)
thread = threading.Thread(target=download_file, args=(window, ), daemon=True)
thread.start()
elif event == 'Next':
count = values[event]
progress_bar.update(current_count=count)
percent.update(value=f'{count:>3d}%')
window.refresh()
if count == 100:
sleep(1)
download.update(disabled=False)
progressB.update(visible=False)
window.close()
将此语句放在for loop
窗口中。写入事件值(“下一步”,计数)
其中count
是下载的卡盘的百分比,您需要计算它。我如何计算它?我看不出有什么可以用它来计算的。那int(1024*(I+1)/预期大小)
呢?等等,你是怎么计算出来的?哦,顺便说一下,代码中没有I