https://github.com/python/cpython/commit/45fa07b025cb38e607a8f3c6d3ade9a28ce84e34
commit: 45fa07b025cb38e607a8f3c6d3ade9a28ce84e34
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-07-11T05:12:52Z
summary:

gh-153256: Add tk_print() methods to tkinter Canvas and Text (GH-153257)

The native print dialog (the "tk print" command, Tk 8.7/9.0+) only
supports canvas and text widgets, so expose it as a method of Canvas
and Text rather than of Misc.

Co-authored-by: Claude Opus 4.8 <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-07-06-14-18-33.gh-issue-153256.tkPrnt.rst
M Doc/library/tkinter.rst
M Doc/whatsnew/3.16.rst
M Lib/test/test_tkinter/test_misc.py
M Lib/tkinter/__init__.py

diff --git a/Doc/library/tkinter.rst b/Doc/library/tkinter.rst
index 90bb5db7eb38d8..f72912632deb92 100644
--- a/Doc/library/tkinter.rst
+++ b/Doc/library/tkinter.rst
@@ -3555,6 +3555,13 @@ Widget classes
    A newly created item is placed at the top of the list; the order can be
    changed with :meth:`tag_raise` and :meth:`tag_lower`.
 
+   .. method:: tk_print()
+
+      Print the contents of the canvas using the native print dialog.
+      Requires Tk 8.7/9.0 or newer.
+
+      .. versionadded:: next
+
    .. method:: create_arc(*args, **kw)
                create_bitmap(*args, **kw)
                create_image(*args, **kw)
@@ -5366,6 +5373,13 @@ Widget classes
    to its base; several modifiers may be combined and are applied from left to
    right, for example ``'insert wordstart - 1 c'``.
 
+   .. method:: tk_print()
+
+      Print the contents of the text widget using the native print dialog.
+      Requires Tk 8.7/9.0 or newer.
+
+      .. versionadded:: next
+
    .. method:: insert(index, chars, *args)
 
       Insert the string *chars* just before the character at *index* (if
diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst
index 3ee8de18cb5cd3..af4e7f6fda59fc 100644
--- a/Doc/whatsnew/3.16.rst
+++ b/Doc/whatsnew/3.16.rst
@@ -406,6 +406,11 @@ tkinter
   report the user idle time.
   (Contributed by Serhiy Storchaka in :gh:`151881`.)
 
+* Added the :meth:`!tk_print` method to :class:`tkinter.Canvas` and
+  :class:`tkinter.Text` which prints the contents of the widget using the
+  native print dialog.  It requires Tk 8.7/9.0 or newer.
+  (Contributed by Serhiy Storchaka in :gh:`153256`.)
+
 * Added new window-management methods :meth:`~tkinter.Misc.winfo_isdark`
   (dark mode detection), :meth:`~tkinter.Wm.wm_iconbadge` (application icon
   badge) and :meth:`~tkinter.Wm.wm_stackorder` (toplevel stacking order).
diff --git a/Lib/test/test_tkinter/test_misc.py 
b/Lib/test/test_tkinter/test_misc.py
index d315a23ef794de..a225079dd99091 100644
--- a/Lib/test/test_tkinter/test_misc.py
+++ b/Lib/test/test_tkinter/test_misc.py
@@ -651,6 +651,15 @@ def test_tk_inactive(self):
         # Resetting the timer returns None and does not raise.
         self.assertIsNone(self.root.tk_inactive(reset=True))
 
+    def test_tk_print(self):
+        # tk print supports only canvas and text widgets, so tk_print is a
+        # method of Canvas and Text only.  Calling it opens the print
+        # dialog, so the behavior itself cannot be tested here.
+        self.assertHasAttr(tkinter.Canvas, 'tk_print')
+        self.assertHasAttr(tkinter.Text, 'tk_print')
+        self.assertNotHasAttr(tkinter.Frame, 'tk_print')
+        self.assertNotHasAttr(tkinter.Misc, 'tk_print')
+
     def test_wait_variable(self):
         var = tkinter.StringVar(self.root)
         self.assertEqual(self.root.waitvar, self.root.wait_variable)
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index 0616dadcbbf210..6c8eef02478c77 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -3133,6 +3133,12 @@ def __init__(self, master=None, cnf={}, **kw):
         yscrollcommand, yscrollincrement."""
         Widget.__init__(self, master, 'canvas', cnf, kw)
 
+    def tk_print(self):
+        """Print the contents of the canvas using the native print dialog.
+
+        Requires Tk 8.7/9.0 or newer."""
+        self.tk.call('tk', 'print', self._w)
+
     def addtag(self, *args):
         """Internal function."""
         self.tk.call((self._w, 'addtag') + args)
@@ -4074,6 +4080,12 @@ def __init__(self, master=None, cnf={}, **kw):
         """
         Widget.__init__(self, master, 'text', cnf, kw)
 
+    def tk_print(self):
+        """Print the contents of the text widget using the native print dialog.
+
+        Requires Tk 8.7/9.0 or newer."""
+        self.tk.call('tk', 'print', self._w)
+
     def bbox(self, index):  # overrides Misc.bbox
         """Return a tuple of (x,y,width,height) which gives the bounding
         box of the visible part of the character at the given index."""
diff --git 
a/Misc/NEWS.d/next/Library/2026-07-06-14-18-33.gh-issue-153256.tkPrnt.rst 
b/Misc/NEWS.d/next/Library/2026-07-06-14-18-33.gh-issue-153256.tkPrnt.rst
new file mode 100644
index 00000000000000..db61bdb7f7b97d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-07-06-14-18-33.gh-issue-153256.tkPrnt.rst
@@ -0,0 +1,3 @@
+Added the :meth:`!tk_print` method to :class:`tkinter.Canvas` and
+:class:`tkinter.Text` which prints the contents of the widget using the
+native print dialog.  It requires Tk 8.7/9.0 or newer.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to