Im new to python and was wondering how to make a loading animation while my program runs. I need this because I don't want users thinking that the program is caught in a dead loop. I prefer a something like...
Loading...(with the dots disappearing and reappearing one by one)
Thanks!
If your output window supports the carriage return character, you can print it to make the cursor return to the beginning of the current line (provided you end your
print
statement with a comma, so a newline character isn't automatically printed). Then subsequent prints will overwrite what was already printed. You can use this to do very simple one line animation. Example:... Where
time.sleep(1)
is a placeholder representing the actual work you want to do.Result:
Then, one second later:
Then, one second later:
Then, one second later:
Then, one second later:
etc.
Compatibility note: in 3.X,
print
is no longer a statement, and the "end with a comma" trick no longer works. Instead, specify theend
parameter:The most proper way I can think of to do it would be using threading.
You would initiate a thread that starts displaying some indication that the program is doing something and then open a new thread that actually does the work.
When the thread doing the work finished then you can move on with whatever else the program does.
This looks ok when ran in windows command prompt, not sure how linux will like it: