Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python 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.

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

(also asked on thecodingforums, freecodecamp, python-forum.io)
 
You could try outputting the right number of backspace characters to get the next Enter to appear in the exact same location as the previous. Not sure if it will work though. Regardless, I think it's a bad idea. Put yourself in the role of the user, they've typed something in and your program just wipes it out and asks again. Visually, it will look like the program is broken. IMO it is good practice to give some feedback as to why the input was declined. That would typically be done on a new line, and you then output another prompt. Or is that information displayed elsewhere ? Why don't you want a new line for every prompt ?
 
@cbreemer Consider a textbox on a website. If you enter wrong info, they don't display another textbox underneath, and then another and another, until you get it right. They give you an alert, yes, but they keep re-using the same textbox.
 
@cbreemer Consider a textbox on a website. If you enter wrong info, they don't display another textbox underneath, and then another and another, until you get it right. They give you an alert, yes, but they keep re-using the same textbox.
Of course. But since you mentioned "terminal" I was thinking of old-fashioned line-based tty input. Now it remains unclear (at least to me) what exactly you are doing, what exactly you want, and why.
 
@cbreemer Just doing what i described.

curses/ncurses can be used to control a terminal in free-form ways, without being restricted to basic line-entry. ncurses has been around since 1982. Is that old fashioned?

curses is available for python, but I'm hoping there's a way to do the simple thing i described without a lib.

 
python is designed to go line by line, making things easier for us to bug test or see the output but in some instances like a loading bar etc keeping things on the same line works, have you ever heard of "prompt_toolkit" it does just this. It stores the input in a history function.


Python:
from prompt_toolkit import prompt
from prompt_toolkit.history import InMemoryHistory
 
history = InMemoryHistory()

entry = ''
while entry not in menu:
    entry = prompt('Enter: ', history=history)
 
 
print(f"You entered a valid option: {entry}")

I haven't tested this but it should work, but it will only work in CMD
 
have you ever heard of "prompt_toolkit" it does just this. It stores the input in a history function.

I haven't tested this but it should work, but it will only work in CMD
What do you mean, only in CMD? You mean, it won't work in vscode terminal?

I'm not getting desired result in vscode terminal or separate command prompt.
Bash:
Enter: 8
Enter: 6
Enter: 5
Enter: 3
You entered a valid option: 3
 
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 worms.py 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.
 
Back
Top Bottom