How to clear the Entry widget after a button is pressed in Tkinter?

Tkinter Entry widgets are used to display a single line text that is generally taken in the form of user input. We can clear the content of Entry widget by defining a method delete(0, END) which aims to clear all the content in the range. The method can be invoked by defining a function which can be used by creating a Button object.

Syntax

The basic syntax to clear an Entry widget ?

entry_widget.delete(0, END)

Example

In this example, we have created an entry widget and a button that can be used to clear all the content from the widget ?

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame
win = Tk()

# Set the geometry of frame
win.geometry("650x250")

# Define a function to clear the Entry Widget Content
def clear_text():
    text.delete(0, END)

# Create a entry widget
text = Entry(win, width=40)
text.pack(pady=10)

# Create a button to clear the Entry Widget
Button(win, text="Clear", command=clear_text, font=('Helvetica bold', 10)).pack(pady=5)

win.mainloop()

How It Works

The delete(0, END) method removes all text from the Entry widget. The parameters work as follows:

  • 0 − Starting position (first character)
  • END − Ending position (last character)

Alternative Approach Using set() Method

You can also clear an Entry widget using a StringVar and the set() method ?

from tkinter import *

win = Tk()
win.geometry("650x250")

# Create a StringVar
text_var = StringVar()

def clear_text():
    text_var.set("")  # Clear by setting empty string

# Create entry widget with StringVar
entry = Entry(win, textvariable=text_var, width=40)
entry.pack(pady=10)

Button(win, text="Clear", command=clear_text, font=('Helvetica bold', 10)).pack(pady=5)

win.mainloop()

Comparison

Method Code Best For
delete() entry.delete(0, END) Direct manipulation of Entry widget
set() text_var.set("") When using StringVar for data binding
Sample text in Entry widget Clear (empty) Before: After:

Conclusion

Use delete(0, END) to clear Entry widgets directly. For more complex applications with data binding, consider using StringVar with the set("") method for cleaner code management.

Updated on: 2026-03-25T18:24:58+05:30

46K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements