Python:有没有办法让Entry字段有默认值?

时间:2013-07-25 13:44:43

标签: python tkinter tkinter-entry

有没有办法让GUI中的文本框显示默认文本?

我希望文本框中包含“请设置所需文件的路径... ” 但是,当我运行它时 - 它是空白的......

我的代码如下:

  path=StringVar()
  textEntry=Entry(master,textvariable=path,text='Please set the path of the file you want...')
  textEntry.pack()

2 个答案:

答案 0 :(得分:1)

path.set("Please set the path of the file you want...")

-OR -

textEntry.insert(0, "Please set the path of the file you want...")

有用的文档:

答案 1 :(得分:1)

这应该展示如何做你想做的事情:

import Tkinter as tk

root = tk.Tk()

entry = tk.Entry(root, width=40)
entry.pack()
# Put text in the entrybox with the insert method.
# The 0 means "at the begining".
entry.insert(0, 'Please set the path of the file you want...')

text = tk.Text(root, width=45, height=5)
text.pack()
# Textboxes also have an insert.
# However, since they have a height and a width, you need to
# put 0.0 to spcify the beginning.  That is basically the same as
# x=0, y=0.
text.insert(0.0, 'Please set the path of the file you want...')

root.mainloop()