通过使用\r
和\b
转义序列来控制光标的位置,您可以使用文本创建翻页书样式的动画。以下是如何制作一个旋转器来指示忙碌状态:
#!/usr/bin/env python
from time import sleep
# Show the spinning animation 3 times
print('Everybody look busy ', end='', flush=True)
for x in range(3):
for frame in r'-\|/-\|/':
# Back up one character then print our next frame in the animation
print('\b', frame, sep='', end='', flush=True)
sleep(0.2)
# Back up one character, print a space to erase the spinner, then a newline
# so that the prompt after the program exits isn't on the same line as our
# message
print('\b ')
以下是制作进度条的方法:
#!/usr/bin/env python
from time import sleep
def progress(percent=0, width=30):
# The number of hashes to show is based on the percent passed in. The
# number of blanks is whatever space is left after.
hashes = width * percent // 100
blanks = width - hashes
print('\r[', hashes*'#', blanks*' ', ']', f' {percent:.0f}%', sep='',
end='', flush=True)
print('This will take a moment')
for i in range(101):
progress(i)
sleep(0.1)
# Newline so command prompt isn't on the same line
print()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)