2012-03-22 141 views
3

我正在创建应用程序,并且想要在GUI Entry小部件中使用输入的值。获取Tkinter Entry小部件的内容

如何从Tkinter Entry小部件获取输入的输入?

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

root.mainloop() 
+0

的可能重复[Tkinter的:获取入口内容使用get()](http://stackoverflow.com/questions/ 10727131/tkinter-get-entry-content-with-get) – nbro 2015-03-09 19:45:19

+0

@Rinzler?这个问题比那个更老。为什么现在要标记为重复的? – Zizouz212 2015-04-12 20:23:26

回答

10

你需要做两件事情:保持到窗口小部件的引用,然后使用get()方法得到的字符串。

下面是一个例子:

self.entry = Entry(...) 
... 
print("the text is", self.entry.get()) 
2

这里有一个例子:

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() 
+0

这个答案被Bryan Oakley从[this other one](http://stackoverflow.com/a/10729040/3924118)无耻复制。 – nbro 2016-08-19 21:27:56