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?
*
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
You could also use a
StringVar
variable, even if it's not strictly necessary:For more information, see this page on effbot.org.
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 callmainloop
.Try adding a button that calls the
get
method. This is much easier if you write your application as a class. For example:Run the program, type into the entry widget, then click on the button.
A simple example without classes: