Why is Tkinter Entry's get function returning nothing?
Solution 1
It looks like you may be confused as to when commands are run. In your example, you are calling the get
method before the GUI has a chance to be displayed on the screen (which happens after you call mainloop
.
Try adding a button that calls the get
method. This is much easier if you write your application as a class. For example:
import tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
print(self.entry.get())
app = SampleApp()
app.mainloop()
Run the program, type into the entry widget, then click on the button.
Solution 2
You could also use a StringVar
variable, even if it's not strictly necessary:
v = StringVar()
e = Entry(master, textvariable=v)
e.pack()
v.set("a default value")
s = v.get()
For more information, see this page on effbot.org.
Solution 3
A simple example without classes:
from tkinter import *
master = Tk()
# Create this method before you create the entry
def return_entry(en):
"""Gets and prints the content of the entry"""
content = entry.get()
print(content)
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
# Connect the entry with the return button
entry.bind('<Return>', return_entry)
mainloop()
Solution 4
*
master = Tk()
entryb1 = StringVar
Label(master, text="Input: ").grid(row=0, sticky=W)
Entry(master, textvariable=entryb1).grid(row=1, column=1)
b1 = Button(master, text="continue", command=print_content)
b1.grid(row=2, column=1)
def print_content():
global entryb1
content = entryb1.get()
print(content)
master.mainloop()
What you did wrong was not put it inside a Define function then you hadn't used the .get
function with the textvariable you had set.
Related videos on Youtube
Comments
-
CodingCat almost 4 years
I'm trying to use an
Entry
field to get manual input, and then work with that data.All sources I've found claim I should use the
get()
function, but I haven't found a simple working mini example yet, and I can't get it to work.I hope someone can tel me what I'm doing wrong. Here's a mini file:
from tkinter import * master = Tk() Label(master, text="Input: ").grid(row=0, sticky=W) entry = Entry(master) entry.grid(row=0, column=1) content = entry.get() print(content) # does not work mainloop()
This gives me an
Entry
field I can type in, but I can't do anything with the data once it's typed in.I suspect my code doesn't work because initially,
entry
is empty. But then how do I access input data once it has been typed in?-
Russell Smith almost 12 yearsIn your example, what exactly are you expecting? You haven't given the entry widget any text before you call
get
so of course it returns an empty string.
-
-
Russell Smith almost 12 yearsA
StringVar
isn't necessary, strictly speaking. They are handy, but for this question they are completely superfluous. -
CodingCat almost 12 yearsAh, I see. I'm not really firm on classes yet (still very much a beginner at programming in general), but I see the problem. I'll just make an "Analyze!" button and put the get()-function in there, that should work. Thank you!
-
Deep-B about 10 yearsYou might need to add self as a parameter while calling the superclass init: tk.Tk.__init__(self). Otherwise, very useful example!
-
Deep-B about 10 yearsAnd, er, you misspelt app in the last line. <_<"