Skip to content

Commit f401cff

Browse files
gh-59396: Add use_ttk parameter to tkinter ScrolledText
ScrolledText gained a keyword-only use_ttk parameter to build the surrounding frame and the vertical scroll bar from the themed tkinter.ttk widgets instead of the classic tkinter widgets. The classic widgets remain the default. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 3d9d6c8 commit f401cff

5 files changed

Lines changed: 39 additions & 5 deletions

File tree

Doc/library/tkinter.scrolledtext.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@ most normal geometry management behavior.
2323
Should more specific control be necessary, the following attributes are
2424
available:
2525

26-
.. class:: ScrolledText(master=None, **kw)
26+
.. class:: ScrolledText(master=None, *, use_ttk=False, **kw)
27+
28+
The keyword arguments are passed to the :class:`~tkinter.Text` widget.
29+
30+
When *use_ttk* is true, the surrounding frame and the scroll bar are the
31+
themed :mod:`tkinter.ttk` widgets;
32+
the default is the classic :mod:`tkinter` widgets.
33+
34+
.. versionchanged:: next
35+
Added the *use_ttk* parameter.
2736

2837

2938
.. attribute:: frame

Doc/whatsnew/3.16.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ tkinter
391391
them.
392392
(Contributed by Serhiy Storchaka in :gh:`59396`.)
393393

394+
* :class:`tkinter.scrolledtext.ScrolledText` gained a *use_ttk* parameter to use
395+
the themed :mod:`tkinter.ttk` frame and scroll bar instead of the classic
396+
:mod:`tkinter` widgets.
397+
(Contributed by Serhiy Storchaka in :gh:`59396`.)
398+
394399
xml
395400
---
396401

Lib/test/test_tkinter/test_scrolledtext.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import tkinter
3+
from tkinter import ttk
34
from tkinter.scrolledtext import ScrolledText
45
from test.support import requires
56
from test.test_tkinter.support import setUpModule # noqa: F401
@@ -19,15 +20,24 @@ def test_create(self):
1920
st = self.create(background='red', height=5)
2021
# It is a Text widget held in a Frame together with a Scrollbar.
2122
self.assertIsInstance(st, tkinter.Text)
23+
# By default the frame and scroll bar are the classic tkinter widgets.
2224
self.assertIsInstance(st.frame, tkinter.Frame)
25+
self.assertNotIsInstance(st.frame, ttk.Frame)
2326
self.assertIsInstance(st.vbar, tkinter.Scrollbar)
27+
self.assertNotIsInstance(st.vbar, ttk.Scrollbar)
2428
self.assertEqual(st.winfo_parent(), str(st.frame))
2529
# str() returns the frame, so that geometry managers manage it.
2630
self.assertEqual(str(st), str(st.frame))
2731
# Keyword options configure the Text.
2832
self.assertEqual(str(st['background']), 'red')
2933
self.assertEqual(st['height'], 5 if self.wantobjects else '5')
3034

35+
def test_use_ttk(self):
36+
# use_ttk=True uses the themed tkinter.ttk widgets.
37+
st = self.create(use_ttk=True)
38+
self.assertIsInstance(st.frame, ttk.Frame)
39+
self.assertIsInstance(st.vbar, ttk.Scrollbar)
40+
3141
def test_text_methods(self):
3242
st = self.create()
3343
st.insert('1.0', 'hello\nworld')

Lib/tkinter/scrolledtext.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@
99
the Scrollbar widget.
1010
Most methods calls are inherited from the Text widget; Pack, Grid and
1111
Place methods are redirected to the Frame widget however.
12+
13+
Pass use_ttk=True for the themed tkinter.ttk frame and scroll bar
14+
instead of the classic tkinter widgets.
1215
"""
1316

14-
from tkinter import Frame, Text, Scrollbar, Pack, Grid, Place
17+
from tkinter import Frame, Text, Scrollbar, Pack, Grid, Place, ttk
1518
from tkinter.constants import RIGHT, LEFT, Y, BOTH
1619

1720
__all__ = ['ScrolledText']
1821

1922

2023
class ScrolledText(Text):
21-
def __init__(self, master=None, **kw):
22-
self.frame = Frame(master)
23-
self.vbar = Scrollbar(self.frame)
24+
def __init__(self, master=None, *, use_ttk=False, **kw):
25+
if use_ttk:
26+
self.frame = ttk.Frame(master)
27+
self.vbar = ttk.Scrollbar(self.frame)
28+
else:
29+
self.frame = Frame(master)
30+
self.vbar = Scrollbar(self.frame)
2431
self.vbar.pack(side=RIGHT, fill=Y)
2532

2633
kw['yscrollcommand'] = self.vbar.set
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`tkinter.scrolledtext.ScrolledText` gained a *use_ttk* parameter to use
2+
the themed :mod:`tkinter.ttk` frame and scroll bar instead of the classic
3+
:mod:`tkinter` widgets.

0 commit comments

Comments
 (0)