-1

I may have phrased the title oddly, but essentially I'm wanting the console to display:

'Loading...'

Where the dots loop, on the same line.

  1. 'Loading'
  2. 'Loading.'
  3. 'Loading..'
  4. 'Loading...'

My thought pattern would be to have four different strings, print one, clear the line, print the next, etc.

Is this possible to implement in Python? Perhaps using Windows console commands?

Thanks

madmonkey
  • 83
  • 8
  • What are you trying to do, more generally? Why do you want to simulate console activity? – Be Chiller Too Aug 17 '21 at 15:23
  • Does this answer your question? [How to manipulate cmd.exe from python, so that the output is shown in the cmd.exe window](https://stackoverflow.com/questions/20472367/how-to-manipulate-cmd-exe-from-python-so-that-the-output-is-shown-in-the-cmd-ex) – Be Chiller Too Aug 17 '21 at 15:25
  • using `\r` instead of `\n` you can move to the beginning of line and write again in the same place `print("Loading", end="\r")`, `print("Loading.", end="\r")`. OR simply use `print(".", end="")` to write dot after `Loading` without cleaning line. – furas Aug 17 '21 at 16:33

2 Answers2

0

see this: Python Progress Bar

You can use a progress bar, which looks better ( taken from the link )
Code Example from the link:

from time import sleep
from tqdm import tqdm
for i in tqdm(range(10)):
    sleep(3)

 60%|██████    | 6/10 [00:18<00:12,  0.33 it/s]
Jonathan
  • 88
  • 1
  • 8
0

If you use end="\r" in print() then it will not go to next line but it will move to beginning of line - and then you can write again in the same line.

import time

print("Loading", end="\r")
time.sleep(1)
print("Loading.", end="\r")
time.sleep(1)
print("Loading..", end="\r")
time.sleep(1)
print("Loading...", end="\r")
time.sleep(1)

print()  # move to next line

And this can be useful when you have to change text in line. But for adding dots it is simpler to use end="" to write in the same line

import itme

print("Loading", end="")

for _ in range(3):
    time.sleep(1)
    print(".", end="")

print()  # move to next line

This method is used by some modules to draw progressbar


BTW:

You can also \r and end="" to keep cursor at the end of line

import time

print("\rLoading", end="")
time.sleep(1)
print("\rLoading.", end="")
time.sleep(1)
print("\rLoading..", end="")
time.sleep(1)
print("\rLoading...", end="")
time.sleep(1)

print()  # move to next line

If new text is shorter than older text then you should write spaces at the end to remove previous text.

import time

print("\r....", end="")
time.sleep(1)
print("\r... ", end="")
time.sleep(1)
print("\r.. ", end="")
time.sleep(1)
print("\r. ", end="")
time.sleep(1)
print("\r ", end="")

print()  # move to next line

If you want to display other text at the same time then you may need to use module like curses, urwind, npyscreen which can use special codes to move cursor in differen places in (some) terminals.

furas
  • 134,197
  • 12
  • 106
  • 148