1

Is it possible to create scrolling text in the Python command line by repeatedly updating the same line of text with small time.sleep() delays?

I believe that the \b (backspace) character can effectively move the cursor backward over text already written. I thought that combining this with end="" parameter in Python 3 might allow subsequent print commands to update a previous print command. With careful tracking of the length of the text and insertion of backspace and other characters it should be possible to create static lines of text that animate in place.

Unfortunately none of this works and even the \b character seems to do nothing:

word = input("Type something-> ")
print(word+"\b\b\bHello", end="")
print("New text")

Anyone got any ideas?

Many thanks, Kw

Kwangle
  • 349
  • 2
  • 15
  • 2
    Take a look at the ncurses module if you are working on a unixish OS – deets May 20 '18 at 11:33
  • Another approach would to simple clear any text in the command line between updates of the scrolling text, but again I'm not sure this is possible. – Kwangle May 20 '18 at 11:35
  • You may be able to use VT100 Terminal Control Escape Sequences, eg http://stackoverflow.com/a/37501797/4014959 – PM 2Ring May 20 '18 at 11:58

1 Answers1

0

Maybe you need carriage return, or \r. This takes you to the beginning of the line. It is the same effect as in a physical typewriter when you move your carriage to the beginning and overwrite whatever is there.

If you use this:

print("Hello", end=" ")
print("world")

The output will be:

Hello World

But if you use:

print("Hello", end="\r")
print("world")

The output will be only:

world