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

22,130

Solution 1

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...).

Solution 2

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.

Share:
22,130
Ds Arjun
Author by

Ds Arjun

Finished B.Tech and working as a Software Engineer.

Updated on July 26, 2020

Comments

  • Ds Arjun
    Ds Arjun almost 4 years

    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.

  • Russell Smith
    Russell Smith almost 9 years
    This won't work if they use the tab key to move focus to the entrt. It would be better to bind on <FocusIn>
  • Olivier Samson
    Olivier Samson almost 6 years
    To get the entry text color, you can use cget as such: entry.cget('fg')
  • greendino
    greendino about 4 years
    if entry.get() == ''" #this condition is dangerous, it should've been like this =>if len(entry.get()) - entry.get().count(' ') < 1 #aware of space only character