ภาพรวม
บางครั้งต้องการลบ Widget ที่ไม่ใช้ในแอป ลบ Widgets จาก Window/Frame ได้ด้วยเมธอด .destroy() ใน Tkinter เรียกใน Widget โดยนิยาม Function สำหรับมัน
ตัวอย่างโค้ด
# Import the tkinter library
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Set the geometry
win.geometry("650x450")
# Define a function to remove the text from the screen
def delete_text():
text.destroy()
# Create a text widget
text = Label(win, text="This is a New Line", font=('Arial bold', 20))
text.pack(pady=20)
# Create a button for Deleting Widget
Button(win, text="Click Here", font=('bold', 20), command=delete_text).pack(pady=10)
win.mainloop()
ผลลัพธ์
รันโค้ดจะผลลัพธ์ดังนี้

คลิก "Click Here" จะลบ Label Text Widget จากหน้าต่าง
