简体   繁体   English

如何在 tkinter ENTRY 中突出显示预先输入的文本,以便用户只需输入(无需选择、删除)

[英]How can I highlight the pre-entered text in tkinter ENTRY so User can just type over ( without selecting, deleting)

noob here trying to create a simple entry with tkinter, so the pre-entered text "Enter your name here" is highlighted. noob 在这里尝试使用 tkinter 创建一个简单的条目,因此突出显示了预先输入的文本“在此处输入您的姓名”。 The user can then just types their name ( or hit delete to remove the content...) is there any simple argument / method to use with ENTRY?然后用户可以输入他们的名字(或者点击删除来删除内容......)是否有任何简单的参数/方法可以与 ENTRY 一起使用?

from tkinter import *
root = Tk()

lbl1 = Label ( root, text = "Let's Play together")
lbl2 = Label ( root, text = "What's your Name ? ")
lbl1.grid( row=0, column=0)
lbl2.grid (row=1, column=0)


nameplayer = Entry(root, width=25, )
nameplayer.insert(0,"Enter your name here ")
nameplayer.grid(row=2, column=0)
root.mainloop()

thanks !!谢谢 !!

Auto-Select text can be done with Entry.selection_range自动选择文本可以通过Entry.selection_range来完成

class Focus(event):
    nameplayer.selection_range(0, END)

nameplayer.bind("<FocusIn>", Focus)

This will select all text when the user clicks in the Entry Widget.当用户在 Entry Widget 中单击时,这将 select 所有文本。 Hope this works:)希望这有效:)

I suggest you write a function and create a place holder for your Entry .我建议你写一个 function 并为你的Entry创建一个占位符。 Then the user just clicks and writes his/her name.然后用户只需点击并写下他/她的名字。 no need to delete the pre-text and then write name.无需删除前置文本,然后写入名称。
use this function click() for defining a hint or actually a place holder:使用此 function click()来定义提示或实际占位符:

from tkinter import *
root = Tk()

lbl1 = Label(root, text = "Let's Play together")
lbl2 = Label(root, text = "What's your Name?")
lbl1.grid(row=0, column=0)
lbl2.grid(row=1, column=0)

nameplayer = Entry(root, width=25, )
nameplayer.insert(0, 'Enter your name here')
nameplayer.grid(row=2, column=0)
def click(event):
    nameplayer.configure(state=NORMAL)
    nameplayer.delete(0, END)
    nameplayer.unbind('<Button-1>', clicked)
    
clicked = nameplayer.bind('<Button-1>', click)
root.mainloop()

LAMBDA LAMBDA

nameplayer.bind('<FocusIn>', lambda x: nameplayer.selection_range(0, END))

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

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