0

I know the title is quite vague, but I have no idea how to sum what I want to do.

An example is a loading screen, I want to do something like this:

Loading assets - 10%

and then remove the output from the terminal and print

Loading textures - 20%

Every time I try something like this I get

Loading assets - 10%
Loading textures - 20%

How can I do this?

  • 2
    Can I suggest you to adopt some kind of library for this, like [tqdm](https://github.com/tqdm/tqdm), instead of writing your own stuff? – Alberto Re May 07 '18 at 08:26
  • For which kind of operating system and terminal do you want this to work? Windows default `cmd` has quite different requirements for this than must UNIX terminals. – Martijn May 07 '18 at 09:10

4 Answers4

0

Use os package and clear screen and rewrite every update time.

import os
os.system("clear")
print "your text"
n1md7
  • 2,854
  • 1
  • 12
  • 27
  • That would work but I have other things on the screen that I don't want to clear, are there any other solutions? – Basic and Default May 07 '18 at 08:26
  • probably you need this. https://stackoverflow.com/questions/36520120/overwriting-clearing-previous-console-line – n1md7 May 07 '18 at 08:29
0

Assuming that you could be using python 3.3+ the print statement can help you with this:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

So when printing use:

print('Mytext', flush=True)
zipa
  • 27,316
  • 6
  • 40
  • 58
0

try this script ; i tested it and it work fine , i added time.sleep for you see the effects

import os
from platform import system
import time


if system() == 'Windows':
   os.system('cls')
if system() == 'Linux':
   os.system('cls')

print("Loading Images - 10%")

time.sleep(2)

if system() == 'Windows':
   os.system('cls')
if system() == 'Linux':
   os.system('cls')

print("Loading Texture - 20%")
Skiller Dz
  • 897
  • 10
  • 17
0

Clear and simple solution for Python 3

import sys, time
for x in (10, 20, 30):
    print("Loading assets - {}%".format(x), end="\r")
    time.sleep(2)

For good measure, you may want to add a sys.stdout.flush() after the print. This works on UNIXes at least…

frapadingue
  • 209
  • 1
  • 8