user1417933
user1417933

Reputation: 835

How do I retrieve the value of an Entry widget in Tkinter?

I'm using the following code to try and get the content of the entry/input box to be printed when I click the submit button, however, nothing seems to happen.

def submit_answer(response):
    print(response)


def get_answer():
    root = Tkinter.Tk()

    contentFrame = Tkinter.Frame(root)

    entryWidget = Tkinter.Entry(contentFrame)
    entryWidget['width'] = 50
    entryWidget.pack()

    contentFrame.pack()

    button = Tkinter.Button(root, text='Submit', command=submit_answer(entryWidget.get()))
    button.pack()

    root.mainloop()

Can anyone point out what I'm doing wrong here?

Upvotes: 0

Views: 145

Answers (1)

Tim
Tim

Reputation: 12174

ValekHalfHeart's comment is correct.

When you do command=submit_answer(entryWidget.get())) it executes submit(entryWidget.get()) once, and then uses the result of that function (probably None) as the click event. This is not what you want.

When you wrap it in a lambda, the function is executed each time you click. For it to work, you should have command=lambda:submit_answer(entryWidget.get())

Upvotes: 1

Related Questions