Skip to content

Bootstyle

Helpers for parsing and applying ttkbootstrap "bootstyle" options.

Bootstyle augments ttk widgets with a compact styling API that lets you configure color, orientation, and type with a single string (or tuple) such as "primary-outline", "success", or ("danger", "inverse").

This class provides utilities to parse those tokens from strings and widget state, determine the target widget class and orientation, and resolve the requested color and variant. It also wires ttkbootstrap into tkinter/ttk via setup_ttkbootstrap_api so that widgets accept the bootstyle=... keyword at construction or during configure().

Typical end users will not call these methods directly; they are used internally by the Style engine and by widget constructor overrides.

Source code in src/ttkbootstrap/style.py
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
class Bootstyle:
    """Helpers for parsing and applying ttkbootstrap "bootstyle" options.

    Bootstyle augments ttk widgets with a compact styling API that lets
    you configure color, orientation, and type with a single string (or
    tuple) such as "primary-outline", "success", or ("danger", "inverse").

    This class provides utilities to parse those tokens from strings and
    widget state, determine the target widget class and orientation, and
    resolve the requested color and variant. It also wires ttkbootstrap
    into tkinter/ttk via ``setup_ttkbootstrap_api`` so that widgets accept
    the ``bootstyle=...`` keyword at construction or during configure().

    Typical end users will not call these methods directly; they are used
    internally by the Style engine and by widget constructor overrides.
    """
    @staticmethod
    def ttkstyle_widget_class(widget=None, string=""):
        """Find and return the widget class

        Parameters:

            widget (Widget):
                The widget object.

            string (str):
                A keyword string to parse.

        Returns:

            str:
                A widget class keyword.
        """
        # find widget class from string pattern
        match = re.search(Keywords.CLASS_PATTERN, string.lower())
        if match is not None:
            widget_class = match.group(0)
            return widget_class

        # find widget class from tkinter/tcl method
        if widget is None:
            return ""
        _class = widget.winfo_class()
        match = re.search(Keywords.CLASS_PATTERN, _class.lower())
        if match is not None:
            widget_class = match.group(0)
            return widget_class
        else:
            return ""

    @staticmethod
    def ttkstyle_widget_type(string):
        """Find and return the widget type.

        Parameters:

            string (str):
                A keyword string to parse.

        Returns:

            str:
                A widget type keyword.
        """
        match = re.search(Keywords.TYPE_PATTERN, string.lower())
        if match is None:
            return ""
        else:
            widget_type = match.group(0)
            return widget_type

    @staticmethod
    def ttkstyle_widget_orient(widget=None, string="", **kwargs):
        """Find and return widget orient, or default orient for widget if
        a widget with orientation.

        Parameters:

            widget (Widget):
                The widget object.

            string (str):
                A keyword string to parse.

            **kwargs:
                Optional keyword arguments passed in the widget constructor.

        Returns:

            str:
                A widget orientation keyword.
        """
        # string method (priority)
        match = re.search(Keywords.ORIENT_PATTERN, string)
        widget_orient = ""

        if match is not None:
            widget_orient = match.group(0)
            return widget_orient

        # orient from kwargs
        if "orient" in kwargs:
            _orient = kwargs.pop("orient")
            if _orient == "h":
                widget_orient = "horizontal"
            elif _orient == "v":
                widget_orient = "vertical"
            else:
                widget_orient = _orient
            return widget_orient

        # orient from settings
        if widget is None:
            return widget_orient
        try:
            widget_orient = str(widget.cget("orient"))
        except:
            pass

        return widget_orient

    @staticmethod
    def ttkstyle_widget_color(string):
        """Find and return widget color

        Parameters:

            string (str):
                A keyword string to parse.

        Returns:

            str:
                A color keyword.
        """
        _color = re.search(Keywords.COLOR_PATTERN, string.lower())
        if _color is None:
            return ""
        else:
            widget_color = _color.group(0)
            return widget_color

    @staticmethod
    def ttkstyle_name(widget=None, string="", **kwargs):
        """Parse a string to build and return a ttkstyle name.

        Parameters:

            widget (Widget):
                The widget object.

            string (str):
                A keyword string to parse.

            **kwargs:
                Other keyword arguments to parse widget orientation.

        Returns:

            str:
                A ttk style name
        """
        style_string = "".join(string).lower()
        widget_color = Bootstyle.ttkstyle_widget_color(style_string)
        widget_type = Bootstyle.ttkstyle_widget_type(style_string)
        widget_orient = Bootstyle.ttkstyle_widget_orient(
            widget, style_string, **kwargs
        )
        widget_class = Bootstyle.ttkstyle_widget_class(widget, style_string)

        if widget_color:
            widget_color = f"{widget_color}."

        if widget_type:
            widget_type = f"{widget_type.title()}."

        if widget_orient:
            widget_orient = f"{widget_orient.title()}."

        if widget_class.startswith("t"):
            widget_class = widget_class.title()
        else:
            widget_class = f"T{widget_class.title()}"

        ttkstyle = f"{widget_color}{widget_type}{widget_orient}{widget_class}"
        return ttkstyle

    @staticmethod
    def ttkstyle_method_name(widget=None, string=""):
        """Parse a string to build and return the name of the
        `StyleBuilderTTK` method that creates the ttk style for the
        target widget.

        Parameters:

            widget (Widget):
                The widget object to lookup.

            string (str):
                The keyword string to parse.

        Returns:

            str:
                The name of the update method used to update the widget.
        """
        style_string = "".join(string).lower()
        widget_type = Bootstyle.ttkstyle_widget_type(style_string)
        widget_class = Bootstyle.ttkstyle_widget_class(widget, style_string)

        if widget_type:
            widget_type = f"_{widget_type}"

        if widget_class:
            widget_class = f"_{widget_class}"

        if not widget_type and not widget_class:
            return ""
        else:
            method_name = f"create{widget_type}{widget_class}_style"
            return method_name

    @staticmethod
    def tkupdate_method_name(widget):
        """Lookup the tkinter style update method from the widget class

        Parameters:

            widget (Widget):
                The widget object to lookup.

        Returns:

            str:
                The name of the method used to update the widget object.
        """
        widget_class = Bootstyle.ttkstyle_widget_class(widget)

        if widget_class:
            widget_class = f"_{widget_class}"

        method_name = f"update{widget_class}_style"
        return method_name

    @staticmethod
    def override_ttk_widget_constructor(func):
        """Override widget constructors with bootstyle api options.

        Parameters:

            func (Callable):
                The widget class `__init__` method
        """

        def __init__(self, *args, **kwargs):

            # capture bootstyle and style arguments
            if "bootstyle" in kwargs:
                bootstyle = kwargs.pop("bootstyle")
            else:
                bootstyle = ""

            if "style" in kwargs:
                style = kwargs.pop("style") or ""
            else:
                style = ""

            # instantiate the widget
            func(self, *args, **kwargs)

            # must be called AFTER instantiation in order to use winfo_class
            #    in the `get_ttkstyle_name` method

            if style:
                if Style.get_instance().style_exists_in_theme(style):
                    self.configure(style=style)
                else:
                    ttkstyle = Bootstyle.update_ttk_widget_style(
                        self, style, **kwargs
                    )
                    self.configure(style=ttkstyle)
            elif bootstyle:
                ttkstyle = Bootstyle.update_ttk_widget_style(
                    self, bootstyle, **kwargs
                )
                self.configure(style=ttkstyle)
            else:
                ttkstyle = Bootstyle.update_ttk_widget_style(
                    self, "default", **kwargs
                )
                self.configure(style=ttkstyle)

        return __init__

    @staticmethod
    def override_ttk_widget_configure(func):
        """Overrides the configure method on a ttk widget.

        Parameters:

            func (Callable):
                The widget class `configure` method
        """

        def configure(self, cnf=None, **kwargs):
            # get configuration
            if cnf in ("bootstyle", "style"):
                return self.cget("style")

            if cnf is not None:
                return func(self, cnf)

            # set configuration
            if "bootstyle" in kwargs:
                bootstyle = kwargs.pop("bootstyle")
            else:
                bootstyle = ""

            if "style" in kwargs:
                style = kwargs.get("style")
                ttkstyle = Bootstyle.update_ttk_widget_style(
                    self, style, **kwargs
                )
            elif bootstyle:
                ttkstyle = Bootstyle.update_ttk_widget_style(
                    self, bootstyle, **kwargs
                )
                kwargs.update(style=ttkstyle)

            # update widget configuration
            func(self, cnf, **kwargs)

        return configure

    @staticmethod
    def update_ttk_widget_style(
            widget: ttk.Widget = None, style_string: str = None, **kwargs
    ):
        """Update the ttk style or create if not existing.

        Parameters:

            widget (ttk.Widget):
                The widget instance being updated.

            style_string (str):
                The style string to evalulate. May be the `style`, `ttkstyle`
                or `bootstyle` argument depending on the context and scenario.

            **kwargs:
                Other optional keyword arguments.

        Returns:

            str:
                The ttkstyle or empty string if there is none.
        """
        style: Style = Style.get_instance() or Style()

        # get existing widget style if not provided
        if style_string is None:
            style_string = widget.cget("style")

        # do nothing if the style has not been set
        if not style_string:
            return ""

        if style_string == '.':
            return '.'

        # build style if not existing (example: theme changed)
        ttkstyle = Bootstyle.ttkstyle_name(widget, style_string, **kwargs)
        if not style.style_exists_in_theme(ttkstyle):
            widget_color = Bootstyle.ttkstyle_widget_color(ttkstyle)
            method_name = Bootstyle.ttkstyle_method_name(widget, ttkstyle)
            builder: StyleBuilderTTK = style._get_builder()
            builder_method = builder.name_to_method(method_name)
            builder_method(builder, widget_color)

        # subscribe popdown style to theme changes
        try:
            if widget.winfo_class() == "TCombobox":
                builder: StyleBuilderTTK = style._get_builder()
                winfo_id = hex(widget.winfo_id())
                winfo_pathname = widget.winfo_pathname(winfo_id)
                Publisher.subscribe(
                    name=winfo_pathname,
                    func=lambda w=widget: builder.update_combobox_popdown_style(
                        w
                    ),
                    channel=Channel.STD,
                )
                builder.update_combobox_popdown_style(widget)
        except:
            pass

        return ttkstyle

    @staticmethod
    def setup_ttkbootstrap_api():
        """Setup ttkbootstrap for use with tkinter and ttk. This method
        is called when ttkbootstrap is imported to perform all of the
        necessary method overrides that implement the bootstyle api."""
        from ttkbootstrap.widgets import TTK_WIDGETS
        from ttkbootstrap.widgets import TK_WIDGETS

        # TTK WIDGETS
        for widget in TTK_WIDGETS:
            try:
                # override widget constructor
                _init = Bootstyle.override_ttk_widget_constructor(
                    widget.__init__
                )
                widget.__init__ = _init

                # override configure method
                _configure = Bootstyle.override_ttk_widget_configure(
                    widget.configure
                )
                widget.configure = _configure
                widget.config = widget.configure

                # override get and set methods
                _orig_getitem = widget.__getitem
                _orig_setitem = widget.__setitem

                def __setitem(self, key, val):
                    if key in ("bootstyle", "style"):
                        return _configure(self, **{key: val})
                    return _orig_setitem(key, val)

                def __getitem(self, key):
                    if key in ("bootstyle", "style"):
                        return _configure(self, cnf=key)
                    return _orig_getitem(key)

                if (
                        widget.__name__ != "OptionMenu"
                ):  # this has it's own override
                    widget.__setitem__ = __setitem
                    widget.__getitem__ = __getitem
            except:
                # this may fail in python 3.6 for ttk widgets that do not exist
                #   in that version.
                continue

        # TK WIDGETS
        for widget in TK_WIDGETS:
            # override widget constructor
            _init = Bootstyle.override_tk_widget_constructor(widget.__init__)
            widget.__init__ = _init

    @staticmethod
    def update_tk_widget_style(widget):
        """Lookup the widget name and call the appropriate update
        method

        Parameters:

            widget (object):
                The tcl/tk name given by `tkinter.Widget.winfo_name()`
        """
        try:
            style = Style.get_instance()
            method_name = Bootstyle.tkupdate_method_name(widget)
            builder = style._get_builder_tk()
            builder_method = getattr(StyleBuilderTK, method_name)
            builder_method(builder, widget)
        except:
            """Must pass here to prevent a failure when the user calls
            the `Style`method BEFORE an instance of `Tk` is instantiated.
            This will defer the update of the `Tk` background until the end
            of the `BootStyle` object instantiation (created by the `Style`
            method)"""
            pass

    @staticmethod
    def override_tk_widget_constructor(func):
        """Override widget constructors to apply default style for tk
        widgets.

        Parameters:

            func (Callable):
                The `__init__` method for this widget.
        """

        def __init__wrapper(self, *args, **kwargs):

            # check for autostyle flag
            if "autostyle" in kwargs:
                autostyle = kwargs.pop("autostyle")
            else:
                autostyle = True

            # instantiate the widget
            func(self, *args, **kwargs)

            if autostyle:
                Publisher.subscribe(
                    name=str(self),
                    func=lambda w=self: Bootstyle.update_tk_widget_style(w),
                    channel=Channel.STD,
                )
                Bootstyle.update_tk_widget_style(self)

        return __init__wrapper

override_tk_widget_constructor(func) staticmethod

Override widget constructors to apply default style for tk widgets.

Parameters:

func (Callable):
    The `__init__` method for this widget.
Source code in src/ttkbootstrap/style.py
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
@staticmethod
def override_tk_widget_constructor(func):
    """Override widget constructors to apply default style for tk
    widgets.

    Parameters:

        func (Callable):
            The `__init__` method for this widget.
    """

    def __init__wrapper(self, *args, **kwargs):

        # check for autostyle flag
        if "autostyle" in kwargs:
            autostyle = kwargs.pop("autostyle")
        else:
            autostyle = True

        # instantiate the widget
        func(self, *args, **kwargs)

        if autostyle:
            Publisher.subscribe(
                name=str(self),
                func=lambda w=self: Bootstyle.update_tk_widget_style(w),
                channel=Channel.STD,
            )
            Bootstyle.update_tk_widget_style(self)

    return __init__wrapper

override_ttk_widget_configure(func) staticmethod

Overrides the configure method on a ttk widget.

Parameters:

func (Callable):
    The widget class `configure` method
Source code in src/ttkbootstrap/style.py
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
@staticmethod
def override_ttk_widget_configure(func):
    """Overrides the configure method on a ttk widget.

    Parameters:

        func (Callable):
            The widget class `configure` method
    """

    def configure(self, cnf=None, **kwargs):
        # get configuration
        if cnf in ("bootstyle", "style"):
            return self.cget("style")

        if cnf is not None:
            return func(self, cnf)

        # set configuration
        if "bootstyle" in kwargs:
            bootstyle = kwargs.pop("bootstyle")
        else:
            bootstyle = ""

        if "style" in kwargs:
            style = kwargs.get("style")
            ttkstyle = Bootstyle.update_ttk_widget_style(
                self, style, **kwargs
            )
        elif bootstyle:
            ttkstyle = Bootstyle.update_ttk_widget_style(
                self, bootstyle, **kwargs
            )
            kwargs.update(style=ttkstyle)

        # update widget configuration
        func(self, cnf, **kwargs)

    return configure

override_ttk_widget_constructor(func) staticmethod

Override widget constructors with bootstyle api options.

Parameters:

func (Callable):
    The widget class `__init__` method
Source code in src/ttkbootstrap/style.py
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
@staticmethod
def override_ttk_widget_constructor(func):
    """Override widget constructors with bootstyle api options.

    Parameters:

        func (Callable):
            The widget class `__init__` method
    """

    def __init__(self, *args, **kwargs):

        # capture bootstyle and style arguments
        if "bootstyle" in kwargs:
            bootstyle = kwargs.pop("bootstyle")
        else:
            bootstyle = ""

        if "style" in kwargs:
            style = kwargs.pop("style") or ""
        else:
            style = ""

        # instantiate the widget
        func(self, *args, **kwargs)

        # must be called AFTER instantiation in order to use winfo_class
        #    in the `get_ttkstyle_name` method

        if style:
            if Style.get_instance().style_exists_in_theme(style):
                self.configure(style=style)
            else:
                ttkstyle = Bootstyle.update_ttk_widget_style(
                    self, style, **kwargs
                )
                self.configure(style=ttkstyle)
        elif bootstyle:
            ttkstyle = Bootstyle.update_ttk_widget_style(
                self, bootstyle, **kwargs
            )
            self.configure(style=ttkstyle)
        else:
            ttkstyle = Bootstyle.update_ttk_widget_style(
                self, "default", **kwargs
            )
            self.configure(style=ttkstyle)

    return __init__

setup_ttkbootstrap_api() staticmethod

Setup ttkbootstrap for use with tkinter and ttk. This method is called when ttkbootstrap is imported to perform all of the necessary method overrides that implement the bootstyle api.

Source code in src/ttkbootstrap/style.py
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
@staticmethod
def setup_ttkbootstrap_api():
    """Setup ttkbootstrap for use with tkinter and ttk. This method
    is called when ttkbootstrap is imported to perform all of the
    necessary method overrides that implement the bootstyle api."""
    from ttkbootstrap.widgets import TTK_WIDGETS
    from ttkbootstrap.widgets import TK_WIDGETS

    # TTK WIDGETS
    for widget in TTK_WIDGETS:
        try:
            # override widget constructor
            _init = Bootstyle.override_ttk_widget_constructor(
                widget.__init__
            )
            widget.__init__ = _init

            # override configure method
            _configure = Bootstyle.override_ttk_widget_configure(
                widget.configure
            )
            widget.configure = _configure
            widget.config = widget.configure

            # override get and set methods
            _orig_getitem = widget.__getitem
            _orig_setitem = widget.__setitem

            def __setitem(self, key, val):
                if key in ("bootstyle", "style"):
                    return _configure(self, **{key: val})
                return _orig_setitem(key, val)

            def __getitem(self, key):
                if key in ("bootstyle", "style"):
                    return _configure(self, cnf=key)
                return _orig_getitem(key)

            if (
                    widget.__name__ != "OptionMenu"
            ):  # this has it's own override
                widget.__setitem__ = __setitem
                widget.__getitem__ = __getitem
        except:
            # this may fail in python 3.6 for ttk widgets that do not exist
            #   in that version.
            continue

    # TK WIDGETS
    for widget in TK_WIDGETS:
        # override widget constructor
        _init = Bootstyle.override_tk_widget_constructor(widget.__init__)
        widget.__init__ = _init

tkupdate_method_name(widget) staticmethod

Lookup the tkinter style update method from the widget class

Parameters:

widget (Widget):
    The widget object to lookup.

Returns:

str:
    The name of the method used to update the widget object.
Source code in src/ttkbootstrap/style.py
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
@staticmethod
def tkupdate_method_name(widget):
    """Lookup the tkinter style update method from the widget class

    Parameters:

        widget (Widget):
            The widget object to lookup.

    Returns:

        str:
            The name of the method used to update the widget object.
    """
    widget_class = Bootstyle.ttkstyle_widget_class(widget)

    if widget_class:
        widget_class = f"_{widget_class}"

    method_name = f"update{widget_class}_style"
    return method_name

ttkstyle_method_name(widget=None, string='') staticmethod

Parse a string to build and return the name of the StyleBuilderTTK method that creates the ttk style for the target widget.

Parameters:

widget (Widget):
    The widget object to lookup.

string (str):
    The keyword string to parse.

Returns:

str:
    The name of the update method used to update the widget.
Source code in src/ttkbootstrap/style.py
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
@staticmethod
def ttkstyle_method_name(widget=None, string=""):
    """Parse a string to build and return the name of the
    `StyleBuilderTTK` method that creates the ttk style for the
    target widget.

    Parameters:

        widget (Widget):
            The widget object to lookup.

        string (str):
            The keyword string to parse.

    Returns:

        str:
            The name of the update method used to update the widget.
    """
    style_string = "".join(string).lower()
    widget_type = Bootstyle.ttkstyle_widget_type(style_string)
    widget_class = Bootstyle.ttkstyle_widget_class(widget, style_string)

    if widget_type:
        widget_type = f"_{widget_type}"

    if widget_class:
        widget_class = f"_{widget_class}"

    if not widget_type and not widget_class:
        return ""
    else:
        method_name = f"create{widget_type}{widget_class}_style"
        return method_name

ttkstyle_name(widget=None, string='', **kwargs) staticmethod

Parse a string to build and return a ttkstyle name.

Parameters:

widget (Widget):
    The widget object.

string (str):
    A keyword string to parse.

**kwargs:
    Other keyword arguments to parse widget orientation.

Returns:

str:
    A ttk style name
Source code in src/ttkbootstrap/style.py
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
@staticmethod
def ttkstyle_name(widget=None, string="", **kwargs):
    """Parse a string to build and return a ttkstyle name.

    Parameters:

        widget (Widget):
            The widget object.

        string (str):
            A keyword string to parse.

        **kwargs:
            Other keyword arguments to parse widget orientation.

    Returns:

        str:
            A ttk style name
    """
    style_string = "".join(string).lower()
    widget_color = Bootstyle.ttkstyle_widget_color(style_string)
    widget_type = Bootstyle.ttkstyle_widget_type(style_string)
    widget_orient = Bootstyle.ttkstyle_widget_orient(
        widget, style_string, **kwargs
    )
    widget_class = Bootstyle.ttkstyle_widget_class(widget, style_string)

    if widget_color:
        widget_color = f"{widget_color}."

    if widget_type:
        widget_type = f"{widget_type.title()}."

    if widget_orient:
        widget_orient = f"{widget_orient.title()}."

    if widget_class.startswith("t"):
        widget_class = widget_class.title()
    else:
        widget_class = f"T{widget_class.title()}"

    ttkstyle = f"{widget_color}{widget_type}{widget_orient}{widget_class}"
    return ttkstyle

ttkstyle_widget_class(widget=None, string='') staticmethod

Find and return the widget class

Parameters:

widget (Widget):
    The widget object.

string (str):
    A keyword string to parse.

Returns:

str:
    A widget class keyword.
Source code in src/ttkbootstrap/style.py
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
@staticmethod
def ttkstyle_widget_class(widget=None, string=""):
    """Find and return the widget class

    Parameters:

        widget (Widget):
            The widget object.

        string (str):
            A keyword string to parse.

    Returns:

        str:
            A widget class keyword.
    """
    # find widget class from string pattern
    match = re.search(Keywords.CLASS_PATTERN, string.lower())
    if match is not None:
        widget_class = match.group(0)
        return widget_class

    # find widget class from tkinter/tcl method
    if widget is None:
        return ""
    _class = widget.winfo_class()
    match = re.search(Keywords.CLASS_PATTERN, _class.lower())
    if match is not None:
        widget_class = match.group(0)
        return widget_class
    else:
        return ""

ttkstyle_widget_color(string) staticmethod

Find and return widget color

Parameters:

string (str):
    A keyword string to parse.

Returns:

str:
    A color keyword.
Source code in src/ttkbootstrap/style.py
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
@staticmethod
def ttkstyle_widget_color(string):
    """Find and return widget color

    Parameters:

        string (str):
            A keyword string to parse.

    Returns:

        str:
            A color keyword.
    """
    _color = re.search(Keywords.COLOR_PATTERN, string.lower())
    if _color is None:
        return ""
    else:
        widget_color = _color.group(0)
        return widget_color

ttkstyle_widget_orient(widget=None, string='', **kwargs) staticmethod

Find and return widget orient, or default orient for widget if a widget with orientation.

Parameters:

widget (Widget):
    The widget object.

string (str):
    A keyword string to parse.

**kwargs:
    Optional keyword arguments passed in the widget constructor.

Returns:

str:
    A widget orientation keyword.
Source code in src/ttkbootstrap/style.py
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
@staticmethod
def ttkstyle_widget_orient(widget=None, string="", **kwargs):
    """Find and return widget orient, or default orient for widget if
    a widget with orientation.

    Parameters:

        widget (Widget):
            The widget object.

        string (str):
            A keyword string to parse.

        **kwargs:
            Optional keyword arguments passed in the widget constructor.

    Returns:

        str:
            A widget orientation keyword.
    """
    # string method (priority)
    match = re.search(Keywords.ORIENT_PATTERN, string)
    widget_orient = ""

    if match is not None:
        widget_orient = match.group(0)
        return widget_orient

    # orient from kwargs
    if "orient" in kwargs:
        _orient = kwargs.pop("orient")
        if _orient == "h":
            widget_orient = "horizontal"
        elif _orient == "v":
            widget_orient = "vertical"
        else:
            widget_orient = _orient
        return widget_orient

    # orient from settings
    if widget is None:
        return widget_orient
    try:
        widget_orient = str(widget.cget("orient"))
    except:
        pass

    return widget_orient

ttkstyle_widget_type(string) staticmethod

Find and return the widget type.

Parameters:

string (str):
    A keyword string to parse.

Returns:

str:
    A widget type keyword.
Source code in src/ttkbootstrap/style.py
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
@staticmethod
def ttkstyle_widget_type(string):
    """Find and return the widget type.

    Parameters:

        string (str):
            A keyword string to parse.

    Returns:

        str:
            A widget type keyword.
    """
    match = re.search(Keywords.TYPE_PATTERN, string.lower())
    if match is None:
        return ""
    else:
        widget_type = match.group(0)
        return widget_type

update_tk_widget_style(widget) staticmethod

Lookup the widget name and call the appropriate update method

Parameters:

widget (object):
    The tcl/tk name given by `tkinter.Widget.winfo_name()`
Source code in src/ttkbootstrap/style.py
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
@staticmethod
def update_tk_widget_style(widget):
    """Lookup the widget name and call the appropriate update
    method

    Parameters:

        widget (object):
            The tcl/tk name given by `tkinter.Widget.winfo_name()`
    """
    try:
        style = Style.get_instance()
        method_name = Bootstyle.tkupdate_method_name(widget)
        builder = style._get_builder_tk()
        builder_method = getattr(StyleBuilderTK, method_name)
        builder_method(builder, widget)
    except:
        """Must pass here to prevent a failure when the user calls
        the `Style`method BEFORE an instance of `Tk` is instantiated.
        This will defer the update of the `Tk` background until the end
        of the `BootStyle` object instantiation (created by the `Style`
        method)"""
        pass

update_ttk_widget_style(widget=None, style_string=None, **kwargs) staticmethod

Update the ttk style or create if not existing.

Parameters:

widget (ttk.Widget):
    The widget instance being updated.

style_string (str):
    The style string to evalulate. May be the `style`, `ttkstyle`
    or `bootstyle` argument depending on the context and scenario.

**kwargs:
    Other optional keyword arguments.

Returns:

str:
    The ttkstyle or empty string if there is none.
Source code in src/ttkbootstrap/style.py
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
@staticmethod
def update_ttk_widget_style(
        widget: ttk.Widget = None, style_string: str = None, **kwargs
):
    """Update the ttk style or create if not existing.

    Parameters:

        widget (ttk.Widget):
            The widget instance being updated.

        style_string (str):
            The style string to evalulate. May be the `style`, `ttkstyle`
            or `bootstyle` argument depending on the context and scenario.

        **kwargs:
            Other optional keyword arguments.

    Returns:

        str:
            The ttkstyle or empty string if there is none.
    """
    style: Style = Style.get_instance() or Style()

    # get existing widget style if not provided
    if style_string is None:
        style_string = widget.cget("style")

    # do nothing if the style has not been set
    if not style_string:
        return ""

    if style_string == '.':
        return '.'

    # build style if not existing (example: theme changed)
    ttkstyle = Bootstyle.ttkstyle_name(widget, style_string, **kwargs)
    if not style.style_exists_in_theme(ttkstyle):
        widget_color = Bootstyle.ttkstyle_widget_color(ttkstyle)
        method_name = Bootstyle.ttkstyle_method_name(widget, ttkstyle)
        builder: StyleBuilderTTK = style._get_builder()
        builder_method = builder.name_to_method(method_name)
        builder_method(builder, widget_color)

    # subscribe popdown style to theme changes
    try:
        if widget.winfo_class() == "TCombobox":
            builder: StyleBuilderTTK = style._get_builder()
            winfo_id = hex(widget.winfo_id())
            winfo_pathname = widget.winfo_pathname(winfo_id)
            Publisher.subscribe(
                name=winfo_pathname,
                func=lambda w=widget: builder.update_combobox_popdown_style(
                    w
                ),
                channel=Channel.STD,
            )
            builder.update_combobox_popdown_style(widget)
    except:
        pass

    return ttkstyle