Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

python - 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 + "
")

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

tex.delete('1.0', END)

you can also try this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...