Get contents of a Tkinter Entry widget

自作多情 提交于 2019-11-26 22:56:44
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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!