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

ฟีเจอร์ Undo และ Redo ใน Tkinter Text Widget


Tkinter Text widget เป็น widget รับข้อมูลคล้าย Entry widget ที่รับข้อมูลหลายบรรทัด มีฟีเจอร์ในตัวมากมายช่วยกำหนดคุณสมบัติเริ่มต้นของ Text widget อย่างไรก็ตาม เพื่อเพิ่มฟีเจอร์ Undo/Redo ใน Text widget ให้ใช้แอตทริบิวต์ Boolean undo=True ซึ่งทำให้ข้อความสามารถเรียกคืนได้

ตัวอย่าง

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x350")

# Create a Text widget with undo is set
text=Text(win, width=60, height=20, undo=True)
text.pack()
text.insert(END, "Enter anything Here...")
win.mainloop()

ผลลัพธ์

รันโค้ดเพื่อแสดง Text widget ที่มีฟีเจอร์ Undo/Redo เปิดใช้งาน

ฟีเจอร์ Undo และ Redo ใน Tkinter Text Widget

ทดสอบฟีเจอร์: พิมพ์ข้อความใน Text widget แล้วกด Ctrl+Z เพื่อ Undo หรือ Ctrl+Y เพื่อ Redo