sanguine
sanguine

Reputation: 107

How to create a simple loading screen with python using just prints?

I would like create a simple loading screen using just print, something like this:

for x in range(5):
 print("Loading...")
 sleep(1)
 os.system('cls')

How would I add more and more fullstops each time for 5 seconds? Is this attempt even close? Thank you!

Upvotes: 0

Views: 2225

Answers (5)

Shahir Ansari
Shahir Ansari

Reputation: 1848

Although vaguely you could use something like

seconds = 1
while():
    i = "." * seconds
    sys.stdout.write("\rLoading {}", %i)
    seconds += 1
    if seconds == 4: seconds = 1
    time.sleep(5)

but i would highly suggest you use tqdm which will have almost every case you want to use.

Upvotes: 1

Eliran Turgeman
Eliran Turgeman

Reputation: 1656

This is not a direct answer to your practical question, but I would consider using tqdm.
With this module you can create a progress bar which I find more elegant than using these print statemenets.
Try this:

from tqdm import trange
from time import sleep
for i in trange(100):
    sleep(0.01)

You can look at the docs for more examples here

Upvotes: 0

Valentin Vignal
Valentin Vignal

Reputation: 8248

I am not sure about what you mean by "fullstops", but here is something that can help you:

import time
print('Loading', end='')
for x in range(5):
    print('.', end='')
    time.sleep(1)
print('')

The output will be iteratively on the same line:

Loading
Loading.
Loading..
Loading...
Loading....
Loading.....

If you want to create a loading bar you can use the package LoadBar.

pip install load-bar termcolor
import time
from loadbar import LoadBar

bar = LoadBar(
    max=5
)
bar.start()
for x in range(5):
    bar.update(step=x)
    time.sleep(1)
bar.end()

The output will be iteratively on the same line:

0/5 (  0%) [.                   ] ETA -:--:--
1/5 ( 20%) [.....               ] ETA 0:00:04
2/5 ( 40%) [.........           ] ETA 0:00:03
3/5 ( 60%) [.............       ] ETA 0:00:02
4/5 ( 80%) [.................   ] ETA 0:00:01
5/5 (100%) [....................] Time 0:00:05

Upvotes: 0

chrisbyte
chrisbyte

Reputation: 1633

Without knowing what else is going on in your application or how it is structured; you can start with your loading message and then add what you want on each iteration of the loop. Something like this:

import os
from time import sleep

msg = "Loading.."
for x in range(5):
  msg = msg + "."
  print(msg)
  sleep(1)
  os.system("cls")

print("%sDone!" % msg)

Upvotes: 0

G. Anderson
G. Anderson

Reputation: 5955

Using fstrings, you can update the number of full stops by multiplying with your x in the loop

import os
from time import sleep

for x in range(5):
    print(f"Loading{'.'*(x+1)}")
    sleep(1)
    os.system('cls')

Loading.
Loading..
Loading...
Loading....
Loading.....

Upvotes: 0

Related Questions