如何删除 Tkinter 文本框的内容

Jinku Hu 2020年6月25日 2020年3月25日
如何删除 Tkinter 文本框的内容

Tkinter Text 文本框控件具有 delete(first, last=None) 方法,可从文本框中删除 first 索引处或 (first, last) 范围内的字符。

如果未给出 last,则仅删除在 first 位置中指定的字符。

清除 Tkinter 文本控件内容的示例代码

import tkinter as tk
root = tk.Tk()
root.geometry("400x240")

def clearTextInput():
    textExample.delete("1.0","end")

textExample=tk.Text(root, height=10)
textExample.pack()
btnRead=tk.Button(root, height=1, width=10, text="Clear", 
                    command=clearTextInput)

btnRead.pack()

root.mainloop()
textExample.get("1.0", "end")

"1.0""end"是指 Tkinter 文本框控件中内容的第一个字符和最后一个字符,与如何获取文本的输入的文章中介绍的内容相同。

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相关文章 - Tkinter Text