Skip to content
Next Next commit
Add after_info to tkinter
  • Loading branch information
csabella committed Mar 4, 2018
commit f3598e1c658de39f16d1ec1cec360a19a2297661
19 changes: 19 additions & 0 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ def after_idle(self, func, *args):
Return an identifier to cancel the scheduling with
after_cancel."""
return self.after('idle', func, *args)

Comment thread
terryjreedy marked this conversation as resolved.
def after_cancel(self, id):
"""Cancel scheduling of function identified with ID.

Expand All @@ -775,6 +776,24 @@ def after_cancel(self, id):
except TclError:
pass
self.tk.call('after', 'cancel', id)

def after_info(self, id=None):
"""Return information about existing event handlers.

With no argument, return a list of the identifiers for all existing
Comment thread
csabella marked this conversation as resolved.
Outdated
event handlers created by the after command 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. Othewise, the return value is a tuple
containing (script, type) where type is either 'idle' or 'timer' to
Comment thread
csabella marked this conversation as resolved.
Outdated
indicate what kind of event handler it is.
"""
if id is None:
return self.tk.call('after', 'info')
Comment thread
csabella marked this conversation as resolved.
Outdated
else:
return self.tk.call('after', 'info', id)
Comment thread
csabella marked this conversation as resolved.
Outdated

def bell(self, displayof=0):
"""Ring a display's bell."""
self.tk.call(('bell',) + self._displayof(displayof))
Expand Down
34 changes: 34 additions & 0 deletions Lib/tkinter/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,40 @@ def callback():
with self.assertRaises(tkinter.TclError):
root.tk.call('after', 'info', idle1)

def test_after_info(self):
root = self.root
# 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, ))
Comment thread
serhiy-storchaka marked this conversation as resolved.
Outdated
# Process event to remove it.
root.update()

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))
Comment thread
serhiy-storchaka marked this conversation as resolved.
Outdated

# With a paramter returns a tuple of (script, type).
Comment thread
csabella marked this conversation as resolved.
Outdated
timer1_info = root.after_info(timer1)
Comment thread
csabella marked this conversation as resolved.
Outdated
self.assertIn('lambda', timer1_info[0])
Comment thread
csabella marked this conversation as resolved.
Outdated
self.assertEqual(timer1_info[1], 'timer')
idle1_info = root.after_info(idle1)
self.assertIn('lambda', idle1_info[0])
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)


tests_gui = (MiscTest, )

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add after_info to tkinter.
Comment thread
serhiy-storchaka marked this conversation as resolved.
Outdated