Tkinter

How to Clear Text From a Text Widget on Click in Tkinter

In this tutorial, we are going to see how to clear text from a Text widget on click in Tkinter. The Text widget owns the delete(first, last=None) method to delete the characters in the range (first, last) of the Text widget.


 
 

How to Clear Text From a Text Widget on Click in Tkinter

In the following example “1.0” and “end” indicate the first character and the last character of the Text widget’s content.

import tkinter as tk

def clear():
    texte.delete("1.0","end")

gui = tk.Tk()
gui.geometry("300x150")

texte = tk.Text(gui, height=5)
texte.pack(pady=10)

btn = tk.Button(gui, text="Clear", command=clear)
btn.pack()

gui.mainloop()

Output:


 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *