bpo-32839: Add the after_info() method for Tkinter widgets (GH-5664)

This commit is contained in:
Cheryl Sabella 2024-04-26 14:27:58 -07:00 committed by GitHub
parent b43c7e1070
commit 194fd17bc6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 59 additions and 0 deletions

View 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()

View 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))