summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCheryl Sabella <cheryl.sabella@gmail.com>2024-04-26 21:27:58 (GMT)
committerGitHub <noreply@github.com>2024-04-26 21:27:58 (GMT)
commit194fd17bc6cb73138e2fe8eb5ca34b19a6c3b25a (patch)
tree1bf54e346229e095933c7a79a0bc4fbaab11d679
parentb43c7e1070e515b3e94043ff777ab83074234051 (diff)
downloadcpython-194fd17bc6cb73138e2fe8eb5ca34b19a6c3b25a.zip
cpython-194fd17bc6cb73138e2fe8eb5ca34b19a6c3b25a.tar.gz
cpython-194fd17bc6cb73138e2fe8eb5ca34b19a6c3b25a.tar.bz2
bpo-32839: Add the after_info() method for Tkinter widgets (GH-5664)
-rw-r--r--Doc/whatsnew/3.13.rst3
-rw-r--r--Lib/test/test_tkinter/test_misc.py40
-rw-r--r--Lib/tkinter/__init__.py15
-rw-r--r--Misc/NEWS.d/next/Library/2018-02-13-10-02-54.bpo-32839.McbVz3.rst1
4 files changed, 59 insertions, 0 deletions
diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst
index ad107aa..083a70c 100644
--- a/Doc/whatsnew/3.13.rst
+++ b/Doc/whatsnew/3.13.rst
@@ -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
---------
diff --git a/Lib/test/test_tkinter/test_misc.py b/Lib/test/test_tkinter/test_misc.py
index 81a20b6..6dca2a3 100644
--- a/Lib/test/test_tkinter/test_misc.py
+++ b/Lib/test/test_tkinter/test_misc.py
@@ -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()
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index fd7b48e..70a1ed4 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -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
index 0000000..0a2e3e3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-02-13-10-02-54.bpo-32839.McbVz3.rst
@@ -0,0 +1 @@
+Add the :meth:`after_info` method for Tkinter widgets.