User input with a default value that can be modified

half.italian at gmail.com half.italian at gmail.com
Mon May 28 15:47:28 EDT 2007


On May 28, 11:52 am, "Etienne Hilson" <etienne.hil... at gmail.com>
wrote:
> Hello the list :-)
>
> I do a little program that permit the user to manage list of sentences.
> This program runs into a linux shell.
> The user can add, modify and delete the sentences.
>
> What I want to do is :
>
> When the user want to modify one sentence, I would like to do this :
>
> Modify your sentence : The_sentence_appear_here_followed_by_a_cursor
>
> And the user can go back with the cursor, like in the bash shell,
> delete, modify, and when pressing enter, the new value (or the same if
> not modified) is put in my variable.
>
> Of course, the first think I did as a newbie was :
>
> new_sentence = raw_input("Modify your sentence : "old_sentence)
>
> But OF COURSE, stupid am I, the user cannot put the cursor back into
> the old sentence !
>
> I think about playing with some sophisticated keyboard exercise where
> I could program a new input command with a value already appearing as
> answer, but I am pretty sure that it exists already.
>
> What do you think about it ?
>
> Actually, it is quite difficult to find anything on it, because the
> keywords are not very obvious (input, default answer, ...)
>
> Thank you for your help.
>
> Etienne
> --
> (\__/)
> (='.'=)          Ceci est un petit lapin. Copiez/collez-le dans
> (")_(")          votre signature pour l'aider à dominer le monde

Check into the readline module.  This is what I came up with.  A
second thread injects the text into the open readline instance.
Hopefully the experts will show the _right_ way to do it.

import readline, threading
import time

class write(threading.Thread):
		def __init__ (self, s):
			threading.Thread.__init__(self)
			self.s = s

		def run(self):
			time.sleep(.01)
			readline.insert_text(self.s)
			readline.redisplay()

write("Edit this sentence").start()
s = raw_input("prompt:")

print s

~Sean




More information about the Python-list mailing list