简体   繁体   English

如何在tkinter Entry小部件中插入临时文本?

[英]How to insert a temporary text in a tkinter Entry widget?

How to insert a temporary text in a tkinter Entry widget? 如何在tkinter Entry小部件中插入临时文本?

For example, I have a Label User and next to it I have an Entry widget which should have some text "Enter your username..." at the beginning of the application, and while putting cursor on the Entry widget, it should remove "Enter your username..." and allow user to enter data. 例如,我有一个标签User ,旁边有一个Entry小部件,它应该在应用程序的开头有一些文本"Enter your username..." ,并且当把光标放在Entry小部件上时,它应该删除"Enter your username..."并允许用户输入数据。

This is my current code: 这是我目前的代码:

import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="User:")
label.pack()
entry = tk.Entry(root, bd=1, show="Enter your user name...")
entry.pack()

root.mainloop()

How can I do that? 我怎样才能做到这一点?

I didn't find any option or method to delete data on putting cursor on the Entry widget. 我没有找到任何选项或方法来删除将游标放在Entry小部件上的数据。

I would like to add something to the responses that has not been said. 我想在没有说过的回复中添加一些内容。 As 9jera said, we can make the Entry widget clear only when it sees the default text instead of the "firstclick" method used by Leo Tenenbaum . 正如9jera所说,我们只有在看到默认文本而不是Leo Tenenbaum使用的“firstclick”方法时才能使Entry小部件清晰。

We could also add a second function to refill the Entry widget if the user has turned the focus to another widget without typing in anything. 如果用户将焦点转移到另一个小部件而不键入任何内容,我们还可以添加第二个函数来重新填充Entry小部件。

This can be achieved as follows: 这可以通过以下方式实现:

import Tkinter as tk


def on_entry_click(event):
    """function that gets called whenever entry is clicked"""
    if entry.get() == 'Enter your user name...':
       entry.delete(0, "end") # delete all the text in the entry
       entry.insert(0, '') #Insert blank for user input
       entry.config(fg = 'black')
def on_focusout(event):
    if entry.get() == '':
        entry.insert(0, 'Enter your username...')
        entry.config(fg = 'grey')


root = tk.Tk()

label = tk.Label(root, text="User: ")
label.pack(side="left")

entry = tk.Entry(root, bd=1)
entry.insert(0, 'Enter your user name...')
entry.bind('<FocusIn>', on_entry_click)
entry.bind('<FocusOut>', on_focusout)
entry.config(fg = 'grey')
entry.pack(side="left")

root.mainloop()

Finally, I also added grey color to the default text and black to the one written by the user, just for anyone that was wondering about that. 最后,我还为默认文本添加了灰色,并将黑色添加到用户编写的颜色,只是对于那些想知道这一点的人。 The only issue I see here is, if the user actually manually types in there Enter your user name... , focuses out and in again, the text will be deleted even though it was written by the user himself. 我在这里看到的唯一问题是,如果用户实际手动Enter your user name...那里Enter your user name... ,重点关注,即使文本是由用户自己编写的,文本也会被删除。 One solution I thought of is to change the if statement so it gets the color instead of the default text before deleting anything. 我想到的一个解决方案是更改if语句,以便在删除任何内容之前获取颜色而不是默认文本。 If the color is grey, it can go ahead and delete it. 如果颜色为灰色,则可以继续并删除它。 Else, it will not. 否则,它不会。 Yet, I have not found a way to get the text color. 然而,我还没有找到获得文本颜色的方法。 If anyone knows about this, please let me know! 如果有人知道这件事,请告诉我!

EDIT: Apparently, as Olivier Samson has pointed out, it is possible to get the color of the entry by using entry.cget('fg') . 编辑:显然,正如Olivier Samson指出的那样,可以通过使用entry.cget('fg')获得条目的颜色。 I guess, after 2 years, I finally figure out how to do this. 我猜,2年后,我终于弄清楚如何做到这一点。

Therefore, we can now change the line if entry.get() == 'Enter your user name...': to if entry.cget('fg') == 'grey': . 因此,我们现在可以更改行if entry.get() == 'Enter your user name...': if entry.cget('fg') == 'grey': That way, whenever the entry is clicked for the first time and anything is typed inside, the color is changed to black, so next time the user focuses in, it will not delete any text (even if that text is Enter your user name... ). 这样,无论何时第一次点击条目并在内部输入任何内容,颜色都会变为黑色,因此下次用户关注时,它将不会删除任何文本(即使该文本是Enter your user name... )。

I think this should work: 我认为这应该有效:

import Tkinter as tk


firstclick = True

def on_entry_click(event):
    """function that gets called whenever entry1 is clicked"""        
    global firstclick

    if firstclick: # if this is the first time they clicked it
        firstclick = False
        entry.delete(0, "end") # delete all the text in the entry


root = tk.Tk()

label = tk.Label(root, text="User: ")
label.pack(side="left")

entry = tk.Entry(root, bd=1)
entry.insert(0, 'Enter your user name...')
entry.bind('<FocusIn>', on_entry_click)
entry.pack(side="left")

root.mainloop()

This will delete Enter your user name... when the user clicks on the Entry entry . 这将删除当用户单击Entry entryEnter your user name...

From Leo's Code entry1.delete should be changed to entry.delete . 从Leo的代码entry1.delete应更改为entry.delete

Also the Entry widget from his code clears only on first click, but you can make the Entry widget clear only when it is set to the default text. 此外,他的代码中的Entry小部件仅在第一次单击时清除,但只有在将Entry小部件设置为默认文本时才能使其清除。 This could be helpful if your code has to repeat itself. 如果您的代码必须重复,这可能会有所帮助。

Try this: 尝试这个:

import Tkinter as tk


def on_entry_click(event):
    """function that gets called whenever entry is clicked"""
    if entry.get() == 'Enter your user name...':
       entry.delete(0, "end") # delete all the text in the entry
       entry.insert(0, '') #Insert blank for user input


root = tk.Tk()

label = tk.Label(root, text="User: ")
label.pack(side="left")

entry = tk.Entry(root, bd=1)
entry.insert(0, 'Enter your user name...')
entry.bind('<FocusIn>', on_entry_click)
entry.pack(side="left")

root.mainloop()

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

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