]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-151920: Add the ttk.Style.theme_styles() method (GH-151921)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 22 Jun 2026 17:43:43 +0000 (20:43 +0300)
committerGitHub <noreply@github.com>
Mon, 22 Jun 2026 17:43:43 +0000 (20:43 +0300)
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>
Doc/library/tkinter.ttk.rst
Doc/whatsnew/3.16.rst
Lib/test/test_ttk/test_style.py
Lib/tkinter/ttk.py
Misc/NEWS.d/next/Library/2026-06-22-14-30-00.gh-issue-151920.Th3mSt.rst [new file with mode: 0644]

index fc79c7fa1845d5a5fd70f4cc50354798e54dcd93..b375b6908679de39af4dc0e94a5712af8270211a 100644 (file)
@@ -2016,6 +2016,16 @@ If you don't know the class name of a widget, use the method
       Returns a tuple of all known themes.
 
 
+   .. method:: theme_styles(themename=None)
+
+      Returns a tuple of all styles in *themename*.
+      If *themename* is not given, the current theme is used.
+
+      .. versionadded:: next
+
+      Availability: Tk 9.0.
+
+
    .. method:: theme_use(themename=None)
 
       If *themename* is not given, returns the theme in use.  Otherwise, sets
index c5d73ede9224ff56e969a411065eb8e4b08b71f9..f394298da96a7cda90ef91e4fd2c42fe02320ba2 100644 (file)
@@ -173,6 +173,11 @@ tkinter
   synchronization of the displayed view with the underlying text.
   (Contributed by Serhiy Storchaka in :gh:`151675`.)
 
+* Added the :meth:`ttk.Style.theme_styles
+  <tkinter.ttk.Style.theme_styles>` method which returns the list of styles
+  defined in a theme.
+  (Contributed by Serhiy Storchaka in :gh:`151920`.)
+
 * Added new :class:`!tkinter.Canvas` methods :meth:`~tkinter.Canvas.rchars`
   which replaces the text or coordinates of canvas items, and
   :meth:`~tkinter.Canvas.rotate` which rotates the coordinates of canvas items.
@@ -211,7 +216,6 @@ tkinter
   dithered image when its data was supplied in pieces.
   (Contributed by Serhiy Storchaka in :gh:`151888`.)
 
-
 xml
 ---
 
index f85f76eb499278337b3456765bf644960ccd29d0..6fbcfbdc9b9e4f5730753d4fbdbb5aaead7373f7 100644 (file)
@@ -6,7 +6,8 @@ from tkinter import TclError
 from test import support
 from test.support import requires
 from test.test_tkinter.support import setUpModule  # noqa: F401
-from test.test_tkinter.support import AbstractTkTest, get_tk_patchlevel
+from test.test_tkinter.support import (AbstractTkTest, get_tk_patchlevel,
+                                       requires_tk)
 
 requires('gui')
 
@@ -124,6 +125,25 @@ class StyleTest(AbstractTkTest, unittest.TestCase):
 
         self.style.theme_use(curr_theme)
 
+    @requires_tk(9, 0)
+    def test_theme_styles(self):
+        # The 'default' theme is always available and defines the base styles.
+        default_styles = self.style.theme_styles('default')
+        self.assertIsInstance(default_styles, tuple)
+        self.assertIn('.', default_styles)
+        self.assertIn('TButton', default_styles)
+
+        # Without an argument the current theme is used.
+        styles = self.style.theme_styles()
+        self.assertIsInstance(styles, tuple)
+        self.assertIn('.', styles)
+
+        for theme in self.style.theme_names():
+            self.assertIsInstance(self.style.theme_styles(theme), tuple)
+
+        self.assertRaises(tkinter.TclError,
+                          self.style.theme_styles, 'nonexistingname')
+
     def test_theme_settings(self):
         style = self.style
         theme = style.theme_use()
index c3a5ac160573a6318bc6a6b0ab0c83ea22e69d14..0cabc5c3140fd6b0495d1b8cb60cc0c7e43e4740 100644 (file)
@@ -492,6 +492,20 @@ class Style(object):
         return self.tk.splitlist(self.tk.call(self._name, "theme", "names"))
 
 
+    def theme_styles(self, themename=None):
+        """Returns a list of all styles in themename.
+
+        If themename is omitted, the current theme is used.
+
+        Availability: Tk 9.0.
+        """
+        if themename is None:
+            return self.tk.splitlist(
+                self.tk.call(self._name, "theme", "styles"))
+        return self.tk.splitlist(
+            self.tk.call(self._name, "theme", "styles", themename))
+
+
     def theme_use(self, themename=None):
         """If themename is None, returns the theme in use, otherwise, set
         the current theme to themename, refreshes all widgets and emits
diff --git a/Misc/NEWS.d/next/Library/2026-06-22-14-30-00.gh-issue-151920.Th3mSt.rst b/Misc/NEWS.d/next/Library/2026-06-22-14-30-00.gh-issue-151920.Th3mSt.rst
new file mode 100644 (file)
index 0000000..2c4a069
--- /dev/null
@@ -0,0 +1,3 @@
+Add the :meth:`ttk.Style.theme_styles <tkinter.ttk.Style.theme_styles>`
+method, wrapping the Tk ``ttk::style theme styles`` subcommand, which
+returns the list of styles defined in a theme.