0
import time
import sys
progressbar = []

dashes = 1
spaces = 10
for i in range(11):
    print("Logging In[", end = "")
    for x in range(dashes):
        dash = "|"
        progressbar.append(dash)
        print(progressbar[x], end = "")
    for y in range(spaces):
        print(end = " ")

    dashes += 1
    spaces -= 1
    print("]")

sys.stdout.write()
sys.stdout.flush()

I was wondering how to get this progress bar to print on only one line like a game or loading screen. It prints over and over again on a new line each time and I have no clue how to fix it.

Raniz
  • 10,882
  • 1
  • 32
  • 64

1 Answers1

-1

You can go back at the beginning of the line by terminating your line with a "\r" (print("something", end="\r")) instead of the usual "\r\n" on Windows and "\n" on UNIX.

For example:

import time
import sys


dashes = 1
spaces = 10
for i in range(11):
    progressbar = ['|' for x in range(dashes)]
    print("Logging In [", "|" * dashes, " " * spaces, "]", end="\r")
    dashes += 1
    spaces -= 1
    sys.stdout.flush()
    time.sleep(1)

Edit: The link posted by Yakir Tsuberi is more complete, don't mind my answer

Elominp
  • 59
  • 5