python - 为更新的 exe 创建一个下载栏

标签 python python-3.x progress-bar pyinstaller download

我正在尝试为我的更新脚本添加一个下载栏,但我不太清楚该怎么做。奇怪的是,目前在调试期间出现了一个下载栏:

https://github.com/JMSwag/PyUpdater/blob/master/pyupdater/client/downloader.py

DEBUG:pyupdater.client.downloader:{'total': 11128343, 'downloaded': 16385, 'status': 'downloading', 'percent_complete': '0.1', 'time': '05:36'}
DEBUG:pyupdater.client.downloader:Block size: 43553
DEBUG:pyupdater.client.downloader:{'total': 11128343, 'downloaded': 49153, 'status': 'downloading', 'percent_complete': '0.4', 'time': '04:42'}
DEBUG:pyupdater.client.downloader:Block size: 43515

我查看了他们的 downloader.py 并尝试创建某种进度条。关于如何实现这一目标的任何想法? 目前显示一个条(已完成),但没有进度条显示还需要多长时间。

def check_for_update():
    bar = progressbar.ProgressBar()
    for i in bar(range(100)):
        time.sleep(0.0)

    def cb(status):
        zz = status['downloaded'] * 100.0 / status['total']

        zz = bar.update(status.the_dwad)

        print(zz)

    import os
    import sys
    from contextlib import contextmanager


    import sys
    import os
    stdout_save = sys.stdout
    sys.stdout = open(os.devnull, 'w')

    client = Client(ClientConfig(), refresh=True, progress_hooks=[cb], headers={'basic_auth': 'brofewfefwefewef:EKAXsWkdt5H6yJEmtexN'})

看起来:def cb(status): 没有被调用

如果需要复制 here,我还提供了完整的脚本和其他信息

最佳答案

代码的一些问题。

  • 你不应该启用调试日志,否则会妨碍进度条
  • 你没有启动进度条
  • 进度条回调 status.the_dwad
  • 出错
  • 代码在 IDE 中可能无法正常运行,因为进度条存在一些问题,因此您应该在终端中运行它

Progress bar working

下面是我运行的代码

import urllib3.poolmanager

orig_urlopen = urllib3.poolmanager.PoolManager.urlopen


def new_urlopen(self, method, url, redirect=True, **kw):
    if "s3.amazonaws.com" in url and 'authorization' in self.headers:
        self.headers.pop('authorization')
    return orig_urlopen(self, method, url, redirect, **kw)


urllib3.poolmanager.PoolManager.urlopen = new_urlopen

import logging

# logging.basicConfig(level=logging.DEBUG)

import http.client as http_client

# http_client.HTTPConnection.debuglevel = 1

import logging
from selenium import webdriver

# logging.basicConfig(level=logging.DEBUG)

from client_config import ClientConfig
from pyupdater.client import Client, AppUpdate

import progressbar
import sys

bar = None


def check_for_update():

    def cb(status):
        global bar

        if bar is None:
            bar = progressbar.ProgressBar(widgets=[progressbar.Percentage(), progressbar.Bar()], fd=sys.stdout).start()
        zz = float(status['percent_complete'])

        bar.update(zz)

    # sys.stdout = open(os.devnull, 'w')

    client = Client(ClientConfig(), refresh=True,
                    headers={'basic_auth': 'brofewfefwefewef:EKAXsWkdt5H6yJEmtexN'})

    client.platform = "win"
    app_update = client.update_check(ClientConfig.APP_NAME, ClientConfig.APP_VERSION, channel='stable')
    if app_update is not None:
        app_update.progress_hooks.append(cb)
        if app_update.download():
            if isinstance(app_update, AppUpdate):
                app_update.extract_restart()
                return True
            else:
                app_update.extract()
                return True
    return False


def main():
    print('Current version is ', ClientConfig.APP_VERSION)
    if check_for_update():
        print('there\'s a new update :D')
    driver = webdriver.Firefox()
    driver.get('https://www.youtube.com/')


if __name__ == "__main__":
    main()

关于python - 为更新的 exe 创建一个下载栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48248013/

相关文章:

python - 从python中的字符串消息中提取括号 '[ ]'中指定的数据

python - QTableWidget 当前选择改变信号

file - 扫描硬盘时的进度条

python - "do this in one pass"是什么意思?

Python pandas dataframe 将元素添加到前一行值并创建一个新列

python-3.x - Markdown 文本突出显示性能问题 - Tkinter

java - ProgressBar 在一台设备上无法工作,但在另一台设备上工作正常

由多个值填充的 CSS 静态进度条

python - 合并 2 个以上子查询的有效查询

python - 如何排除 Pandas 标题中的第一个单词?