Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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() 
like image 359
Fahadkalis Avatar asked Jan 15 '15 15:01

Fahadkalis


People also ask

How do you clear a textbox in Python?

Type something inside the textbox, and then, click the “Delete” button. It will erase the content inside the textbox.

How do you delete everything in tkinter?

While creating a canvas in tkinter, it will effectively eat some memory which needs to be cleared or deleted. In order to clear a canvas, we can use the delete() method. By specifying “all”, we can delete and clear all the canvas that are present in a tkinter frame.

How do you clear entries in Python?

Build A Paint Program With TKinter and Python We can clear the content of Entry widget by defining a method delete(0, END) which aims to clear all the content in the range. The method can be invoked by defining a function which can be used by creating a Button Object.

How do I clear the screen GUI in Python?

Build A Paint Program With TKinter and Python If we want to clear the frame content or delete all the widgets inside the frame, we can use the destroy() method. This method can be invoked by targeting the children of the frame using winfo_children().


2 Answers

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

tex.delete('1.0', END) 

you can also try this

like image 174
Fahadkalis Avatar answered Oct 06 '22 08:10

Fahadkalis


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

like image 23
DaanP Avatar answered Oct 06 '22 07:10

DaanP