如何为 Tkinter Entry 小部件设置默认文本 [英] How to set default text for a Tkinter Entry widget
本文介绍了如何为 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()
这篇关于如何为 Tkinter Entry 小部件设置默认文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文