Sim*_*tty 1 python tkinter tkinter-entry
我想知道如何在此处附加的代码中定义的输入框中添加某种背景文本:
该框可能会显示“示例:Joe Bloggs”,但是当用户在框内单击时会变灰,然后将其删除?希望这不是太棘手。
        # ************ Retrieve user's Full name ************
    tk.Label(self, text='First and last name:').grid(sticky='e') # Label
    self.full_name_entry = tk.Entry(self, bg='white', width=30) # Entry box
    self.full_name_entry.grid(row=1, column=1, pady=15, columnspan=2) # Entry box placement
Reb*_*que 10
你需要:
tk.Entry.insert将默认文本添加到Entry小部件这是代码的样子
import tkinter as tk
def handle_focus_in(_):
    full_name_entry.delete(0, tk.END)
    full_name_entry.config(fg='black')
def handle_focus_out(_):
    full_name_entry.delete(0, tk.END)
    full_name_entry.config(fg='grey')
    full_name_entry.insert(0, "Example: Joe Bloggs")
def handle_enter(txt):
    print(full_name_entry.get())
    handle_focus_out('dummy')
root = tk.Tk()
label = tk.Label(root, text='First and last name:')
label.grid(sticky='e')
full_name_entry = tk.Entry(root, bg='white', width=30, fg='grey')
full_name_entry.grid(row=1, column=1, pady=15, columnspan=2)
full_name_entry.insert(0, "Example: Joe Bloggs")
full_name_entry.bind("<FocusIn>", handle_focus_in)
full_name_entry.bind("<FocusOut>", handle_focus_out)
full_name_entry.bind("<Return>", handle_enter)
root.mainloop()
这是打开窗口时的样子:
将焦点放在Entry小部件上后,示例文本被删除,字体颜色变为黑色;填写完entry后,aspect是:
| 归档时间: | 
 | 
| 查看次数: | 4743 次 | 
| 最近记录: |