Why is Tkinter Entry's get function returning nothing?
Asked Answered
D

7

28

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?

Decanal answered 23/5, 2012 at 20:12 Comment(1)
In 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.Heisler
H
50

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.

Heisler answered 23/5, 2012 at 23:0 Comment(3)
Ah, 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!Decanal
You might need to add self as a parameter while calling the superclass init: tk.Tk.__init__(self). Otherwise, very useful example!Sheri
And, er, you misspelt app in the last line. <_<"Sheri
A
10

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.

Adamandeve answered 23/5, 2012 at 20:17 Comment(1)
A StringVar isn't necessary, strictly speaking. They are handy, but for this question they are completely superfluous.Heisler
M
2

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()
Mephistopheles answered 12/9, 2016 at 15:20 Comment(0)
B
1

*

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.

Bouldin answered 2/3, 2016 at 21:15 Comment(0)
S
1

you need to put a textvariable in it, so you can use set() and get() method :

var=StringVar()
x= Entry (root,textvariable=var)
Shipway answered 15/10, 2019 at 9:13 Comment(0)
P
1

Most of the answers I found only showed how to do it with tkinter as tk. This was a problem for me as my program was 300 lines long with tons of other labels and buttons, and I would have had to change a lot of it.

Here's a way to do it without importing tkinter as tk or using StringVars. I modified the original mini program by:

  • making it a class
  • adding a button and an extra method.

This program opens up a tkinter window with an entry box and an "Enter" button. Clicking the Enter button prints whatever is in the entry box.

from tkinter import *

class mini():

    def __init__(self):
    
        master = Tk()

        Label(master, text="Input: ").grid(row=0, sticky=W)
        Button(master, text='Enter', command=self.get_content).grid(row=1)
        self.entry = Entry(master)
        self.entry.grid(row=0, column=1)
        master.mainloop()

    def get_content(self):
        content = self.entry.get()
        print(content)  


m = mini()
Palaeography answered 20/8, 2022 at 19:19 Comment(0)
R
0

Here's what worked for me:

from tkinter import *

master = Tk()

Label(master, text="Input: ").grid(row=0, sticky=W)

entry = Entry(master)
entry.grid(row=0, column=1)


def click(entry):
    content = entry.get()
    print(content)  
add_but = Button(text="Add", width=36, command=lambda: click(entry))
add_but.grid(row=1, column=1)

mainloop()
Related answered 6/7 at 9:22 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Giliane

© 2022 - 2024 — McMap. All rights reserved.