Skip to content

Commit 5fe26fb

Browse files
gh-151674: Add tkinter Text.edit_canundo() and Text.edit_canredo()
Wrap the Tk text widget "edit canundo" and "edit canredo" subcommands, which report whether the undo and redo stacks are non-empty.
1 parent bfecfcc commit 5fe26fb

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
@@ -138,6 +138,14 @@ shlex
138138
a string, even if it is already safe for a shell without being quoted.
139139
(Contributed by Jay Berry in :gh:`148846`.)
140140

141+
tkinter
142+
-------
143+
144+
* Added new :class:`!tkinter.Text` methods :meth:`~tkinter.Text.edit_canundo`
145+
and :meth:`~tkinter.Text.edit_canredo` which return whether an undo or redo
146+
is possible.
147+
(Contributed by Serhiy Storchaka in :gh:`151674`.)
148+
141149
xml
142150
---
143151

Lib/test/test_tkinter/test_text.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ def test_debug(self):
2525
text.debug(olddebug)
2626
self.assertEqual(text.debug(), olddebug)
2727

28+
def test_edit_undo_redo(self):
29+
text = self.text
30+
text.configure(undo=True)
31+
32+
self.assertIs(text.edit_canundo(), False)
33+
self.assertIs(text.edit_canredo(), False)
34+
35+
text.insert('1.0', 'spam')
36+
self.assertIs(text.edit_canundo(), True)
37+
self.assertIs(text.edit_canredo(), False)
38+
39+
text.edit_undo()
40+
self.assertIs(text.edit_canundo(), False)
41+
self.assertIs(text.edit_canredo(), True)
42+
43+
text.edit_redo()
44+
self.assertIs(text.edit_canundo(), True)
45+
self.assertIs(text.edit_canredo(), False)
46+
47+
text.edit_reset()
48+
self.assertIs(text.edit_canundo(), False)
49+
self.assertIs(text.edit_canredo(), False)
50+
2851
def test_search(self):
2952
text = self.text
3053

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)