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()
Bryan Oakley
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())
Someones_Name
Here's an 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())
w = SampleApp()
w.mainloop()
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.
来源:https://stackoverflow.com/questions/9815063/get-contents-of-a-tkinter-entry-widget