johnnycongo
johnnycongo

Reputation: 9

tkinter entry() not returning string

I have several entry boxes made with tk: Entry()

I need to put what the user enters into a variable, which I do as such (as I have found online):

window = Tk()

#make entry and turn it into stringvar
entry1string = tk.StringVar
entry_1 = Entry(window,textvariable=entry1string)

#retrieve it into a variable
retrieved = entry1string.get()

This gives the following error:

AttributeError: 'str' object has no attribute 'get'

How do I get the string/value entered into the entry box by the user into a variable? The code seems to be just how every example I've found is, I don't see why it's giving me that error.

Upvotes: 0

Views: 869

Answers (1)

Kartikeya
Kartikeya

Reputation: 838

Refer here to know about Entry widgets in Tkinter.

What you can do is create a button, on which upon clicking, the data entered in the Entry box will be retrieved.

...
entry1string = tk.StringVar()
entry_1 = Entry(window,textvariable=entry1string).pack()

def retrieveData():
    #retrieve it into a variable
    retrieved = entry1string.get()
    #print the data
    print(retrieved)
    #Or output the data on the window in a Label :
    Label(window, text=retrieved).pack()

button1 = Button(window, text="PRINT", command=retrieveData).pack()

There needs to be a function to call when the button is clicked. you can print the data in the command line or even output on the GUI window, that's your choice.

Read the documentation to know more.

Upvotes: 1

Related Questions