Skip to content

Commit aa5b164

Browse files
gh-151674: Add tkinter Text.edit_canundo() and Text.edit_canredo() (GH-151676)
Wrap the Tk text widget "edit canundo" and "edit canredo" subcommands, which report whether the undo and redo stacks are non-empty. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent a9db5cb commit aa5b164

5 files changed

Lines changed: 64 additions & 0 deletions

File tree

Doc/library/tkinter.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5475,6 +5475,20 @@ Widget classes
54755475
inserted or deleted.
54765476
Otherwise set the flag to the boolean *arg*.
54775477

5478+
.. method:: edit_canundo()
5479+
5480+
Return ``True`` if there is an edit action on the undo stack that can be
5481+
undone, and ``False`` otherwise.
5482+
5483+
.. versionadded:: next
5484+
5485+
.. method:: edit_canredo()
5486+
5487+
Return ``True`` if there is an edit action on the redo stack that can be
5488+
reapplied, and ``False`` otherwise.
5489+
5490+
.. versionadded:: next
5491+
54785492
.. method:: edit_undo()
54795493

54805494
Undo the most recent edit action, that is, all the inserts and deletes

Doc/whatsnew/3.16.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ shlex
144144
a string, even if it is already safe for a shell without being quoted.
145145
(Contributed by Jay Berry in :gh:`148846`.)
146146

147+
tkinter
148+
-------
149+
150+
* Added new :class:`!tkinter.Text` methods :meth:`~tkinter.Text.edit_canundo`
151+
and :meth:`~tkinter.Text.edit_canredo` which return whether an undo or redo
152+
is possible.
153+
(Contributed by Serhiy Storchaka in :gh:`151674`.)
154+
147155
xml
148156
---
149157

Lib/test/test_tkinter/test_text.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,29 @@ def test_edit_undo_redo(self):
306306
text.edit_reset()
307307
self.assertRaises(TclError, text.edit_undo)
308308

309+
def test_edit_canundo_canredo(self):
310+
text = self.text
311+
text.configure(undo=True)
312+
313+
self.assertIs(text.edit_canundo(), False)
314+
self.assertIs(text.edit_canredo(), False)
315+
316+
text.insert('1.0', 'spam')
317+
self.assertIs(text.edit_canundo(), True)
318+
self.assertIs(text.edit_canredo(), False)
319+
320+
text.edit_undo()
321+
self.assertIs(text.edit_canundo(), False)
322+
self.assertIs(text.edit_canredo(), True)
323+
324+
text.edit_redo()
325+
self.assertIs(text.edit_canundo(), True)
326+
self.assertIs(text.edit_canredo(), False)
327+
328+
text.edit_reset()
329+
self.assertIs(text.edit_canundo(), False)
330+
self.assertIs(text.edit_canredo(), False)
331+
309332
def test_dump(self):
310333
text = self.text
311334
text.insert('1.0', 'hello')

Lib/tkinter/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3970,6 +3970,22 @@ def edit(self, *args):
39703970
"""
39713971
return self.tk.call(self._w, 'edit', *args)
39723972

3973+
def edit_canredo(self):
3974+
"""Return whether redo is possible.
3975+
3976+
Return True if redo is possible, i.e. when the redo stack is
3977+
not empty, and False otherwise.
3978+
"""
3979+
return self.tk.getboolean(self.edit("canredo"))
3980+
3981+
def edit_canundo(self):
3982+
"""Return whether undo is possible.
3983+
3984+
Return True if undo is possible, i.e. when the undo stack is
3985+
not empty, and False otherwise.
3986+
"""
3987+
return self.tk.getboolean(self.edit("canundo"))
3988+
39733989
def edit_modified(self, arg=None):
39743990
"""Get or Set the modified flag
39753991
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add the :meth:`~tkinter.Text.edit_canundo` and :meth:`~tkinter.Text.edit_canredo`
2+
methods of :class:`!tkinter.Text`, wrapping the Tk ``edit canundo`` and
3+
``edit canredo`` subcommands.

0 commit comments

Comments
 (0)