免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2584 | 回复: 0
打印 上一主题 下一主题

写控制台进度显示类的时候碰到的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-23 18:29 |只看该作者 |倒序浏览
我就是想在控制台下运行下载程序的时候进行如下的显示


  1. 下载线程1的文字提示-----10%
  2. 下载线程2的文字提示-----10%
  3. 下载线程3的文字提示-----10%
复制代码


但我努力了半天还是不行,如果是单线程的情况下我就是用 先显示 一个数字 然后在 \b\b\b\b 退回,然后在显示。但这样还有问题。Print 加逗号 显示有一个强制的空格。
经过反复试验后,我写出了一个类。适合单线程的程序。如果多线程的话,就成了。


  1. 下载进程1的文字提示-----下载进程2的文字提示-----下载进程3的文字提示-----10%
复制代码


不知道有没有解决的方法啊。快疯了。。我写这个类是给我的下载程序用的。
可以看
http://bbs.chinaunix.net/viewthread.php?tid=796303&extra=page%3D1



最后是我写的显示进度类代码

  1. # Class Name: Progress Number Class
  2. #
  3. # Author: rikioy
  4. #
  5. # Email: rikioy at gmail DOT com
  6. #
  7. # Date: 07/23/2006
  8. #
  9. #

  10. class progressNumber:
  11.     "Print prgress number behind the string."
  12.     def __init__( self,finalcount):
  13.         self.finalcount = finalcount
  14.     def progress(self,count):
  15.         "Update the current progress."
  16.         
  17.         count = min(count,self.finalcount)
  18.         if self.finalcount:
  19.             percentcomplete = int(round(100*count/self.finalcount))
  20.         
  21.             if percentcomplete < 10:
  22.                 print "%d%%" % percentcomplete,
  23.                 print "\b\b\b\b",
  24.             else:
  25.                 print "%d%%" % percentcomplete,
  26.                 print "\b\b\b\b\b",
  27.         else:
  28.             pass

  29. if __name__ == '__main__':
  30.     from time import sleep
  31.    
  32.     print "Name          : Class prgressNumber"
  33.     print "Class __doc__ : %s\n" % progressNumber.__doc__
  34.    
  35.     print "Demo."
  36.     #notice: Add one comma behind the string
  37.     #When you use comma in print,the pring don't output '\n'
  38.     print "Display the progress behind this string.",
  39.     pn = progressNumber(70)
  40.     count = 0
  41.     while count < 70:
  42.         count += 1
  43.         pn.progress(count)
  44.         sleep(0.1)
复制代码

[ 本帖最后由 rikioy 于 2006-7-24 19:04 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP