Skip to content

Commit aa50e0b

Browse files
gh-153256: Add tk_print() methods to tkinter Canvas and Text
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 2092192 commit aa50e0b

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
@@ -3546,6 +3546,13 @@ Widget classes
35463546
A newly created item is placed at the top of the list; the order can be
35473547
changed with :meth:`tag_raise` and :meth:`tag_lower`.
35483548

3549+
.. method:: tk_print()
3550+
3551+
Print the contents of the canvas using the native print dialog.
3552+
Requires Tk 8.7/9.0 or newer.
3553+
3554+
.. versionadded:: next
3555+
35493556
.. method:: create_arc(*args, **kw)
35503557
create_bitmap(*args, **kw)
35513558
create_image(*args, **kw)
@@ -5357,6 +5364,13 @@ Widget classes
53575364
to its base; several modifiers may be combined and are applied from left to
53585365
right, for example ``'insert wordstart - 1 c'``.
53595366

5367+
.. method:: tk_print()
5368+
5369+
Print the contents of the text widget using the native print dialog.
5370+
Requires Tk 8.7/9.0 or newer.
5371+
5372+
.. versionadded:: next
5373+
53605374
.. method:: insert(index, chars, *args)
53615375

53625376
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
@@ -358,6 +358,11 @@ tkinter
358358
report the user idle time.
359359
(Contributed by Serhiy Storchaka in :gh:`151881`.)
360360

361+
* Added the :meth:`!tk_print` method to :class:`tkinter.Canvas` and
362+
:class:`tkinter.Text` which prints the contents of the widget using the
363+
native print dialog. It requires Tk 8.7/9.0 or newer.
364+
(Contributed by Serhiy Storchaka in :gh:`153256`.)
365+
361366
* Added new window-management methods :meth:`~tkinter.Misc.winfo_isdark`
362367
(dark mode detection), :meth:`~tkinter.Wm.wm_iconbadge` (application icon
363368
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
@@ -599,6 +599,15 @@ def test_tk_inactive(self):
599599
# Resetting the timer returns None and does not raise.
600600
self.assertIsNone(self.root.tk_inactive(reset=True))
601601

602+
def test_tk_print(self):
603+
# tk print supports only canvas and text widgets, so tk_print is a
604+
# method of Canvas and Text only. Calling it opens the print
605+
# dialog, so the behavior itself cannot be tested here.
606+
self.assertHasAttr(tkinter.Canvas, 'tk_print')
607+
self.assertHasAttr(tkinter.Text, 'tk_print')
608+
self.assertNotHasAttr(tkinter.Frame, 'tk_print')
609+
self.assertNotHasAttr(tkinter.Misc, 'tk_print')
610+
602611
def test_wait_variable(self):
603612
var = tkinter.StringVar(self.root)
604613
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
@@ -3127,6 +3127,12 @@ def __init__(self, master=None, cnf={}, **kw):
31273127
yscrollcommand, yscrollincrement."""
31283128
Widget.__init__(self, master, 'canvas', cnf, kw)
31293129

3130+
def tk_print(self):
3131+
"""Print the contents of the canvas using the native print dialog.
3132+
3133+
Requires Tk 8.7/9.0 or newer."""
3134+
self.tk.call('tk', 'print', self._w)
3135+
31303136
def addtag(self, *args):
31313137
"""Internal function."""
31323138
self.tk.call((self._w, 'addtag') + args)
@@ -4068,6 +4074,12 @@ def __init__(self, master=None, cnf={}, **kw):
40684074
"""
40694075
Widget.__init__(self, master, 'text', cnf, kw)
40704076

4077+
def tk_print(self):
4078+
"""Print the contents of the text widget using the native print dialog.
4079+
4080+
Requires Tk 8.7/9.0 or newer."""
4081+
self.tk.call('tk', 'print', self._w)
4082+
40714083
def bbox(self, index): # overrides Misc.bbox
40724084
"""Return a tuple of (x,y,width,height) which gives the bounding
40734085
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)