Skip to content

Commit 45fa07b

Browse files
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 <noreply@anthropic.com>
1 parent bac73b0 commit 45fa07b

5 files changed

Lines changed: 43 additions & 0 deletions

File tree

Doc/library/tkinter.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,6 +3555,13 @@ Widget classes
35553555
A newly created item is placed at the top of the list; the order can be
35563556
changed with :meth:`tag_raise` and :meth:`tag_lower`.
35573557

3558+
.. method:: tk_print()
3559+
3560+
Print the contents of the canvas using the native print dialog.
3561+
Requires Tk 8.7/9.0 or newer.
3562+
3563+
.. versionadded:: next
3564+
35583565
.. method:: create_arc(*args, **kw)
35593566
create_bitmap(*args, **kw)
35603567
create_image(*args, **kw)
@@ -5366,6 +5373,13 @@ Widget classes
53665373
to its base; several modifiers may be combined and are applied from left to
53675374
right, for example ``'insert wordstart - 1 c'``.
53685375

5376+
.. method:: tk_print()
5377+
5378+
Print the contents of the text widget using the native print dialog.
5379+
Requires Tk 8.7/9.0 or newer.
5380+
5381+
.. versionadded:: next
5382+
53695383
.. method:: insert(index, chars, *args)
53705384

53715385
Insert the string *chars* just before the character at *index* (if

Doc/whatsnew/3.16.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,11 @@ tkinter
406406
report the user idle time.
407407
(Contributed by Serhiy Storchaka in :gh:`151881`.)
408408

409+
* Added the :meth:`!tk_print` method to :class:`tkinter.Canvas` and
410+
:class:`tkinter.Text` which prints the contents of the widget using the
411+
native print dialog. It requires Tk 8.7/9.0 or newer.
412+
(Contributed by Serhiy Storchaka in :gh:`153256`.)
413+
409414
* Added new window-management methods :meth:`~tkinter.Misc.winfo_isdark`
410415
(dark mode detection), :meth:`~tkinter.Wm.wm_iconbadge` (application icon
411416
badge) and :meth:`~tkinter.Wm.wm_stackorder` (toplevel stacking order).

Lib/test/test_tkinter/test_misc.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,15 @@ def test_tk_inactive(self):
651651
# Resetting the timer returns None and does not raise.
652652
self.assertIsNone(self.root.tk_inactive(reset=True))
653653

654+
def test_tk_print(self):
655+
# tk print supports only canvas and text widgets, so tk_print is a
656+
# method of Canvas and Text only. Calling it opens the print
657+
# dialog, so the behavior itself cannot be tested here.
658+
self.assertHasAttr(tkinter.Canvas, 'tk_print')
659+
self.assertHasAttr(tkinter.Text, 'tk_print')
660+
self.assertNotHasAttr(tkinter.Frame, 'tk_print')
661+
self.assertNotHasAttr(tkinter.Misc, 'tk_print')
662+
654663
def test_wait_variable(self):
655664
var = tkinter.StringVar(self.root)
656665
self.assertEqual(self.root.waitvar, self.root.wait_variable)

Lib/tkinter/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3133,6 +3133,12 @@ def __init__(self, master=None, cnf={}, **kw):
31333133
yscrollcommand, yscrollincrement."""
31343134
Widget.__init__(self, master, 'canvas', cnf, kw)
31353135

3136+
def tk_print(self):
3137+
"""Print the contents of the canvas using the native print dialog.
3138+
3139+
Requires Tk 8.7/9.0 or newer."""
3140+
self.tk.call('tk', 'print', self._w)
3141+
31363142
def addtag(self, *args):
31373143
"""Internal function."""
31383144
self.tk.call((self._w, 'addtag') + args)
@@ -4074,6 +4080,12 @@ def __init__(self, master=None, cnf={}, **kw):
40744080
"""
40754081
Widget.__init__(self, master, 'text', cnf, kw)
40764082

4083+
def tk_print(self):
4084+
"""Print the contents of the text widget using the native print dialog.
4085+
4086+
Requires Tk 8.7/9.0 or newer."""
4087+
self.tk.call('tk', 'print', self._w)
4088+
40774089
def bbox(self, index): # overrides Misc.bbox
40784090
"""Return a tuple of (x,y,width,height) which gives the bounding
40794091
box of the visible part of the character at the given index."""
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added the :meth:`!tk_print` method to :class:`tkinter.Canvas` and
2+
:class:`tkinter.Text` which prints the contents of the widget using the
3+
native print dialog. It requires Tk 8.7/9.0 or newer.

0 commit comments

Comments
 (0)