Skip to content

Commit a250d3f

Browse files
Xiaokang2022serhiy-storchaka
authored andcommitted
pythongh-126899: Add **kw to tkinter.Misc.after and tkinter.Misc.after_idle (python#126900)
--------- Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent e9c9f33 commit a250d3f

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

Doc/whatsnew/3.14.rst

+8
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,14 @@ sys
576576
from other interpreters than the one it's called in.
577577

578578

579+
tkinter
580+
-------
581+
582+
* Make tkinter widget methods :meth:`!after` and :meth:`!after_idle` accept
583+
arguments passed by keyword.
584+
(Contributed by Zhikang Yan in :gh:`126899`.)
585+
586+
579587
unicodedata
580588
-----------
581589

Lib/test/test_tkinter/test_misc.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ def test_tk_setPalette(self):
123123
def test_after(self):
124124
root = self.root
125125

126-
def callback(start=0, step=1):
126+
def callback(start=0, step=1, *, end=0):
127127
nonlocal count
128-
count = start + step
128+
count = start + step + end
129129

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

164+
# Set up with callback with keyword args.
165+
count = 0
166+
timer1 = root.after(0, callback, 42, step=11, end=1)
167+
root.update() # Process all pending events.
168+
self.assertEqual(count, 54)
169+
164170
def test_after_idle(self):
165171
root = self.root
166172

167-
def callback(start=0, step=1):
173+
def callback(start=0, step=1, *, end=0):
168174
nonlocal count
169-
count = start + step
175+
count = start + step + end
170176

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

202+
# Set up with callback with keyword args.
203+
count = 0
204+
idle1 = root.after_idle(callback, 42, step=11, end=1)
205+
root.update() # Process all pending events.
206+
self.assertEqual(count, 54)
207+
196208
def test_after_cancel(self):
197209
root = self.root
198210

Lib/tkinter/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ def tk_focusPrev(self):
847847
if not name: return None
848848
return self._nametowidget(name)
849849

850-
def after(self, ms, func=None, *args):
850+
def after(self, ms, func=None, *args, **kw):
851851
"""Call function once after given time.
852852
853853
MS specifies the time in milliseconds. FUNC gives the
@@ -861,7 +861,7 @@ def after(self, ms, func=None, *args):
861861
else:
862862
def callit():
863863
try:
864-
func(*args)
864+
func(*args, **kw)
865865
finally:
866866
try:
867867
self.deletecommand(name)
@@ -875,13 +875,13 @@ def callit():
875875
name = self._register(callit)
876876
return self.tk.call('after', ms, name)
877877

878-
def after_idle(self, func, *args):
878+
def after_idle(self, func, *args, **kw):
879879
"""Call FUNC once if the Tcl main loop has no event to
880880
process.
881881
882882
Return an identifier to cancel the scheduling with
883883
after_cancel."""
884-
return self.after('idle', func, *args)
884+
return self.after('idle', func, *args, **kw)
885885

886886
def after_cancel(self, id):
887887
"""Cancel scheduling of function identified with ID.

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,7 @@ Arnon Yaari
20792079
Alakshendra Yadav
20802080
Hirokazu Yamamoto
20812081
Masayuki Yamamoto
2082+
Zhikang Yan
20822083
Jingchen Ye
20832084
Ka-Ping Yee
20842085
Chi Hsuan Yen
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make tkinter widget methods :meth:`!after` and :meth:`!after_idle` accept
2+
arguments passed by keyword.

0 commit comments

Comments
 (0)