]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-32839: Add the after_info() method for Tkinter widgets (GH-5664)
authorCheryl Sabella <cheryl.sabella@gmail.com>
Fri, 26 Apr 2024 21:27:58 +0000 (14:27 -0700)
committerGitHub <noreply@github.com>
Fri, 26 Apr 2024 21:27:58 +0000 (00:27 +0300)
Doc/whatsnew/3.13.rst
Lib/test/test_tkinter/test_misc.py
Lib/tkinter/__init__.py
Misc/NEWS.d/next/Library/2018-02-13-10-02-54.bpo-32839.McbVz3.rst [new file with mode: 0644]

index ad107aad5db3bd7191f5d23669dec22ad294b9a9..083a70ce2405e3986d5f0bd7955c2b1d036783e5 100644 (file)
@@ -791,6 +791,9 @@ tkinter
   :class:`tkinter.ttk.Style`.
   (Contributed by Serhiy Storchaka in :gh:`68166`.)
 
+* Add the :meth:`!after_info` method for Tkinter widgets.
+  (Contributed by Cheryl Sabella in :gh:`77020`.)
+
 traceback
 ---------
 
index 81a20b698a72eb4db00d31c4d09ec5245613dcc9..6dca2a3920e06afdb9f81997290edb99a34d1b91 100644 (file)
@@ -232,6 +232,46 @@ class MiscTest(AbstractTkTest, unittest.TestCase):
         with self.assertRaises(tkinter.TclError):
             root.tk.call('after', 'info', idle1)
 
+    def test_after_info(self):
+        root = self.root
+
+        # No events.
+        self.assertEqual(root.after_info(), ())
+
+        # Add timer.
+        timer = root.after(1, lambda: 'break')
+
+        # With no parameter, it returns a tuple of the event handler ids.
+        self.assertEqual(root.after_info(), (timer, ))
+        root.after_cancel(timer)
+
+        timer1 = root.after(5000, lambda: 'break')
+        timer2 = root.after(5000, lambda: 'break')
+        idle1 = root.after_idle(lambda: 'break')
+        # Only contains new events and not 'timer'.
+        self.assertEqual(root.after_info(), (idle1, timer2, timer1))
+
+        # With a parameter returns a tuple of (script, type).
+        timer1_info = root.after_info(timer1)
+        self.assertEqual(len(timer1_info), 2)
+        self.assertEqual(timer1_info[1], 'timer')
+        idle1_info = root.after_info(idle1)
+        self.assertEqual(len(idle1_info), 2)
+        self.assertEqual(idle1_info[1], 'idle')
+
+        root.after_cancel(timer1)
+        with self.assertRaises(tkinter.TclError):
+            root.after_info(timer1)
+        root.after_cancel(timer2)
+        with self.assertRaises(tkinter.TclError):
+            root.after_info(timer2)
+        root.after_cancel(idle1)
+        with self.assertRaises(tkinter.TclError):
+            root.after_info(idle1)
+
+        # No events.
+        self.assertEqual(root.after_info(), ())
+
     def test_clipboard(self):
         root = self.root
         root.clipboard_clear()
index fd7b48e351999001088688355eae24c310b2d649..70a1ed46fd077433e820344b6732d7cef55436fb 100644 (file)
@@ -897,6 +897,21 @@ class Misc:
             pass
         self.tk.call('after', 'cancel', id)
 
+    def after_info(self, id=None):
+        """Return information about existing event handlers.
+
+        With no argument, return a tuple of the identifiers for all existing
+        event handlers created by the after and after_idle commands for this
+        interpreter.  If id is supplied, it specifies an existing handler; id
+        must have been the return value from some previous call to after or
+        after_idle and it must not have triggered yet or been canceled. If the
+        id doesn't exist, a TclError is raised.  Otherwise, the return value is
+        a tuple containing (script, type) where script is a reference to the
+        function to be called by the event handler and type is either 'idle'
+        or 'timer' to indicate what kind of event handler it is.
+        """
+        return self.tk.splitlist(self.tk.call('after', 'info', id))
+
     def bell(self, displayof=0):
         """Ring a display's bell."""
         self.tk.call(('bell',) + self._displayof(displayof))
diff --git a/Misc/NEWS.d/next/Library/2018-02-13-10-02-54.bpo-32839.McbVz3.rst b/Misc/NEWS.d/next/Library/2018-02-13-10-02-54.bpo-32839.McbVz3.rst
new file mode 100644 (file)
index 0000000..0a2e3e3
--- /dev/null
@@ -0,0 +1 @@
+Add the :meth:`after_info` method for Tkinter widgets.