ใน Tkinter บางครั้งเราอาจต้องการทำให้ Text Widget ไม่สามารถแก้ไขได้ (Disabled) เพื่อให้บรรลุเป้าหมายนี้ เราสามารถตั้งค่า Text Configuration เป็น DISABLED ซึ่งจะทำให้ Text Widget ล็อกและกลายเป็น Read-Only
ในตัวอย่างนี้ เราจะสร้าง Text Widget และปุ่มที่อนุญาตให้ผู้ใช้ Disable หรือล็อก Text Widget ได้ทันที
ตัวอย่าง
# Import the library
from tkinter import *
# Create an instance of window
win = Tk()
# Set the geometry of the window
win.geometry("700x400")
def disable_button():
text.config(state=DISABLED)
# Label
Label(win, text="Type Something", font=('Helvetica bold', 25), fg="green").pack(pady=20)
# Create a Text widget
text = Text(win, height=10, width=40)
text.pack()
# Create a Disable Button
Button(win, text="Disable", command=disable_button, fg="white", bg="black", width=20).pack(pady=20)
win.mainloop()
ผลลัพธ์
การรันโค้ดข้างต้นจะสร้าง Text Widget และปุ่มที่ใช้ Disable หรือล็อก Widget ได้ทันที

หลังจากคุณคลิกปุ่ม "Disable" Text Widget จะถูก Disable และคุณจะไม่สามารถพิมพ์อะไรเพิ่มเติมภายในได้