]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-151674: Add tkinter Text.edit_canundo() and Text.edit_canredo() (GH-151676)
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 20 Jun 2026 06:39:09 +0000 (09:39 +0300)
committerGitHub <noreply@github.com>
Sat, 20 Jun 2026 06:39:09 +0000 (09:39 +0300)
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>
Doc/library/tkinter.rst
Doc/whatsnew/3.16.rst
Lib/test/test_tkinter/test_text.py
Lib/tkinter/__init__.py
Misc/NEWS.d/next/Library/2026-06-19-12-00-00.gh-issue-151674.FCYTvs.rst [new file with mode: 0644]

index b0421721bf8d7e5ea6c6615946e57c2f9942125c..3d8983c57a12bf03e53f38319b0d691f0d2bf2ff 100644 (file)
@@ -5475,6 +5475,20 @@ Widget classes
       inserted or deleted.
       Otherwise set the flag to the boolean *arg*.
 
+   .. method:: edit_canundo()
+
+      Return ``True`` if there is an edit action on the undo stack that can be
+      undone, and ``False`` otherwise.
+
+      .. versionadded:: next
+
+   .. method:: edit_canredo()
+
+      Return ``True`` if there is an edit action on the redo stack that can be
+      reapplied, and ``False`` otherwise.
+
+      .. versionadded:: next
+
    .. method:: edit_undo()
 
       Undo the most recent edit action, that is, all the inserts and deletes
index 0a110795f371eb7f1279c46644564881036e7c35..2b6f396bdf16dd9376d9800b0c40d36dcbc1ba8d 100644 (file)
@@ -144,6 +144,14 @@ shlex
   a string, even if it is already safe for a shell without being quoted.
   (Contributed by Jay Berry in :gh:`148846`.)
 
+tkinter
+-------
+
+* Added new :class:`!tkinter.Text` methods :meth:`~tkinter.Text.edit_canundo`
+  and :meth:`~tkinter.Text.edit_canredo` which return whether an undo or redo
+  is possible.
+  (Contributed by Serhiy Storchaka in :gh:`151674`.)
+
 xml
 ---
 
index 0303c2ac1ed1dabbdbd63bc85b887ad27b210ead..0279198b179404fbc3aa94a1eb6c0f0e8f721c86 100644 (file)
@@ -306,6 +306,29 @@ class TextTest(AbstractTkTest, unittest.TestCase):
         text.edit_reset()
         self.assertRaises(TclError, text.edit_undo)
 
+    def test_edit_canundo_canredo(self):
+        text = self.text
+        text.configure(undo=True)
+
+        self.assertIs(text.edit_canundo(), False)
+        self.assertIs(text.edit_canredo(), False)
+
+        text.insert('1.0', 'spam')
+        self.assertIs(text.edit_canundo(), True)
+        self.assertIs(text.edit_canredo(), False)
+
+        text.edit_undo()
+        self.assertIs(text.edit_canundo(), False)
+        self.assertIs(text.edit_canredo(), True)
+
+        text.edit_redo()
+        self.assertIs(text.edit_canundo(), True)
+        self.assertIs(text.edit_canredo(), False)
+
+        text.edit_reset()
+        self.assertIs(text.edit_canundo(), False)
+        self.assertIs(text.edit_canredo(), False)
+
     def test_dump(self):
         text = self.text
         text.insert('1.0', 'hello')
index ba8365f56c37a70ca71869a58b7d622223ecd539..559fe87ed46193b1dbe10cb801145040f3352cf4 100644 (file)
@@ -3970,6 +3970,22 @@ class Text(Widget, XView, YView):
         """
         return self.tk.call(self._w, 'edit', *args)
 
+    def edit_canredo(self):
+        """Return whether redo is possible.
+
+        Return True if redo is possible, i.e. when the redo stack is
+        not empty, and False otherwise.
+        """
+        return self.tk.getboolean(self.edit("canredo"))
+
+    def edit_canundo(self):
+        """Return whether undo is possible.
+
+        Return True if undo is possible, i.e. when the undo stack is
+        not empty, and False otherwise.
+        """
+        return self.tk.getboolean(self.edit("canundo"))
+
     def edit_modified(self, arg=None):
         """Get or Set the modified flag
 
diff --git a/Misc/NEWS.d/next/Library/2026-06-19-12-00-00.gh-issue-151674.FCYTvs.rst b/Misc/NEWS.d/next/Library/2026-06-19-12-00-00.gh-issue-151674.FCYTvs.rst
new file mode 100644 (file)
index 0000000..84e21d4
--- /dev/null
@@ -0,0 +1,3 @@
+Add the :meth:`~tkinter.Text.edit_canundo` and :meth:`~tkinter.Text.edit_canredo`
+methods of :class:`!tkinter.Text`, wrapping the Tk ``edit canundo`` and
+``edit canredo`` subcommands.