noga
noga

Reputation: 43

How can I edit an inputed text in python

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 ?

Upvotes: 3

Views: 3242

Answers (4)

You can do this:

import os
os.system('clear')

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

Upvotes: -2

noga
noga

Reputation: 43

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

Upvotes: 0

Ofer Arial
Ofer Arial

Reputation: 1139

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()

Upvotes: 2

Davis Herring
Davis Herring

Reputation: 40013

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.

Upvotes: 0

Related Questions