如何刪除 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