Python 如何为 Tkinter Entry 小部件设置默认文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20125967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set default text for a Tkinter Entry widget
提问by Big Al
How do I set the default text for a Tkinter Entry widget in the constructor? I checked the documentation, but I do not see a something like a "string="option to set in the constructor?
如何在构造函数中为 Tkinter Entry 小部件设置默认文本?我检查了文档,但我没有看到类似"string="在构造函数中设置的选项?
There is a similar answer out there for using tables and lists, but this is for a simple Entry widget.
对于使用表格和列表,有一个类似的答案,但这是一个简单的 Entry 小部件。
采纳答案by falsetru
Use Entry.insert. For example:
使用Entry.insert. 例如:
try:
from tkinter import * # Python 3.x
except Import Error:
from Tkinter import * # Python 2.x
root = Tk()
e = Entry(root)
e.insert(END, 'default text')
e.pack()
root.mainloop()
Or use textvariableoption:
或使用textvariable选项:
try:
from tkinter import * # Python 3.x
except Import Error:
from Tkinter import * # Python 2.x
root = Tk()
v = StringVar(root, value='default text')
e = Entry(root, textvariable=v)
e.pack()
root.mainloop()

