Skip to content

gh-126899: Add **kw to tkinter.Misc.after and tkinter.Misc.after_idle #126900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 1, 2024
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,14 @@ sys
from other interpreters than the one it's called in.


tkinter
-------

* The methods :meth:`!tkinter.Misc.after` and :meth:`!tkinter.Misc.after_idle`
now support passing keyword arguments to the function.
(Contributed by Zhikang Yan in :gh:`126899`.)


unicodedata
-----------

Expand Down
20 changes: 16 additions & 4 deletions Lib/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ def test_tk_setPalette(self):
def test_after(self):
root = self.root

def callback(start=0, step=1):
def callback(start=0, step=1, *, end=0):
nonlocal count
count = start + step
count = start + step + end

# Without function, sleeps for ms.
self.assertIsNone(root.after(1))
Expand Down Expand Up @@ -161,12 +161,18 @@ def callback(start=0, step=1):
root.update() # Process all pending events.
self.assertEqual(count, 53)

# Set up with callback with keyword args.
count = 0
timer1 = root.after(0, callback, 42, step=11, end=1)
root.update() # Process all pending events.
self.assertEqual(count, 54)

def test_after_idle(self):
root = self.root

def callback(start=0, step=1):
def callback(start=0, step=1, *, end=0):
nonlocal count
count = start + step
count = start + step + end

# Set up with callback with no args.
count = 0
Expand All @@ -193,6 +199,12 @@ def callback(start=0, step=1):
with self.assertRaises(tkinter.TclError):
root.tk.call(script)

# Set up with callback with keyword args.
count = 0
idle1 = root.after_idle(callback, 42, step=11, end=1)
root.update() # Process all pending events.
self.assertEqual(count, 54)

def test_after_cancel(self):
root = self.root

Expand Down
8 changes: 4 additions & 4 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def tk_focusPrev(self):
if not name: return None
return self._nametowidget(name)

def after(self, ms, func=None, *args):
def after(self, ms, func=None, *args, **kw):
"""Call function once after given time.

MS specifies the time in milliseconds. FUNC gives the
Expand All @@ -861,7 +861,7 @@ def after(self, ms, func=None, *args):
else:
def callit():
try:
func(*args)
func(*args, **kw)
finally:
try:
self.deletecommand(name)
Expand All @@ -875,13 +875,13 @@ def callit():
name = self._register(callit)
return self.tk.call('after', ms, name)

def after_idle(self, func, *args):
def after_idle(self, func, *args, **kw):
"""Call FUNC once if the Tcl main loop has no event to
process.

Return an identifier to cancel the scheduling with
after_cancel."""
return self.after('idle', func, *args)
return self.after('idle', func, *args, **kw)

def after_cancel(self, id):
"""Cancel scheduling of function identified with ID.
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,7 @@ Arnon Yaari
Alakshendra Yadav
Hirokazu Yamamoto
Masayuki Yamamoto
Zhikang Yan
Jingchen Ye
Ka-Ping Yee
Chi Hsuan Yen
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the var-keyword parameter to :meth:`!tkinter.Misc.after` and :meth:`!tkinter.Misc.after_idle`.
Loading