例4.1.设置一组文本进度条,使之运行效果如下:

--------执行开始--------
% 0 [->**********]
%10 [*->*********]
%20 [**->********]
%30 [***->*******]
%40 [****->******]
%50 [*****->*****]
%60 [******->****]
%70 [*******->***]
%80 [********->**]
%90 [*********->*]
%100[**********->]
--------执行结束--------

 #文本进度条1.py
import time
print("{0:-^20}".format('执行开始'))
scale = 10
for i in range(scale+1):
a = '*' * i
b = '*' * (scale - i)
c = (i/scale)*100
print("%{:^3.0f}[{}->{}]".format(c,a,b))
time.sleep(0.5)
print("{0:-^20}".format('执行结束'))

在此讲解time库的使用:

获取时间:time() 得到时间戳;  

     ctime()得到时间的字符串表达形式;

     gmtime()得到时间的程序表达形式。

时间格式化:strftime(tpl ,ts ) 其中tpl是格式化模板字符串,ts是计算机内部时间类型变量

        如>>>t = time.gmtime()

             time.strftime("%Y-%m-%d   %H:%M:%S",t)

      strptime(str,tpl)

程序计时:perf_counter() 返回精确的时间计数值。

     sleep(s)休眠时间,s为秒

例4.2在命令提示符窗口实现单行动态刷新,进度条运行效果如下

100%>>>

>>> import time
>>> for i in range(101):
... print("\r{:2}%".format(i),end="")
... time.sleep(0.05)
...

例4.3实现带刷新的文本进度条

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

博客参考《Python程序设计基础》高等教育出版社(第2版) 嵩天,礼欣,黄天羽著

推荐与上述教材配套的北京理工大学 嵩天,礼欣,黄天羽老师的MOOC

最新文章

  1. DIOCP之编写第一个应用程序(二)
  2. Linux SSH,FTP服务配置
  3. HP 7440老机器重启
  4. each的用法
  5. bzoj4199
  6. Getting Started with Zend Framework MVC Applications
  7. C#利用NPOI生成具有精确列宽行高的Excel文件
  8. QFII
  9. POJ2406 KMP算法
  10. DataUml Design 教程6-DataUML Design 1.1版本号正式公布(支持PD数据模型)
  11. 关于Oracle将小于1的数字to_char后丢掉0的解决办法
  12. 同步 异步 AJAX JS
  13. jquery 点击事件切换样式
  14. 报错:Maximum call stack size exceeded
  15. Spring中的@Transactional(rollbackFor = Exception.class)属性详解
  16. hibernate入门程序
  17. C#中,重新排列panel中的按钮
  18. 019_nginx upstream中keepalive参数
  19. PID控制器(比例-积分-微分控制器)- III
  20. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

热门文章

  1. Sql server 启用调试
  2. tomcat日志切割脚本shell
  3. 2、Jmeter测试
  4. 使用BaGet来管理内部Nuget包
  5. hadoop HA + HBase HA搭建:
  6. 2018-10-25-weekly
  7. 查看Linux系统所对应的版本
  8. luogu P3411 序列变换
  9. 用于理解C++右值引用的例子
  10. Solr JAVA客户端SolrJ的使用