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

10

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()
Aalborg answered 15/1, 2015 at 15:15 Comment(7)
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?Articulation
Maybe effbot.org/tkinterbook/entry.htm#Tkinter.Entry.delete-methodGalegalea
Can you plz write the one statement here to get my required resultAalborg
i wrote this command but it not effective tex.delete('0', END)Aalborg
@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.Caenogenesis
@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.Supersession
@ρss: The link in your comment is for a Tkinter.Entry widget. The effbot.org documentation for a Tkinter.Text widget is here. It describes how to what the OP wants (see the Patterns section near the beginning). That said, I think the first argument should be the string '1.0', not the integer 0 it shows for the first argument.Supersession
A
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

Aalborg answered 15/1, 2015 at 16:1 Comment(6)
Thanks, that worked once I realized I needed to do a tex.config(state=NORMAL) before I could delete it.Zip
1.0 or '1.0' both work. Make sure the state of the widget is set to normal, tex.config(state=NORMAL).Lori
@Lori Your comment helps me understanding my error. if state=disabled, then it won't work.Impel
I am getting this error self.Text_box.delete('1.0' , END) NameError: name 'END' is not definedAnthropogeography
@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.Omniscient
If END isn't working for you even after doing what @SamuelBierwagen said, try 'end-1c' instead.Jocose
B
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
Baptlsta answered 4/11, 2016 at 15:0 Comment(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).Supersession
T
11

this works

import tkinter as tk
inputEdit.delete("1.0",tk.END)
Todd answered 11/4, 2018 at 2:14 Comment(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 valueInvolved
S
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.

Sicyon answered 20/7, 2017 at 0:5 Comment(1)
How to set the scrollbar for that viewing text data?Twentyfour
L
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)
Lian answered 19/6, 2018 at 20:19 Comment(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)Sibilant
L
4

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')
Lisle answered 8/5, 2023 at 14:50 Comment(0)
P
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

Pontiac answered 20/8, 2020 at 23:48 Comment(1)
This is a copy of @Fahadkalis's accepted solution which was posted all the way back in 2015.Godavari
J
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")

Jocose answered 20/11, 2020 at 23:38 Comment(0)
A
1

im using custometkinter this work with me

textbox.delete(0.0, END)
Angelynanger answered 16/2 at 7:56 Comment(0)
B
0
text.delete(0, END)

This deletes everything inside the text box

Birthright answered 7/6, 2021 at 17:18 Comment(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.Lathery

© 2022 - 2024 — McMap. All rights reserved.