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

To insert a temporary text (placeholder) in a tkinter Entry widget, we can bind the <FocusIn> event to clear the placeholder text when the user clicks on the entry field. This creates a user-friendly interface with helpful hints.

Basic Implementation

Here's how to create a basic placeholder text that disappears when the user focuses on the entry widget ?

import tkinter as tk

# Create the main window
root = tk.Tk()
root.geometry("400x200")
root.title("Temporary Text Example")

def clear_placeholder(event):
    """Clear placeholder text when entry gets focus"""
    if entry.get() == "Enter your name here...":
        entry.delete(0, tk.END)
        entry.config(fg='black')

# Create entry widget
entry = tk.Entry(root, width=30, fg='grey')
entry.insert(0, "Enter your name here...")
entry.pack(pady=50)

# Bind the focus event
entry.bind("<FocusIn>", clear_placeholder)

root.mainloop()

Enhanced Implementation with Restore Functionality

This improved version restores the placeholder text if the user leaves the field empty ?

import tkinter as tk

class PlaceholderEntry(tk.Entry):
    def __init__(self, master=None, placeholder="PLACEHOLDER", color='grey'):
        super().__init__(master)
        
        self.placeholder = placeholder
        self.placeholder_color = color
        self.default_fg_color = self['fg']
        
        self.bind("<FocusIn>", self.focus_in)
        self.bind("<FocusOut>", self.focus_out)
        
        self.put_placeholder()

    def put_placeholder(self):
        self.insert(0, self.placeholder)
        self['fg'] = self.placeholder_color

    def focus_in(self, *args):
        if self['fg'] == self.placeholder_color:
            self.delete('0', 'end')
            self['fg'] = self.default_fg_color

    def focus_out(self, *args):
        if not self.get():
            self.put_placeholder()

# Create the main window
root = tk.Tk()
root.geometry("400x250")
root.title("Advanced Placeholder Example")

# Create multiple placeholder entries
tk.Label(root, text="User Registration Form", font=("Arial", 14)).pack(pady=10)

name_entry = PlaceholderEntry(root, "Enter your full name")
name_entry.pack(pady=5)

email_entry = PlaceholderEntry(root, "Enter your email address")
email_entry.pack(pady=5)

phone_entry = PlaceholderEntry(root, "Enter phone number")
phone_entry.pack(pady=5)

def get_values():
    print(f"Name: {name_entry.get()}")
    print(f"Email: {email_entry.get()}")
    print(f"Phone: {phone_entry.get()}")

tk.Button(root, text="Submit", command=get_values).pack(pady=20)

root.mainloop()

Key Features Explained

The enhanced implementation provides these benefits:

  • <FocusIn> event: Clears placeholder when user clicks the entry
  • <FocusOut> event: Restores placeholder if field is left empty
  • Color coding: Placeholder text appears in grey, user input in black
  • Reusable class: Easy to create multiple placeholder entries

Comparison of Methods

Method Pros Cons
Basic FocusIn Simple, quick implementation Placeholder doesn't return if field is empty
Class-based Reusable, handles empty fields, visual feedback More complex code

Conclusion

Use the basic method for simple placeholder text that disappears on focus. The enhanced class-based approach provides better user experience by handling empty fields and offering visual feedback through color changes.

Updated on: 2026-03-26T18:37:55+05:30

24K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements