84669인 학습
152542인 학습
20005인 학습
5487인 학습
7821인 학습
359900인 학습
3350인 학습
180660인 학습
48569인 학습
18603인 학습
40936인 학습
1549인 학습
1183인 학습
32909인 학습
我在尝试写个命令行的fm,但是碰到播放时间的适合遇到了问题,如何实现一个计时器,每次自减1秒后覆盖之前的print 输出我想把歌曲总时间传入如下代码,封装个函数,但是如何覆盖上一次print的结果呢?
def remain(min) count = 0 while (count < min): count += 1 n = min - count time.sleep(1) print n
学习是最好的投资!
先科普下,"\r" 表示光标回到行首(参考自ascii编码表)。利用它就能达到覆盖字符的效果,但前提是别换行(消灭\n)。来吧~ 看吾主的神迹。
pythonline = "=============================" print(line + "\r" + line, end='');print("\r" + line + "\r" + line)
python
line = "=============================" print(line + "\r" + line, end='');print("\r" + line + "\r" + line)
再补充个进度条demo
pythonimport time lineLength = 20 delaySeconds = 0.05 frontSymbol = '=' frontSymbol2 = ['—', '\\', '|', '/'] backSymbol = ' ' for i in range(10): lineTmpla = "{:%s<%s} {} {:<2}"%(backSymbol, lineLength) for j in range(lineLength): tmpSymbol = frontSymbol2[j%(len(frontSymbol2))] print("\r" + lineTmpla.format(frontSymbol * j, tmpSymbol, j), end='') time.sleep(delaySeconds)
import time lineLength = 20 delaySeconds = 0.05 frontSymbol = '=' frontSymbol2 = ['—', '\\', '|', '/'] backSymbol = ' ' for i in range(10): lineTmpla = "{:%s<%s} {} {:<2}"%(backSymbol, lineLength) for j in range(lineLength): tmpSymbol = frontSymbol2[j%(len(frontSymbol2))] print("\r" + lineTmpla.format(frontSymbol * j, tmpSymbol, j), end='') time.sleep(delaySeconds)
先科普下,"\r" 表示光标回到行首(参考自ascii编码表)。利用它就能达到覆盖字符的效果,但前提是别换行(消灭\n)。
来吧~ 看吾主的神迹。
再补充个进度条demo