How do I get the Entry's value in tkinter?
Asked Answered
E

2

23

I'm trying to use Tkinter's Entry widget. I can't get it to do something very basic: return the entered value. Does anyone have any idea why such a simple script would not return anything? I've tried tons of combinations and looked at different ideas.

This script runs but does not print the entry:

from Tkinter import *
root = Tk()
E1 = Entry(root)
E1.pack()
entry = E1.get()
root.mainloop()
print "Entered text:", entry

Seems so simple.


Edit

In case anyone else comes across this problem and doesn't understand, here is what ended up working for me. I added a button to the entry window. The button's command closes the window and does the get() function:

from Tkinter import *
def close_window():
    global entry
    entry = E.get()
    root.destroy()

root = Tk()
E = tk.Entry(root)
E.pack(anchor = CENTER)
B = Button(root, text = "OK", command = close_window)
B.pack(anchor = S)
root.mainloop()

And that returned the desired value.

Exmoor answered 26/2, 2016 at 22:22 Comment(2)
get() is called before any text is entered. So entry is an empty string (change entry to repr(entry) in the last line and you'll see that). If you want to call get() when some text is entered (you probably do), you need to bind get() to an event.Gooch
And the print won't happen because you have an infinite loop before it (the root.mainloop()).Golub
M
19

Your first problem is that the call to get in entry = E1.get() happens even before your program starts, so clearly entry will point to some empty string.

Your eventual second problem is that the text would anyhow be printed only after the mainloop finishes, i.e. you close the tkinter application.

If you want to print the contents of your Entry widget while your program is running, you need to schedule a callback. For example, you can listen to the pressing of the <Return> key as follows

import Tkinter as tk


def on_change(e):
    print e.widget.get()

root = tk.Tk()

e = tk.Entry(root)
e.pack()    
# Calling on_change when you press the return key
e.bind("<Return>", on_change)  

root.mainloop()
Mascara answered 26/2, 2016 at 22:51 Comment(1)
Thanks, this helped me figure out the problem. It seems like a catch-22 that you can't put e.get() in the program because the string is empty, and putting it outside breaks the script because the program has been destroyed. Realizing that, I added a command including e.get() to close the entry window. I will edit my question with the full working code.Exmoor
F
7
from tkinter import *
import tkinter as tk
root =tk.Tk()
mystring =tk.StringVar(root)
def getvalue():
    print(mystring.get())
e1 = Entry(root,textvariable = mystring,width=100,fg="blue",bd=3,selectbackground='violet').pack()
button1 = tk.Button(root, 
                text='Submit', 
                fg='White', 
                bg= 'dark green',height = 1, width = 10,command=getvalue).pack()

root.mainloop()
Fluid answered 7/11, 2017 at 15:50 Comment(1)
Thanks a lot , was quite frustrated over it for a dayMatusow

© 2022 - 2024 — McMap. All rights reserved.