Amen
Amen

Reputation: 75

tkinter TextVar.get() returns '.!entry' not a string

I have the below code which for some reason when i call .get() to the textvariable on tkinter.Entry, I get '.!entry' instead of the string I am expecting. How can I fix this?

def getter():
    final = e1str_var.get()

e1str_var = StringVar()
e1 = Entry(root, textvar=e1str_var)

e1.grid(row=4, column=0)

print(getter())


Returns '.!entry'

Upvotes: 0

Views: 166

Answers (2)

Ritesh Khanna
Ritesh Khanna

Reputation: 146

You can only get the string in a text box when perfomed some event like button click or binding label or some other widget with an event. Here I have used a button for using get().

from tkinter import *

root=Tk()

def getter():
    final = e1str_var.get()
    print(final)

e1str_var = StringVar()
e1 = Entry(root, textvar=e1str_var)
b1=Button(root,text="Click",command=getter)

e1.grid(row=4, column=0)
b1.grid(row=5,column=0)

root.mainloop()

Upvotes: 1

EXODIA
EXODIA

Reputation: 958

Your question seems unclear, you will be able to use get() of an Entry field on a certain action like a button click.

In the below code I made a button as well and when you click this button you will get what is written inside the textbox as your output.

from tkinter import *
def helloCallBack():
    print(E1.get())

top = Tk()
L1 = Label(top, text="User Name")
L2 = Button(top, text="Click",command = helloCallBack)
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = RIGHT)
L2.pack(side = BOTTOM)
print(E1.get())
top.mainloop()

Upvotes: 1

Related Questions