Building a theme selector

Themes aren't something users are going to need to change often, and as we've seen, changing the theme can undo style changes we've made to our widgets. In light of this, we'll play it safe by designing our theme changer in such a way that it requires a restart of the program to make the actual change.

We'll start by adding a theme option to our SettingsModel:

    variables = {
        ...
        'theme': {'type': 'str', 'value': 'default'}
    }

Every platform has a theme aliased to default, so this is a safe and sensible default value.

Next, our Application.__init__() method will need to check this value and set the theme accordingly.

Add this code just after the call to load_settings():

 style = ttk.Style() theme = self.settings.get('theme').get() ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.