Search code examples
python-3.xtkintertkinter-entry

Unexpected Output on storing the value of the text in Entry widget to a variable


I am trying to store the value in the Entry widget to a variable and then print it. But instead of getting the value, I'm simply getting .!entry as the output everytime. I have used the following code:

from tkinter import *

def printEntry():
    value = myEntry.get()
    print(myEntry)

root=Tk()

myLabel = Label(root,text="Enter Star: ")
myEntry = Entry(root)

myLabel.pack()
myEntry.pack()

plotButton= Button(root,text="plot", command=printEntry)
plotButton.pack()

root.mainloop()

And getting the following output everytime even if I change the input.

.!entry

I am unable to find why this is happening. Please help me and let me know if any further information is required.


Solution

  • You are printing (the repr of) the Entry widget instead of the value: Try:

    def printEntry():
        value = myEntry.get()
        print(value)