2

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 ?

noga
  • 33
  • 7
  • 1
    You'd probably need to use a full UI like with Tkinter, or use a library like Curses. Text manipulation in plain consoles is fairly limited. – Carcigenicate Nov 02 '19 at 18:04
  • this is rather system issue, as I know console does not support that (unless I am wrong) so you need to run another app where user may enter data into input control – Anatoliy R Nov 02 '19 at 18:05
  • But take exemple of vim, the user can modify the text that is in vim – noga Nov 02 '19 at 18:16
  • vim is an application written in c, it constructs it's windows in c, so it's a bit irrelevant since you are writing in python. (You could use c in python, but I think it's a bit more complex than other available solutions) – Ofer Arial Nov 02 '19 at 18:41

4 Answers4

2

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()
Ofer Arial
  • 1,129
  • 1
  • 10
  • 25
0

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.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
0

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

noga
  • 33
  • 7
-2

You can do this:

import os
os.system('clear')

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