简体   繁体   中英

How can I make "animated" text?

I'm writing something and I want the text to say

Computing...

But I want the dots (forgot what they're called) to animate one by one, repeating, like this:

Computing

Then

Computing.

Then

Computing..

Then

Computing...

And repeat. How do I do this?

import time
PAUSE = 0.5  # s
n = 0
while True:
    print('\r', 'Computing' + '.' * n + ' ' * (3 - n), end='')
    time.sleep(PAUSE)
    n = (n + 1) % 4

The magic is basically \r , it sets the cursor back to the start of the line and the next characters will overwrite the previous ones. Then, we will print "Computing" followed by a varying number of dots and spaces. The spaces are required to overwrite the dots from the previous iteration if the number of dots reduces.

Tested and working with Python 3 on

  • Ubuntu
  • Windows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM