Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีลบเนื้อหาใน Tkinter Text Box

Tkinter มีฟังก์ชันและโมดูลมากมายที่ช่วยสร้างแอปพลิเคชัน đầyฟีเจอร์ด้วยปุ่ม กล่องโต้ตอบ Widgets และอื่นๆ

เพื่อสร้าง Text Widget เราสามารถใช้ฟังก์ชัน Entry Widget ของ Tkinter ซึ่งเป็น Constructor และรับ Window หรือ Frame ของ Tkinter ต่อไปเราสามารถลบเนื้อหาของ Text Widget นี้ได้โดยใช้ Built-in Method delete(first, last=None) ซึ่งรับช่วง (Range) ภายใน Textbox

ในตัวอย่างนี้ เราจะสร้างปุ่ม Delete ซึ่งลบเนื้อหาทั้งหมดจาก Text Box ที่กำหนด

ตัวอย่าง

from tkinter import *

win = Tk()
win.geometry("600x300")

label = Label(win, text="Write something ??", font=('Helvetica', 25))
label.pack(pady=20)

# Create a Text Widget
text = Text(win, height=10)
text.pack()

def delete():
    text.delete("1.0", "end")

# Create a Delete Button to remove the Text from the text-widget
b1 = Button(win, text="Delete", command=delete)
b1.pack(pady=10)

win.mainloop()

ผลลัพธ์

การรันโค้ดข้างต้นจะสร้าง Text Widget และปุ่ม Delete ที่ใช้ลบเนื้อหาที่เขียนใน Text Box

วิธีลบเนื้อหาใน Tkinter Text Box

พิมพ์ข้อความใน Textbox แล้วคลิกปุ่ม "Delete" จะลบเนื้อหาภายใน Textbox

วิธีลบเนื้อหาใน Tkinter Text Box