5
8

More than 1 year has passed since last update.

プログレスバーを文字列で表現する関数

Last updated at Posted at 2021-10-02

車輪の再発明。もはや趣味。
printしてもいいし、TkinterのlabelのtextVariableを変えてもいいかも

def progressbar(current, max) -> str:
    '''
    args
        current: int/float 現在値
        max: int/float 最大値

    length:いじれる。表示したい長さに合わせる。
    bar:いじれる。好みでどうぞ。今見えてるやつは、プロポーショナルフォントでも使える
    '''
    ratio = current / max
    length = 20
    progress = int(ratio * length)
    bar =  f'[{"■" * progress}{"□" * (length - progress)}]'
    percentage = int(ratio * 100)
    return f'{bar} {percentage}%'
    '''
    [■■■■■■■■■■□□□□□□□□□□] 54%
    [■■■■■■■■■■■□□□□□□□□□] 58%
    [■■■■■■■■■■■■□□□□□□□□] 62%
    [■■■■■■■■■■■■■□□□□□□□] 66%
    [■■■■■■■■■■■■■■□□□□□□] 70%
    [■■■■■■■■■■■■■■■□□□□□] 75%
    [■■■■■■■■■■■■■■■□□□□□] 79%
    [■■■■■■■■■■■■■■■■□□□□] 83%
    [■■■■■■■■■■■■■■■■■□□□] 87%
    [■■■■■■■■■■■■■■■■■■□□] 91%
    [■■■■■■■■■■■■■■■■■■■□] 95%
    [■■■■■■■■■■■■■■■■■■■■] 100%
    (呼び出してprintした時のイメージ)
    '''
5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8