]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-126899: Add `**kw` to `tkinter.Misc.after` and `tkinter.Misc.after_idle` (#126900)
authorZhikang Yan <2951256653@qq.com>
Sun, 1 Dec 2024 19:29:27 +0000 (03:29 +0800)
committerGitHub <noreply@github.com>
Sun, 1 Dec 2024 19:29:27 +0000 (19:29 +0000)
---------
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Doc/whatsnew/3.14.rst
Lib/test/test_tkinter/test_misc.py
Lib/tkinter/__init__.py
Misc/ACKS
Misc/NEWS.d/next/Library/2024-11-16-10-52-48.gh-issue-126899.GFnfBt.rst [new file with mode: 0644]

index 869a47c12612936f9110854b56150e89a8b8d5dc..f9322da3d4fbb018d701bbbd75255a36fd4c0e4a 100644 (file)
@@ -576,6 +576,14 @@ sys
   from other interpreters than the one it's called in.
 
 
+tkinter
+-------
+
+* Make tkinter widget methods :meth:`!after` and :meth:`!after_idle` accept
+  arguments passed by keyword.
+  (Contributed by Zhikang Yan in :gh:`126899`.)
+
+
 unicodedata
 -----------
 
index 579ce2af9fa0bf1ea63deb92d0acec8ed6650306..475edcbd5338a7c50622dcffcf6afcf6ec829072 100644 (file)
@@ -123,9 +123,9 @@ class MiscTest(AbstractTkTest, unittest.TestCase):
     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))
@@ -161,12 +161,18 @@ class MiscTest(AbstractTkTest, unittest.TestCase):
         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
@@ -193,6 +199,12 @@ class MiscTest(AbstractTkTest, unittest.TestCase):
         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
 
index dd7b3e138f4236c970ef3ade819af263e0ae7059..bfec04bb6c1e6e091ce639e8b4bafa9127fd901c 100644 (file)
@@ -847,7 +847,7 @@ class Misc:
         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
@@ -861,7 +861,7 @@ class Misc:
         else:
             def callit():
                 try:
-                    func(*args)
+                    func(*args, **kw)
                 finally:
                     try:
                         self.deletecommand(name)
@@ -875,13 +875,13 @@ class Misc:
             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.
index cd34846574b3041422ffe4eb8c3141cd25c305e6..fc4b83a0e2b823c6cb123910ab09b08a58871c58 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -2079,6 +2079,7 @@ Arnon Yaari
 Alakshendra Yadav
 Hirokazu Yamamoto
 Masayuki Yamamoto
+Zhikang Yan
 Jingchen Ye
 Ka-Ping Yee
 Chi Hsuan Yen
diff --git a/Misc/NEWS.d/next/Library/2024-11-16-10-52-48.gh-issue-126899.GFnfBt.rst b/Misc/NEWS.d/next/Library/2024-11-16-10-52-48.gh-issue-126899.GFnfBt.rst
new file mode 100644 (file)
index 0000000..c1a0ed6
--- /dev/null
@@ -0,0 +1,2 @@
+Make tkinter widget methods :meth:`!after` and :meth:`!after_idle` accept
+arguments passed by keyword.