gh-59396: Add use_ttk parameter to tkinter ScrolledText (GH-153119) · python/cpython@fbce45b

GitHub

Original file line numberDiff line numberDiff line change@@ -23,7 +23,16 @@ most normal geometry management behavior.

2323Should more specific control be necessary, the following attributes are

2424available:

252526-.. 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.

273628372938 .. attribute:: frame

Original file line numberDiff line numberDiff line change@@ -456,6 +456,11 @@ tkinter

456456 them.

457457 (Contributed by Serhiy Storchaka in :gh:`59396`.)

458458459+* :class:`tkinter.scrolledtext.ScrolledText` gained a *use_ttk* parameter to use

460+ the themed :mod:`tkinter.ttk` frame and scroll bar instead of the classic

461+:mod:`tkinter` widgets.

462+ (Contributed by Serhiy Storchaka in :gh:`59396`.)

463+459464* :class:`tkinter.font.Font` can now wrap a font description without creating a

460465 new named font, by passing it as *font* with ``exists=True`` and no *name*.

461466 This avoids a loss of precision in :meth:`~tkinter.font.Font.actual`,

Original file line numberDiff line numberDiff line change@@ -1,5 +1,6 @@

11importunittest

22importtkinter

3+fromtkinterimportttk

34fromtkinter.scrolledtextimportScrolledText

45fromtest.supportimportrequires

56fromtest.test_tkinter.supportimportsetUpModule# noqa: F401

@@ -19,15 +20,24 @@ def test_create(self):

1920st=self.create(background='red', height=5)

2021# It is a Text widget held in a Frame together with a Scrollbar.

2122self.assertIsInstance(st, tkinter.Text)

23+# By default the frame and scroll bar are the classic tkinter widgets.

2224self.assertIsInstance(st.frame, tkinter.Frame)

25+self.assertNotIsInstance(st.frame, ttk.Frame)

2326self.assertIsInstance(st.vbar, tkinter.Scrollbar)

27+self.assertNotIsInstance(st.vbar, ttk.Scrollbar)

2428self.assertEqual(st.winfo_parent(), str(st.frame))

2529# str() returns the frame, so that geometry managers manage it.

2630self.assertEqual(str(st), str(st.frame))

2731# Keyword options configure the Text.

2832self.assertEqual(str(st['background']), 'red')

2933self.assertEqual(st['height'], 5ifself.wantobjectselse'5')

303435+deftest_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+3141deftest_text_methods(self):

3242st=self.create()

3343st.insert('1.0', 'hello\nworld')

Original file line numberDiff line numberDiff line change@@ -9,18 +9,25 @@

99the Scrollbar widget.

1010Most methods calls are inherited from the Text widget; Pack, Grid and

1111Place 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"""

131614-fromtkinterimportFrame, Text, Scrollbar, Pack, Grid, Place

17+fromtkinterimportFrame, Text, Scrollbar, Pack, Grid, Place, ttk

1518fromtkinter.constantsimportRIGHT, LEFT, Y, BOTH

16191720__all__= ['ScrolledText']

182119222023classScrolledText(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+ifuse_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)

2431self.vbar.pack(side=RIGHT, fill=Y)

25322633kw['yscrollcommand'] =self.vbar.set

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.