Get contents of a Tkinter Entry widget
Asked Answered
B

2

14

I am creating an application and I want to use the entered values in the GUI Entry widget.

How do I get the entered input from a Tkinter Entry widget?

root = Tk()
...
entry = Entry(root)
entry.pack()

root.mainloop()
Bufflehead answered 22/3, 2012 at 0:59 Comment(3)
possible duplicate of Tkinter: get Entry content with get()Kinfolk
@Rinzler ? This question is older then that one. And why mark as a duplicate right now?Telecast
@Telecast The age of questions does not matter, per official policy when closing duplicates. That said, this is not a duplicate, although the two questions have an important relationship. This one is a how-to about getting the information generally; the other is a debugging question about timing and managing events (i.e., when to get the information).Lower
A
30

You need to do two things: keep a reference to the widget, and then use the get() method to get the string.

Here's an example:

self.entry = Entry(...)
...
print("the text is", self.entry.get())
Allocution answered 5/4, 2012 at 10:51 Comment(0)
C
8

First declare a variable of required type. For example an integer:

var = IntVar()

Then:

entry = Entry(root, textvariable=var)

entry.pack()

user_input = var.get()

root.mainloop()

Hope this helps.

Capricorn answered 30/3, 2018 at 14:13 Comment(1)
Does this add anything to the current answers @aleksandarkrumovDormer

© 2022 - 2024 — McMap. All rights reserved.