看起来你可能对何时运行命令感到困惑。在你的示例中,你是get
在GUI有机会在屏幕上显示之前调用该方法的(在调用之后发生)mainloop
。
尝试添加一个调用该get方法的按钮。如果你将应用程序编写为类,这会容易得多。例如:
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())
app = SampleApp()
app.mainloop()
运行程序,输入条目小部件,然后单击按钮。