Python 如何清除/删除 Tkinter Text 小部件的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27966626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:32:48  来源:igfitidea点击:

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

pythontkintertext-widget

提问by Fahadkalis

I am writing a Python program in TKinteron Ubuntu to import and print the name of files from particular folder in Textwidget. It is just adding filenames to the previous filnames in the Textwidget, but I want to clear it first, then add a fresh list of filenames. But I am struggling to clear the Textwidget's previous list of filenames.

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

Can someone please explain how to clear a Textwidget?

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

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

采纳答案by Fahadkalis

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

我通过添加“1.0”来检查我的身边,它开始工作

tex.delete('1.0', END)

you can also try this

你也可以试试这个

回答by DaanP

According to the tkinterbook the code to clear a text element should be:

根据 tkinterbook 清除文本元素的代码应该是:

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

entry.delete(0,END) #注意0而不是1.0

回答by atlas

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.

这是一个txt.delete(1.0,END)如上所述的例子。

The use of lambdamakes us able to delete the contents without defining an actual function.

的使用lambda使我们能够在不定义实际函数的情况下删除内容。

回答by Sai Gopi N

this works

这有效

import tkinter as tk
inputEdit.delete("1.0",tk.END)

回答by George Mogilevsky

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:

对我来说,“1.0”不起作用,但“0”起作用。这是 Python 2.7.12,仅供参考。还取决于您如何导入模块。就是这样:

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

并在需要清除时调用以下代码。在我的例子中,有一个按钮 Save 保存了 Entry 文本框中的数据,点击按钮后,文本框被清除

textBox.delete('0',tk.END)