Python: Text achieve progress bar and simple analytical comments

Python: Text achieve progress bar and simple analytical comments

Requirements Analysis:
text string printed using the progress bar mode, the printout can change dynamically, requiring a need for gradual change in the progress bar in a row.
Analysis:
How to get the text changes over time progress bar? → use sleep () to simulate a continuous progress;
single row to increase the dynamic refresh → "\ r"; (refresh essentially before covering with printed characters after the character, the cursor is returned to the position before the post-printing)

Code Example (Note including Analysis):

#文本进度条
import time
scale=50
print("执行开始".center(scale//2,"-"))
#使用字符串处理中的.center方法,讲一个“-”字符填充在执行开始
start=time.perf_counter()  #增加计时效果
for i in range (scale+1):
  a='*'*i
  b='.'*(scale-i)
  c=(i/scale)*100
  dur=time.perf_counter()-start
  #用来每一次用来打印文本进度条所用的时间,方法就是每次调用time.perf_counter()函数
  print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end="")
  #为了使文本进度条有单行刷新效果,增加“\r”,实现光标向行首移动
  #增加end函数,把end函数赋值为空字符串,在每次输出后不换行
  time.sleep(0.1)
print("\n"+"执行结束".center(scale//2,'-'))
#使用字符串处理中的.center方法,讲一个“-”字符填充在执行结束


#覆盖了字符串处理、数字处理以及时间库的使用等方面的内容。

Note: This paragraph will get an error code to run when the IDLE environment. Because IDLE is itself a major environmental programming development environment is not running, so you want to run before they can achieve print out the text of the progress bar at the command line mode. DETAILED lower attachment method.

  1. Windows + R, type cmd, and open a command prompt;
    Windows + R interface

  2. By command, find the location of the file;
    File location

  3. Command: python .py file name completion progress bar single line of text printouts.
    Print output
    Brief description:
    1, the calculation of the expansion
    (1) text progress bar program uses perf_counter () timing;
    (2) the timing method is suitable for all kinds of issues need to calculate the statistics of time.
    For example: time to compare different algorithms, statistical part of the program running time
    2, the progress of the application
    (1) requires a longer program to increase the progress bar running at any time;
    (2) to improve the user experience of the application to increase the progress bar at any desired ;
    (3) the progress bar is one link human-computer interaction.

Released three original articles · won praise 4 · views 48

Guess you like

Origin blog.csdn.net/weixin_44170420/article/details/104924990