如何清除/删除Tkinter Text小部件的内容? [英] How to clear/delete the contents of a Tkinter Text widget?

查看:2416
本文介绍了如何清除/删除Tkinter Text小部件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu上的TKinter中编写一个Python程序以导入和打印 Text小部件中特定文件夹中文件的名称. 它只是将文件名添加到Text中的先前filname中 小部件,但我想先清除它,然后添加一个新的文件名列表. 但我正在努力清除Text小部件的上一个列表 文件名.

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.

有人可以解释一下如何清除Text小部件吗?

Can someone please explain how to clear a Text widget?

屏幕截图和编码如下:

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

推荐答案

我只是添加了'1.0'进行检查,

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

tex.delete('1.0', END)

您也可以尝试

这篇关于如何清除/删除Tkinter Text小部件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆