简体   繁体   English

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

[英]How to set default text for a Tkinter Entry widget

How do I set the default text for a Tkinter Entry widget in the constructor?如何在构造函数中为 Tkinter Entry 小部件设置默认文本? I checked the documentation, but I do not see a something like a "string=" option to set in the constructor?我检查了文档,但在构造函数中没有看到类似"string="选项的内容?

There is a similar answer out there for using tables and lists, but this is for a simple Entry widget.对于使用表格和列表,有一个类似的答案,但这是一个简单的 Entry 小部件。

Use Entry.insert .使用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()

Or use textvariable option:或者使用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()

For me,对我来说,

Entry.insert(END, 'your text')

didn't worked.没有用。

I used Entry.insert(-1, 'your text') .我使用了Entry.insert(-1, 'your text')

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM