Skip to content

Commit 27148d0

Browse files
gh-151920: Add the ttk.Style.theme_styles() method (GH-151921)
Wrap the Tk 9.0 ``ttk::style theme styles ?themeName?`` subcommand as ttk.Style.theme_styles(themename=None), returning the list of styles defined in a theme (the current theme if themename is omitted). Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 6cbb225 commit 27148d0

5 files changed

Lines changed: 53 additions & 2 deletions

File tree

Doc/library/tkinter.ttk.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,6 +2016,16 @@ If you don't know the class name of a widget, use the method
20162016
Returns a tuple of all known themes.
20172017

20182018

2019+
.. method:: theme_styles(themename=None)
2020+
2021+
Returns a tuple of all styles in *themename*.
2022+
If *themename* is not given, the current theme is used.
2023+
2024+
.. versionadded:: next
2025+
2026+
Availability: Tk 9.0.
2027+
2028+
20192029
.. method:: theme_use(themename=None)
20202030

20212031
If *themename* is not given, returns the theme in use. Otherwise, sets

Doc/whatsnew/3.16.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ tkinter
173173
synchronization of the displayed view with the underlying text.
174174
(Contributed by Serhiy Storchaka in :gh:`151675`.)
175175

176+
* Added the :meth:`ttk.Style.theme_styles
177+
<tkinter.ttk.Style.theme_styles>` method which returns the list of styles
178+
defined in a theme.
179+
(Contributed by Serhiy Storchaka in :gh:`151920`.)
180+
176181
* Added new :class:`!tkinter.Canvas` methods :meth:`~tkinter.Canvas.rchars`
177182
which replaces the text or coordinates of canvas items, and
178183
:meth:`~tkinter.Canvas.rotate` which rotates the coordinates of canvas items.
@@ -211,7 +216,6 @@ tkinter
211216
dithered image when its data was supplied in pieces.
212217
(Contributed by Serhiy Storchaka in :gh:`151888`.)
213218

214-
215219
xml
216220
---
217221

Lib/test/test_ttk/test_style.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from test import support
77
from test.support import requires
88
from test.test_tkinter.support import setUpModule # noqa: F401
9-
from test.test_tkinter.support import AbstractTkTest, get_tk_patchlevel
9+
from test.test_tkinter.support import (AbstractTkTest, get_tk_patchlevel,
10+
requires_tk)
1011

1112
requires('gui')
1213

@@ -124,6 +125,25 @@ def test_theme_use(self):
124125

125126
self.style.theme_use(curr_theme)
126127

128+
@requires_tk(9, 0)
129+
def test_theme_styles(self):
130+
# The 'default' theme is always available and defines the base styles.
131+
default_styles = self.style.theme_styles('default')
132+
self.assertIsInstance(default_styles, tuple)
133+
self.assertIn('.', default_styles)
134+
self.assertIn('TButton', default_styles)
135+
136+
# Without an argument the current theme is used.
137+
styles = self.style.theme_styles()
138+
self.assertIsInstance(styles, tuple)
139+
self.assertIn('.', styles)
140+
141+
for theme in self.style.theme_names():
142+
self.assertIsInstance(self.style.theme_styles(theme), tuple)
143+
144+
self.assertRaises(tkinter.TclError,
145+
self.style.theme_styles, 'nonexistingname')
146+
127147
def test_theme_settings(self):
128148
style = self.style
129149
theme = style.theme_use()

Lib/tkinter/ttk.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,20 @@ def theme_names(self):
492492
return self.tk.splitlist(self.tk.call(self._name, "theme", "names"))
493493

494494

495+
def theme_styles(self, themename=None):
496+
"""Returns a list of all styles in themename.
497+
498+
If themename is omitted, the current theme is used.
499+
500+
Availability: Tk 9.0.
501+
"""
502+
if themename is None:
503+
return self.tk.splitlist(
504+
self.tk.call(self._name, "theme", "styles"))
505+
return self.tk.splitlist(
506+
self.tk.call(self._name, "theme", "styles", themename))
507+
508+
495509
def theme_use(self, themename=None):
496510
"""If themename is None, returns the theme in use, otherwise, set
497511
the current theme to themename, refreshes all widgets and emits
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add the :meth:`ttk.Style.theme_styles <tkinter.ttk.Style.theme_styles>`
2+
method, wrapping the Tk ``ttk::style theme styles`` subcommand, which
3+
returns the list of styles defined in a theme.

0 commit comments

Comments
 (0)