如何为 Tkinter Entry 小部件设置默认文本

问题描述:

如何在构造函数中为 Tkinter Entry 小部件设置默认文本?我检查了文档,但我没有看到类似 "string=" 选项的内容要在构造函数中设置?

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?

对于使用表格和列表有一个类似的答案,但这是一个简单的条目小部件.

There is a similar answer out there for using tables and lists, but this is for a simple Entry widget.

使用 Entry.insert.例如:

Use Entry.insert. For example:

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

或者使用textvariable选项:

Or use textvariable option:

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