79

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()
7
  • 1
    Have you read any documentation for the text widget? This feature is clearly documented. You say you're struggling, can you show us what you've tried? Commented Jan 15, 2015 at 15:22
  • 1
    Maybe effbot.org/tkinterbook/entry.htm#Tkinter.Entry.delete-method
    – ρss
    Commented Jan 15, 2015 at 15:34
  • Can you plz write the one statement here to get my required result
    – Fahadkalis
    Commented Jan 15, 2015 at 15:45
  • 2
    @BryanOakley reading the docs seems like a good point though in my opinion the tkinter documentation needs certain kind of transfer effort. So if you ask me: more dokumentation in the form of a question is good documentation. So +1 from me.
    – enthus1ast
    Commented Aug 22, 2016 at 8:37
  • 1
    @enthus1ast: I completely agree that the online effbot.org documentation leaves much to be desired. So instead of using it, I frequently use the Tkinter 8.5 reference guide instead, which was written by John Shipman for the NM Tech Computer Center.
    – martineau
    Commented Nov 11, 2017 at 16:18

10 Answers 10

127

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

tex.delete('1.0', END)

you can also try this

6
  • 8
    Thanks, that worked once I realized I needed to do a tex.config(state=NORMAL) before I could delete it.
    – Seth
    Commented May 17, 2017 at 20:54
  • 5
    1.0 or '1.0' both work. Make sure the state of the widget is set to normal, tex.config(state=NORMAL). Commented Oct 4, 2018 at 0:38
  • 1
    @TankorSmash Your comment helps me understanding my error. if state=disabled, then it won't work.
    – chessguy
    Commented Jul 23, 2020 at 21:36
  • I am getting this error self.Text_box.delete('1.0' , END) NameError: name 'END' is not defined
    – user13786942
    Commented Oct 4, 2020 at 8:00
  • 5
    @NeoNØVÅ7 END only works if you did from Tkinter import *. If you haven't imported everything from Tkinter, then you need to do tk.END instead. Commented Oct 16, 2020 at 23:06
30

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
1
  • I find it strange that 1.0 works (although it indeed does) since my understanding is the Tkinter indices should be specified as strings like '1.0' (which also works and is how it's done in most of the other places in the same document).
    – martineau
    Commented Nov 11, 2017 at 16:39
11

this works

import tkinter as tk
inputEdit.delete("1.0",tk.END)
1
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value Commented Apr 11, 2018 at 2:50
10
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.

1
  • How to set the scrollbar for that viewing text data? Commented Jul 23, 2020 at 7:34
5

I had difficulty figuring out why it did not work for me. Ensure the text/ scrolledtext state is set to 'normal' before clearing it:

def clear_text():
    text_area.config(state='normal')
    text_area.delete('1.0', tk.END)
    text_area.config(state='disabled')
4

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)
1
  • '0' gives me an error when I tried it, I believe the correct value is '1.0', so your code should be textBox.delete('0',tk.END) Commented Jun 19, 2019 at 20:48
1

I think this:

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

Or if you did from tkinter import *

text.delete("1.0", END)

That should work

1
  • 2
    This is a copy of @Fahadkalis's accepted solution which was posted all the way back in 2015.
    – catsock
    Commented Aug 21, 2020 at 4:50
1

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

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

1

im using custometkinter this work with me

textbox.delete(0.0, END)
0
text.delete(0, END)

This deletes everything inside the text box

1
  • 1
    @CogitoErgoCogitoSum Ummmmmm. First of all. Do not assume. It appears you have came here 2 hours ago? My post was on Jun 7! Did you know that answers can be deleted? They can actually. And my comment was posted when this answer was duplicated. So please do not assume and ask me to remove the comment in a polite manner as there is no reason to be rude. Also, this answer is duplicated (textBox.delete('0',tk.END)) is the same thing.
    – Buddy Bob
    Commented Dec 12, 2021 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.