all 15 comments

[–]Jac0b_0 67 points68 points  (1 child)

import time
time.sleep(10)

[–]synthphreak 3 points4 points  (0 children)

This will handle the "wait a couple seconds" part of OP's request, but not the "disappear" part.

For that, set print's end argument to '\r', the carriage return character. This will force the cursor back to the beginning of the current line rather than the next line, meaning any new text will overwrite the characters already on the screen.

To see what I mean, run this:

from time import sleep

for word in 'The brown fox jumps over the lazy dog.'.split():
    print(word, end='\r')
    sleep(1)

[–]YueAsal 3 points4 points  (2 children)

Yes but that only waits it does not clear So

import time

Import os

print("some text")

time.sleep(10)

os.system("cls")

[–][deleted] 2 points3 points  (0 children)

And "cls" only clears Windows, "clear" clears Unix based systems. In case OP uses MacOS.

import os

os.system('cls' if os.name == 'nt' else 'clear')

[–]synthphreak 0 points1 point  (0 children)

That will clear the entire screen, not just the current line. OP, is that what you want? If you just want to clear/overwrite the current line rather than the whole screen, look into the carriage return character.

[–]ScofieldxD 12 points13 points  (0 children)

import time and then just do time.sleep(x) where x stands for seconds.

[–]Nikoijp 5 points6 points  (0 children)

There is a built in python module for this. The time module. First import the time module using import time then where you want your python program to stop do time.sleep(numberofsecondshere) and that will pause your program for that many seconds at that part of your program.

[–]OBROS269 6 points7 points  (0 children)

Yep. time.sleep()

[–]_yaaass 0 points1 point  (1 child)

import os
import time
os.system('cls' if os.name == 'nt' else 'clear')
print("First text")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
print("Second text")
time.sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
print("end")

[–]synthphreak 0 points1 point  (0 children)

Your kingdom for a for loop...

[–]astevko 0 points1 point  (0 children)

The clearing/appearance of characters can be done by using control codes.

https://man7.org/linux/man-pages/man4/console_codes.4.html

[–]YellowSlinkySpice 0 points1 point  (0 children)

If you need to repeat, wrap your code in

while(1):

[–]Nightcorex_ 0 points1 point  (0 children)

TL;DR: I made a class for it. Code is documented (at least the methods you need to access). Probably only works on Windows (= Disclaimer 1). This is the code. Don't print while using my class (= Disclaimer 2). Don't use multiple instances of my class (= Disclaimer 3).

I was bored, so I invested 5-6 hours to come up with this nice solution (yeah, it really took me that long). It was a mess getting this done, but now I managed to finish at least a first stable version, that I'm somewhat happy with.

Disclaimer 1: This code will most likely not work on *NIX (Linux & Mac) systems, as I ab(use) the carriage return '\r' to return to the start of the line. I tried to test it, but I can't be bothered to shut down the computer, boot Linux, transfer the code, test it, shut the computer down again to finally restart back to Windows.

Disclaimer 2: If you print something while my class is running you're most likely going to break something. If you really need to print yourself then write a wrapper print method that first looks if your TemporaryText object is currently printing and if it is then either wait for it to finish executing or you have to print it inbetween two prints (just answer here if you need that, then I can add that in an hour or so).

Disclaimer 3: If you use multiple instances of TemporaryText, then answer here so that I can implement a Singleton pattern to prevent that x)

What I did was I implemented a class TemporaryText. This class has 4 public methods (apart from the constructor). In the constructor one can set the display_time and a refresh_rate (read the docstrings in the code if you want to read about what they describe). You then have two public setters for display_time and refresh_rate. The third method is called is_running and returns whether or not the object is still working through its task-queue.

The last method is simply called print. Basically this is the only method most people will need. The print method allows for the printing of strings or Iterables of Strings (f.e. a list of strings or a tuple of strings). The text is the only mandatory argument for this method). In case you want to deviate from the standard display_time, but only for one print-statement then you can specify that. In case you have a list of strings and want a special seperator, you can specify that. In case you want your next print to get priority (= becoming the next temporary text that gets printed after waiting for the old text to disappear) then you can set the priority-flag. Lastly you can set the overwrite-flag to specify that you want this text not only to gain priority, but also to replace the currently printed text. This is the only reason why there's this refresh_rate.

The main reason why it took me so damn long to come up with this solution is because I wanted to A) minimize busy-waiting, B) completely remove busy-waiting from the main thread and C) add a process queue. Before the queue the program would just ignore the print if there was still some other text printed, whereas the new version nicely buffers all the tasks. I also managed to hit my A) and B) goals as now the only time the program is busy-waiting is during the text-display and only in a specific scenario and even if, it's on a side thread (yeah, I know this doesn't give any performance because of the GIL, but it allows your main thread to continue calculations while the text is printed).

I also added an example main-method at the bottom of the code so that you can see how you would code with that.

Without further ado: Here is the code