Solution for tkinter Text edit_undo()/edit_redo() Not Working

Translation Notice
This article was machine-translated using DeepSeek-R1.

  • Original Version: Authored in Chinese by myself
  • Accuracy Advisory: Potential discrepancies may exist between translations
  • Precedence: The Chinese text shall prevail in case of ambiguity
  • Feedback: Technical suggestions regarding translation quality are welcomed

Problem

tkinter.Text calls to edit_undo() and edit_redo() have no effect.

Analysis

This occurs because the undo parameter of Text is not set to True. Only after setting this can edit_undo() and edit_redo() be “activated”.
This error is difficult to detect since it doesn’t throw an error message. Discovered after extensive research…
Hope the Python official team can improve this aspect in the future.

Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from tkinter import *

root = Tk()
root.title('Undo/redo test')
text = Text(root, undo=True)
#                 ^^^^^^^^^
#                 Important!
text.insert(END, 'Hello tkinter!')
text.edit_undo()
text.edit_redo()

root.mainloop()
Built with Hugo
Theme Stack designed by Jimmy