0

I'm working on a small project and its all ran as a script.

in the terminal I want a small animation to go on while something "loads"

. .. ... and over again on the same line how would I go about doing this

ive made functions to clear the screen but how would i clear only the line without pause in the terminal

Ironkey
  • 2,568
  • 1
  • 8
  • 30
  • 1
    Dupe: [1](https://stackoverflow.com/questions/7039114/waiting-animation-in-command-prompt-python), [2](https://stackoverflow.com/questions/22029562/python-how-to-make-simple-animated-loading-while-process-is-running), [3](https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console), [4](https://stackoverflow.com/questions/2726343/how-to-create-ascii-animation-in-a-console-application-using-python-3-x) – ggorlen Jan 25 '20 at 00:49

1 Answers1

2

For such a simple animation, I think using a carriage return is enough (it put the print cursor to the line start). ...Just not to forget the spaces to clear the other chars ;)

from itertools import cycle
from time import sleep
n_points = 5
points_l = [ '.' * i + ' ' * (n_points - i) + '\r' for i in range(n_points) ]
cond = True

for points in cycle(points_l):
    print(points, end='')
    sleep(0.1)
    if not cond:
        break
hl037_
  • 3,520
  • 1
  • 27
  • 58
  • 1
    this works great! how can i make it stop after 3 seconds -> i made a function to do it but that didnt work – Ironkey Jan 25 '20 at 01:06
  • 1
    either check the time with time.time https://docs.python.org/3/library/time.html#time.time (store the time before the loop, then check the difference to the stored value inside the loop, and break if > 3) Either set a predefined number of time to play the animation using repeat instead of cycle : https://docs.python.org/3.9/library/itertools.html#itertools.repeat – hl037_ Jan 27 '20 at 09:39