72

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()
martineau
  • 119,623
  • 25
  • 170
  • 301
Fahadkalis
  • 2,971
  • 8
  • 23
  • 40
  • 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? – Bryan Oakley Jan 15 '15 at 15:22
  • 1
    Maybe http://effbot.org/tkinterbook/entry.htm#Tkinter.Entry.delete-method – ρss Jan 15 '15 at 15:34
  • Can you plz write the one statement here to get my required result – Fahadkalis Jan 15 '15 at 15:45
  • i wrote this command but it not effective **tex.delete('0', END)** – Fahadkalis Jan 15 '15 at 15:52
  • 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 Aug 22 '16 at 08: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](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html) instead, which was written by John Shipman for the NM Tech Computer Center. – martineau Nov 11 '17 at 16:18
  • @ρss: The link in your comment is for a `Tkinter.Entry` widget. The effbot.org documentation for a `Tkinter.Text` widget is [here](http://effbot.org/tkinterbook/text.htm). 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. – martineau Nov 11 '17 at 16:28

9 Answers9

119

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

tex.delete('1.0', END)

you can also try this

Fahadkalis
  • 2,971
  • 8
  • 23
  • 40
  • 7
    Thanks, that worked once I realized I needed to do a tex.config(state=NORMAL) before I could delete it. – Seth May 17 '17 at 20:54
  • 4
    `1.0` or `'1.0'` both work. Make sure the state of the widget is set to normal, `tex.config(state=NORMAL)`. – TankorSmash Oct 04 '18 at 00:38
  • 1
    @TankorSmash Your comment helps me understanding my error. if `state=disabled`, then it won't work. – chessguy Jul 23 '20 at 21:36
  • I am getting this error `self.Text_box.delete('1.0' , END) NameError: name 'END' is not defined` –  Oct 04 '20 at 08:00
  • 4
    @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. – Samuel Bierwagen Oct 16 '20 at 23:06
  • If `END` isn't working for you even after doing what @SamuelBierwagen said, try `'end-1c'` instead. – Momoro Nov 20 '20 at 23:35
27

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
Guillaume G
  • 313
  • 1
  • 2
  • 15
DaanP
  • 311
  • 3
  • 5
  • 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 Nov 11 '17 at 16:39
11

this works

import tkinter as tk
inputEdit.delete("1.0",tk.END)
saigopi.me
  • 14,011
  • 2
  • 83
  • 54
  • 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 – Vishal Chhodwani Apr 11 '18 at 02: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.

incalite
  • 3,077
  • 3
  • 26
  • 32
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)
  • `'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)` – Al Sweigart Jun 19 '19 at 20:48
2

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')
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

LolaKid
  • 51
  • 2
  • 2
    This is a copy of @Fahadkalis's accepted solution which was posted all the way back in 2015. – Spooky Aug 21 '20 at 04: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")

Dharman
  • 30,962
  • 25
  • 85
  • 135
Momoro
  • 599
  • 4
  • 23
0
text.delete(0, END)

This deletes everything inside the text box

Kazuma
  • 11
  • 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 Dec 12 '21 at 20:10