Search code examples
pythonshellinputonkeyup

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.


Solution

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