テキストプログレスバーが更新されたPytho3.8は、time.clock()メソッドの置換メソッドを廃止します。

問題の原因

Pythonプログラミングの基本で3.7.3の更新されたテキスト進行状況バーを学習したとき、time.clockメソッドを使用できないことがわかりました。プロンプト:AttributeError:モジュール「time」には属性「clock」がありません。クエリを実行した後、time.clock()がPython3.8で廃止されたことがわかりました。
クロックエラーのスクリーンショット

import time
scale=50
print("执行开始".center(scale//2,'-'))
t=time.clock()
for i in range(scale+1):
    a='*'*i
    b='.'*(scale-i)
    c=(i/scale)*100
    t-=time.clock()
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,-t),\
          end='')
    time.sleep(0.05)
print("\n"+"执行结束".center(scale//2,'-'))

別の方法

検索後、2つの方法があります

1.time.perf_counter()

2. time.process_time()

これら2つのメソッドの違いは、time.sleep()で時間を計算するかどうかです。

import time
t0=time.process_time()
time.sleep(1)
t1=time.process_time()
print(t1-t0)#=0.0
t2=time.perf_counter()
time.sleep(1)
t3=time.perf_counter()
print(t3-t2)#=1.0002965000000001

テスト後、メソッド1のperf_counter()はsleep()の値を計算しますが、メソッド2のprocess_time()は計算しません。

問題が解決しました

この質問と組み合わせて、sleep()メソッドが質問で使用されるため、メソッド1を使用します。

import time
scale=50
print("执行开始".center(scale//2,'-'))
t=time.perf_counter()
for i in range(scale+1):
    a='*'*i
    b='.'*(scale-i)
    c=(i/scale)*100
    t-=time.perf_counter()
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,-t),\
          end='')
    time.sleep(0.05)
print("\n"+"执行结束".center(scale//2,'-'))

結果は本TextProgressBar
と同じで、問題は解決しました。
これら2つの方法について詳しく知りたい場合は、[https://blog.csdn.net/qq_27283619/article/details/89280974]を参照してください
。3つのリンクを提供する

おすすめ

転載: blog.csdn.net/AQ_No_Happy/article/details/107103560