python计时器总结_Python实现进度条总结,倒计时

1 先说一下文本系统的控制符:2 \r: 将光标移动到当前行的首位而不换行;3 \n: 将光标移动到下一行,并不移动到首位;4 \r\n: 将光标移动到下一行首位。5

6 环境:7 root@ubuntu16:/alex/py/jingdutiao#python3

8 Python 3.5.2 (default, Jul 5 2016, 12:43:10)9 [GCC 5.4.0 20160609] on linux10 Type "help", "copyright", "credits" or "license" formore information.11 >>>

12

13 方式1:14 root@ubuntu16:/alex/py/jingdutiao#cat test1.py

15 #!/usr/bin/env python

16 from __future__ importdivision17 importsys,time18 j = '#'

19 if __name__ == '__main__':20 for i in range(1,61):21 j += '#'

22 sys.stdout.write(str(int((i/60)*100))+'%'+j+'->'+ "\r")23 sys.stdout.flush()24 time.sleep(0.5)25 print

26

27 root@ubuntu16:/alex/py/jingdutiao#python3 test1.py

28 98% ############################################################->

29

30

31 方式2:32 root@ubuntu16:/alex/py/jingdutiao#cat test4.py

33 #!/usr/bin/env python

34 #-*- coding:utf-8 -*-

35 __author__ = 'l'

36

37 importsys38 from time importsleep39 defviewBar(i):40 """

41 进度条效果42 :param i:43 :return:44 """

45 output =sys.stdout46 for count in range(0, i + 1):47 second = 0.1

48 sleep(second)49 output.write('\rcomplete percent ----->:%.0f%%' %count)50 output.flush()51

52 viewBar(100)53 root@ubuntu16:/alex/py/jingdutiao#python3 test4.py

54 complete percent ----->:100%

55

56

57 方式3:58 tqdm模块59 tqdm是一个快速、扩展性强的进度条工具库,60 其githup地址:https://github.com/tqdm/tqdm61

62 1)安装:63 直接使用pip安装:64 pip install tqdm65 2)使用:66 from time importsleep67 from tqdm importtqdm68 for i in tqdm(range(1, 500)):69 sleep(0.01)70

71 自己实操:在ubuntu上默认安装到2.7环境变量里去了72 root@ubuntu16:/alex/py/jingdutiao#pip install tqdm

73 Collecting tqdm74 Downloading tqdm-4.8.4-py2.py3-none-any.whl75 Installing collected packages: tqdm76 Successfully installed tqdm-4.8.4

77 You are using pip version 8.1.1, however version 8.1.2 isavailable.78 You should consider upgrading via the 'pip install --upgrade pip'command.79

80 pip install --upgrade pip81 pip install tqdm82 pip -V83 cd /usr/local/lib/python2.7/dist-packages/

84 cp -r tqdm tqdm-4.8.4.dist-info/ /usr/local/lib/python3.5/dist-packages85 root@ubuntu16:/alex/py/jingdutiao#cat test5.py

86 from time importsleep87 from tqdm importtqdm88 for i in tqdm(range(1, 500)):89 sleep(0.01)90 root@ubuntu16:/alex/py/jingdutiao#python3 test5.py

91 100%|████████████████████████████████████████████████████████████████████████| 499/499 [00:05<00:00, 92.20it/s92

93

94

95 方式4:96 root@ubuntu16:/alex/py/jingdutiao#cat test2.py

97

98 classProgressBar():99 def __init__(self, width=50):100 self.pointer =0101 self.width =width102 def __call__(self,x):103 #x in percent

104 self.pointer = int(self.width*(x/100.0))105 return "|" + "#"*self.pointer + "-"*(self.width-self.pointer)+ "|\n %d percent done" %int(x)106

107 if __name__ == '__main__':108 importtime,os109 pb =ProgressBar()110 for i in range(101):111 os.system('clear')112 print( pb(i))113 time.sleep(0.1)114

115 root@ubuntu16:/alex/py/jingdutiao#116 执行结果:117 |#################################################-|

118 percent done119 |##################################################| #输出100行内容,但是在屏幕会实时清屏,只展示1行

120 percent done121

122

123 方式5:124 root@ubuntu16:/alex/py/jingdutiao#python3 test3.py

125 ====================================================================================================>100%

126 #cat test3.py

127 importsys128 importtime129 defview_bar(num,total):130 rate = num /total131 rate_num = int(rate * 100)132 #r = '\r %d%%' %(rate_num) #r = '\r%s %d%%' % ('当前进度:' ,rate_num)

133 r = '\r%s>%d%%' % ('=' *rate_num, rate_num,)134 sys.stdout.write(r)135 sys.stdout.flush136 if __name__ == '__main__':137 for i in range(0, 101):138 time.sleep(0.1)139 view_bar(i, 100)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值