How to insert a temporary text in a tkinter Entry widget?

Ds Arjun picture Ds Arjun · May 27, 2015 · Viewed 19.2k times · Source

How to insert a temporary text in a tkinter Entry widget?

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.

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.

Answer

ZeroFunter picture ZeroFunter · Sep 24, 2016

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.

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.

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. 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 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'). I guess, after 2 years, I finally figure out how to do this.

Therefore, we can now change the line if entry.get() == 'Enter your user name...': to 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...).