Linking on from this post, I have some what some code.
(Thanks to one of the people who responded, @Jason Yang)
I have edited their code to this:
import threading
from time import sleep
from random import randint
import PySimpleGUI as sg
import urllib.request
from clint.textui import progress
import requests
url = "http://mirrors.evowise.com/linuxmint/debian/lmde-4-cinnamon-64bit.iso"
path = '/Users/me/test.iso'
def download_file(window):
r = requests.get(url, stream=True)
with open(path, 'wb') as f:
total_length = int(r.headers.get('content-length'))
for chunk in progress.bar(r.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1):
if chunk:
f.write(chunk)
f.flush()
window.write_event_value('Next', count)
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()
So I have made the download function BUT I can't seem to find a way to get the percentage and link it up to the progress bar + the percentage text.
(Again thanks to @Jason Yang for their original code which I edited to download the from the URL)
Update, so far I have this code:
import threading
from time import sleep
from random import randint
import PySimpleGUI as sg
import urllib.request
from clint.textui import progress
import requests
url = "http://mirrors.evowise.com/linuxmint/debian/lmde-4-cinnamon-64bit.iso"
path = '/Users/I hate this/test.iso'
percentE = None
def download_file(window):
r = requests.get(url, stream=True)
with open(path, 'wb') as f:
total_length = int(r.headers.get('content-length'))
for chunk in progress.bar(r.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1):
if chunk:
expected_size=(total_length/1024)
f.write(chunk)
f.flush()
percentE = 1024*(total_length+1)/expected_size
print(int(percentE))
window.write_event_value('Next', count)
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()
But it doesn't seem to print a percent. And @Jason Yang, there is no i
so please edit that comment