Undo and redo features in a Tkinter Text widget


Tkinter Text widget is another input widget similar to the Entry widget which accepts multiline user input in a text field. It contains many inbuilt features and functions which helps to configure the default properties of the text widget. However, to add undo/Redo features in the Tkinter text widget, we can use Boolean attributes undo which ensures that the text can be retrieved again.

Example

# 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()

Output

Run the above code to display a text widget that has Undo/Redo features enabled.

To test the feature, write something in the Text widget and press Ctrl+Z to undo or Ctrl+Y to redo the text.

Updated on: 19-Jun-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements