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

วิธี Clear Entry Widget หลังกดปุ่มใน Tkinter


ภาพรวม

Tkinter Entry Widgets ใช้แสดง Single Line Text ที่รับจาก User Input สามารถ Clear เนื้อหา Entry Widget โดยนิยามเมธอด delete(0, END) ซึ่ง Clear ทุกเนื้อหาใน Range เรียกเมธอดโดยนิยาม Function ใช้โดยสร้าง Button Object

ตัวอย่างโค้ด

# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Set the geometry of frame
win.geometry("650x250")
# Define a function to clear the Entry Widget Content
def clear_text():
    text.delete(0, END)
# Create a entry widget
text = Entry(win, width=40)
text.pack()
# Create a button to clear the Entry Widget
Button(win, text="Clear", command=clear_text, font=('Helvetica bold', 10)).pack(pady=5)
win.mainloop()

ผลลัพธ์

รันโค้ดจะแสดงหน้าต่างที่มี Entry Widget และ Button ใช้ Clear Text ใน Entry Field

วิธี Clear Entry Widget หลังกดปุ่มใน Tkinter

คลิก "Clear" เพื่อ Clear Entry Widget