简体   繁体   中英

Display previous input on keyup from user, in Python?

I have a simple Python program which uses a read-eval-print loop to read user input via raw_input and then print things to the screen. I would like to keep a history of previous inputs and cycle through them when the user presses keyup or keydown, similar to the Python interpreter or to the bash shell. How can I do this in Python?

Someone asked for sample code:

while True:
    user_input = raw_input()
    print user_input + " this many hats!!!"

I'd like to make it so a keyup puts the last line of input on the command line. The first answer given, use the readline module, is likely the best.

Try using the readline module. If your platform supports readline, simply importing the module should make its functionality available via the raw_input prompt.

For standard input function also works. No configuration needed. Just import readline .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM