How to clear/delete the contents of a Tkinter Text widget?

PythonTkinterText Widget

Python Problem Overview


I am writing a Python program in TKinter on Ubuntu to import and print the name of files from particular folder in Text widget. It is just adding filenames to the previous filnames in the Text widget, but I want to clear it first, then add a fresh list of filenames. But I am struggling to clear the Text widget's previous list of filenames.

Can someone please explain how to clear a Text widget?

Screenshoot and coding is giving below:

screenshot showing text widget with contents

import os
from Tkinter import *

def viewFile():
    path = os.path.expanduser("~/python")
    for f in os.listdir(path):
        tex.insert(END, f + "\n")

if __name__ == '__main__':
    root = Tk()

    step= root.attributes('-fullscreen', True)
    step = LabelFrame(root, text="FILE MANAGER", font="Arial 20 bold italic")
    step.grid(row=0, columnspan=7, sticky='W', padx=100, pady=5, ipadx=130, ipady=25)

    Button(step, text="File View", font="Arial 8 bold italic", activebackground=
           "turquoise", width=30, height=5, command=viewFile).grid(row=1, column=2)
    Button(step, text="Quit", font="Arial 8 bold italic", activebackground=
           "turquoise", width=20, height=5, command=root.quit).grid(row=1, column=5)

    tex = Text(master=root)
    scr=Scrollbar(root, orient=VERTICAL, command=tex.yview)
    scr.grid(row=2, column=2, rowspan=15, columnspan=1, sticky=NS)
    tex.grid(row=2, column=1, sticky=W)
    tex.config(yscrollcommand=scr.set, font=('Arial', 8, 'bold', 'italic'))

    root.mainloop()

Python Solutions


Solution 1 - Python

I checked on my side by just adding '1.0' and it start working

tex.delete('1.0', END)

you can also try this

Solution 2 - Python

According to the tkinterbook the code to clear a text element should be:

text.delete(1.0,END)

This worked for me. source

It's different from clearing an entry element, which is done like this:

entry.delete(0,END) #note the 0 instead of 1.0

Solution 3 - Python

this works

import tkinter as tk
inputEdit.delete("1.0",tk.END)

Solution 4 - Python

from Tkinter import *

app = Tk()

# Text Widget + Font Size
txt = Text(app, font=('Verdana',8))
txt.pack()

# Delete Button
btn = Button(app, text='Delete', command=lambda: txt.delete(1.0,END))
btn.pack()

app.mainloop()

Here's an example of txt.delete(1.0,END) as mentioned.

The use of lambda makes us able to delete the contents without defining an actual function.

Solution 5 - Python

for me "1.0" didn't work, but '0' worked. This is Python 2.7.12, just FYI. Also depends on how you import the module. Here's how:

import Tkinter as tk
window = tk.Tk()
textBox = tk.Entry(window)
textBox.pack()

And the following code is called when you need to clear it. In my case there was a button Save that saves the data from the Entry text box and after the button is clicked, the text box is cleared

textBox.delete('0',tk.END)

Solution 6 - Python

I think this:

text.delete("1.0", tkinter.END)

Or if you did from tkinter import *

text.delete("1.0", END)

That should work

Solution 7 - Python

A lot of answers ask you to use END, but if that's not working for you, try:

text.delete("1.0", "end-1c")

Solution 8 - Python

text.delete(0, END)

This deletes everything inside the text box

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFahadkalisView Question on Stackoverflow
Solution 1 - PythonFahadkalisView Answer on Stackoverflow
Solution 2 - PythonDaanPView Answer on Stackoverflow
Solution 3 - Pythonsaigopi.meView Answer on Stackoverflow
Solution 4 - PythonkarlzafirisView Answer on Stackoverflow
Solution 5 - PythonGeorge MogilevskyView Answer on Stackoverflow
Solution 6 - PythonLolaKidView Answer on Stackoverflow
Solution 7 - PythonMomoroView Answer on Stackoverflow
Solution 8 - PythonKazumaView Answer on Stackoverflow