简体   繁体   中英

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?

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

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 .

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

You can do this:

import os
os.system('clear')

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

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