How to clear Text widget content while clicking on the Entry widget itself in Tkinter?

The Tkinter Text widget is an input widget that supports multiline user input, functioning as a text editor. You can clear its content using the delete(0, END) method. Similarly, you can clear an Entry widget's content by clicking on it, achieved by binding a function to a click event.

Example

Here's how to create an Entry widget that clears its placeholder text when clicked ?

# Import the required libraries
from tkinter import *

# Create an instance of Tkinter Frame
win = Tk()

# Set the geometry of Tkinter Frame
win.geometry("700x250")

# Define a function to clear the content of the Entry widget
def click(event):
    name.configure(state=NORMAL)
    name.delete(0, END)
    name.unbind('<Button-1>', clicked)

# Create a Label widget
label = Label(win, text="Enter Your Name", font=('Helvetica', 13, 'bold'))
label.pack(pady=10)

# Create an Entry widget with placeholder text
name = Entry(win, width=45)
name.insert(0, 'Enter Your Name Here...')
name.pack(pady=10)

# Bind the Entry widget with Mouse Button to clear the content
clicked = name.bind('<Button-1>', click)

win.mainloop()

How It Works

The solution uses three key components ?

  • Event Binding: bind('<Button-1>', click) attaches the click function to left mouse clicks
  • Content Deletion: delete(0, END) removes all text from the Entry widget
  • Event Unbinding: unbind('<Button-1>', clicked) removes the click event after first use

Enhanced Example with Text Widget

You can apply the same concept to a Text widget for multiline input ?

from tkinter import *

win = Tk()
win.geometry("700x300")

def clear_text(event):
    text_widget.configure(state=NORMAL)
    text_widget.delete(1.0, END)
    text_widget.unbind('<Button-1>', text_clicked)

# Create a Text widget
text_widget = Text(win, width=50, height=10)
text_widget.insert(1.0, 'Click here to start typing...')
text_widget.pack(pady=20)

# Bind click event to Text widget
text_clicked = text_widget.bind('<Button-1>', clear_text)

win.mainloop()

Key Differences

Widget Delete Method Index Format Use Case
Entry delete(0, END) Integer indices Single line input
Text delete(1.0, END) Line.character format Multiline input

Conclusion

Use event binding with <Button-1> to clear Entry or Text widgets when clicked. The unbind() method ensures the clearing happens only once, creating an effective placeholder text system.

---
Updated on: 2026-03-25T20:48:10+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements