If i had a timer that updates a variable each second, how would i make sure the old value is cleared:
import time
print("hello", end="\r")
time.sleep(1)
print("hello, again...",end="\r")
You will need to run the code from somewhere that is not idle as it is not a real tty, using clear
or cls
is also going to fail in idle. You could possibly use the curses lib as mentioned in a comment but it will certainly not be trivial to implement, if you wanted to reverse the output like the lines in your question you could redirect stdout to an io.StringIO object and reverse the lines:
from contextlib import redirect_stdout
from io import StringIO
f = StringIO()
with redirect_stdout(f):
print("hello")
print("hello, again...")
f.seek(0)
print("".join(f.readlines()[::-1]))
Which in idle will output:
hello, again...
hello
If I were you I would ditch idle, what you are seeing is one of many limitations you may encounter when using idle.
If you really do want to stay using idle, you should download idlex which has some extensions to make idle work more like a terminal