8000 bpo-32839: Add the after_info() method for Tkinter widgets (GH-5664) · python/cpython@194fd17 · GitHub
[go: up one dir, main page]

Skip to content

Commit 194fd17

Browse files
authored
bpo-32839: Add the after_info() method for Tkinter widgets (GH-5664)
1 parent b43c7e1 commit 194fd17

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,9 @@ tkinter
791791
:class:`tkinter.ttk.Style`.
792792
(Contributed by Serhiy Storchaka in :gh:`68166`.)
793793

794+
* Add the :meth:`!after_info` method for Tkinter widgets.
795+
(Contributed by Cheryl Sabella in :gh:`77020`.)
796+
794797
traceback
795798
---------
796799

Lib/test/test_tkinter/test_misc.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,46 @@ def callback():
232232
with self.assertRaises(tkinter.TclError):
233233
root.tk.call('after', 'info', idle1)
234234

235+
def test_after_info(self):
236+
root = self.root
237+
238+
# No events.
239+
self.assertEqual(root.after_info(), ())
240+
241+
# Add timer.
242+
timer = root.after(1, lambda: 'break')
243+
244+
# With no parameter, it returns a tuple of the event handler ids.
245+
self.assertEqual(root.after_info(), (timer, ))
246+
root.after_cancel(timer)
247+
248+
timer1 = root.after(5000, lambda: 'break')
249+
timer2 = root.after(5000, lambda: 'break')
250+
idle1 = root.after_idle(lambda: 'break')
251+
# Only contains new events and not 'timer'.
252+
self.assertEqual(root.after_info(), (idle1, timer2, timer1))
253+
254+
# With a parameter returns a tuple of (script, type).
255+
timer1_info = root.after_info(timer1)
256+
self.assertEqual(len(timer1_info), 2)
257+
self.assertEqual(timer1_info[1], 'timer')
258+
idle1_info = root.after_info(idle1)
259+
self.assertEqual(len(idle1_info), 2)
260+
self.assertEqual(idle1_info[1], 'idle')
261+
262+
root.after_cancel(timer1)
263+
with self.assertRaises(tkinter.TclError):
264+
root.after_info(timer1)
265+
root.after_cancel(timer2)
266+
with self.assertRaises(tkinter.TclError):
267+
root.after_info(timer2)
268+
root.after_cancel(idle1)
269+
with self.assertRaises(tkinter.TclError):
270+
root.after_info(idle1)
271+
272+
# No events.
273+
self.assertEqual(root.after_info(), ())
274+
235275
def test_clipboard(self):
236276
root = self.root
237277
root.clipboard_clear()

Lib/tkinter/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,21 @@ def after_cancel(self, id):
897897
pass
898898
self.tk.call('after', 'cancel', id)
899899

900+
def after_info(self, id=None):
901+
"""Return information about existing event handlers.
902+
903+
With no argument, return a tuple of the identifiers for all existing
904+
event handlers created by the after and after_idle commands for this
905+
interpreter. If id is supplied, it specifies an existing handler; id
906+
must have been the return value from some previous call to after or
907+
after_idle and it must not have triggered yet or been canceled. If the
908+
id doesn't exist, a TclError is raised. Otherwise, the return value is
909+
a tuple containing (script, type) where script is a reference to the
910+
function to be called by the event handler and type is either 'idle'
911+
or 'timer' to indicate what kind of event handler it is.
912+
"""
913+
return self.tk.splitlist(self.tk.call('after', 'info', id))
914+
900915
def bell(self, displayof=0):
901916
"""Ring a display's bell."""
902917
self.tk.call(('bell',) + self._displayof(displayof))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add the :meth:`after_info` method for Tkinter widgets.

0 commit comments

Comments
 (0)
0