How can I edit an inputed text in python

Question:

I’m building a command line application with python, and I Need to be able to print something, then the user edit it and return it to me

I know that Input() doesn’t fit to my case because the user can’t modify the text you give him.
Is there a way to do it ?

Asked By: noga

||

Answers:

readline is the name of the system that lets the user edit terminal input (in a micro-Emacs environment by default). You can use readline.insert_text to supply text to edit. One supported way of doing this is to arrange to call it via set_pre_input_hook before calling input.

Answered By: Davis Herring

I think that one way to do this is by using a keyboard input listener – this way you can figure out exactly what the user is doing (all characters being pressed, as well as backspace) and print the edited text.

You can have a look at this answer, which gives examples of how to achieve this in linux/windows: Key Listeners in python?.

If you are looking for a User Interface (not proper command line), you can use Tkinter do display a text box in which the user can input it’s data.

An example (based on https://effbot.org/tkinterbook/entry.htm):

from Tkinter import *

master = Tk()

e = Entry(master, width=500)
e.pack()

e.focus_set()

def callback():
    print e.get()

b = Button(master, text="get", width=50, command=callback)
b.pack()

mainloop()
Answered By: Ofer Arial

So, as Anatoly said, I’m going to learn curse, because I will need other things curse can do. Thank you for everyone which has responded. I will also use read line.insert_text

Answered By: noga

You can do this:

import os
os.system('clear')

if you are using windows use os.system('cls') 🙂

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.