Why is Tkinter Entry's get function returning nothing? Why is Tkinter Entry's get function returning nothing? tkinter tkinter

Why is Tkinter Entry's get function returning nothing?


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 tkclass 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.


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.


A simple example without classes:

from tkinter import *    master = Tk()# Create this method before you create the entrydef 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 buttonentry.bind('<Return>', return_entry) mainloop()