2

I found a working progress bar here: Using PySimpleGUI, how could I make it download a file with a progress bar? . I need it for PySimpleGUIQt. But it's not working.

import requests
import threading

import PySimpleGUIQt as sg
from math import floor
from tqdm import tqdm

header = {"Authorization": "token ghp_*****************************************",
          "Accept": "application/vnd.github.v4.raw"}
link_up = 'https://raw.githubusercontent.com/[user]/[repo]/[branch]/[file name]'


def download_file(windows, link, f_name):
    resp = requests.get(link, headers=header, stream=True)
    total = int(resp.headers.get('content-length', 0))
    ceil_value = floor(total / 100)
    with open(f_name, 'wb') as file, tqdm(desc=f_name, total=total, unit='iB', unit_scale=True, unit_divisor=ceil_value,
    ) as bar:
        x_i = 0
        for data in resp.iter_content(chunk_size=ceil_value):
            size = file.write(data)
            bar.update(size)
            windows.write_event_value('Next', x_i)
            x_i = x_i + 1


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.Column(progress_bar, key='Progress')],
]
window = sg.Window('Title', layout, size=(520, 80), finalize=True, use_default_focus=False)

file_name = 'upload-launcher.exe'
thread = threading.Thread(target=download_file, args=(window, link_up, file_name), daemon=True)
thread.start()
print('Download Finished')

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break

    if event == 'Download':
        count = 0
        window['Progress Bar'].update(current_count=0, max=100)
        thread = threading.Thread(target=download_file, args=(window, link_up, file_name), daemon=True)
        thread.start()
    elif event == 'Next':
        count = values[event]
        window['Progress Bar'].update(current_count=count)
        window['Percent'].update(value=f'{count:>3d}%')
        window.refresh()

The error I'm getting is:

upload-launcher.exe:   1%|          | 1.00k/100k [00:00<01:37, 449kiB/s]
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\WoLvES\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner
    self.run()
  File "C:\Users\WoLvES\AppData\Local\Programs\Python\Python39\lib\threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "C:\pythoncharm\clean\t1.py", line 23, in download_file
    windows.write_event_value('Next', x_i)
AttributeError: 'Window' object has no attribute 'write_event_value'

IDK if there are other errors

Ifan_Ifan
  • 43
  • 4

0 Answers0