Receive Input on Same Terminal Line?

How to receive input on the same line, without clearing the rest of the terminal?

The following prints a new “Enter” line for every bad entry.

while not entry in menu:
    entry = input('Enter: ')

Try adding a series of backspaces like this:
input('\b\b\b\b\b\b\b\b\bEnter: ')

If that doesn’t work check out Curses

1 Like

backspaces don’t work, as the terminal has moved to a new line.
Curses might work!
Thx i’ll try it.
But would be cool if i can do this without external lib.

I just learned the following:

both blessed and urwid are wrappers around the ncurses library

blessed is a bit lower-level, best for making interactive games, like the example program or custom user interfaces like cursewords

urwid is a widget library, for making “windows” and dialog boxes, like in the mastadon client toot

the “curses” module that is built-in to python is a wrapper of the C library from the 1980’s. It is cumbersome to use, as the example of the blessed README describes at the bottom.

A blessed coder reports: I found Blessed to be very nice to use and code with. The documentation is very good and as such, it did not take too long to suss it out and code the UI.

1 Like

For this you can use the carriage return \r character.

menu = ["option1", "option2", "option3"]
entry = ''

while entry not in menu:
    entry = input('Enter: \r')

print("Selected option:", entry)