2

In my script, I want to ask the user for input on the correct or incorrect spelling of a sentence (i) and allow the user to make corrections if necessary. I am running this simply in Jupyter Notebook on a Mac. (We do not know in advance which sentence contains errors and which do not.) This is quite straightforward to do. However, I want to give the user the sentence i as an editable input at the prompt. I have tried to achieve this by adding a variable to the 'input' line in the script, but that does not work. I cannot find a positive answer to this question. Perhaps somebody knows if it is possible or impossible?

Here is the script.

    i = "Today, John toak the bus to school."
    print(i)
    print(f"Is this sentence spelled correctly? No = 0, Yes = 1")
    choice = input("> ")
    if choice == "1":
        return i
    else choice == "0":
        spelling = input("> ", i) # this does not work. Is there a way?
        return spelling

Suppose the script gives the user the following line:

John wend to school by bus today.
Is this sentence spelled correctly? No = 0, Yes = 1

If the user selects 0 (=No), I want the sentence to already appear at the prompt so that the user can edit it (just change 'wend' to 'went' and hit 'enter') rather than having to type the entire sentence again (with the risk of new mistakes):

 |-----------------------------------------|
 | > John wend to school by bus today.     |
 |-----------------------------------------|

Does anyone know if this is possible and, if so, how?

twhale
  • 725
  • 2
  • 9
  • 25
  • Possible duplicate of [Show default value for editing on Python input possible?](https://stackoverflow.com/questions/2533120/show-default-value-for-editing-on-python-input-possible) – Nathan Apr 01 '18 at 15:01
  • What's wrong with just `spelling = input("> " + i)`? – EriktheRed Apr 01 '18 at 15:02
  • With spelling = input("> " + i) , the sentence is not editable for the user - at least not when I tried this. The user will have to input the entire question. I would like them to be able to just change a wrong word or grammatical error, and hit enter. – twhale Apr 01 '18 at 15:04
  • Oh yeah that is true. Didn't think that through... – EriktheRed Apr 01 '18 at 15:05
  • @Nathan: yes, according that post (2010) it is not possible. But maybe a solution has become available since then? – twhale Apr 01 '18 at 15:07

1 Answers1

0

We can do this in a Tkinter window. Which may not be what you are looking for, but is a solution using the standard library. Here is some example code that creates a window with a default string. You can edit the string. When the return key is pressed, the string in the window is read.

from tkinter import Tk, LEFT, BOTH, StringVar
from tkinter.ttk import Entry, Frame


class Example(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("Entry")
        self.pack(fill=BOTH, expand=1)
        self.contents = StringVar()
        # give the StringVar a default value
        self.contents.set('test')
        self.entry = Entry(self)
        self.entry.pack(side=LEFT, padx=15)
        self.entry["textvariable"] = self.contents
        self.entry.bind('<Key-Return>', self.on_changed)

    def on_changed(self, event):
        print('contents: {}'.format(self.contents.get()))
        return True


def main():
    root = Tk()
    ex = Example(root)
    root.geometry("250x100+300+300")
    root.mainloop()


if __name__ == '__main__':
    main()
Oppy
  • 2,662
  • 16
  • 22