27

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
CodingCat
  • 4,999
  • 10
  • 37
  • 59
  • 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. – Bryan Oakley May 23 '12 at 22:51

6 Answers6

49

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.

nbro
  • 15,395
  • 32
  • 113
  • 196
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 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! – CodingCat May 24 '12 at 07:21
  • 1
    You might need to add self as a parameter while calling the superclass init: tk.Tk.__init__(self). Otherwise, very useful example! – Deep-B Mar 22 '14 at 23:56
  • And, er, you misspelt app in the last line. <_<" – Deep-B Mar 23 '14 at 00:41
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.

nbro
  • 15,395
  • 32
  • 113
  • 196
zortacon
  • 617
  • 5
  • 15
  • 1
    A `StringVar` isn't necessary, strictly speaking. They are handy, but for this question they are completely superfluous. – Bryan Oakley May 23 '12 at 23:03
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()
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.

rodude123
  • 294
  • 3
  • 19
1

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

var=StringVar()
x= Entry (root,textvariable=var)
Rajan Sharma
  • 2,211
  • 3
  • 21
  • 33
Mehdi
  • 27
  • 5
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()
Zearin
  • 1,474
  • 2
  • 17
  • 36
Nia
  • 13
  • 4