Skip to content

Change theme dynamically on the fly #2437

@firedm

Description

@firedm

Type of Issues (Enhancement, Error, Bug, Question): Enhancement

Operating System: windows

Python version: 3.6

PySimpleGUI Port and Version: tkinter

Hello Mike,
This is not an urgent request, I know you are busy so you can delay it when you have time.

Is it possible to change theme while window is running? i.e. without the need for restarting the window?

my current theme implementation is to
1 - close active window
2- call ChangeLookAndFeel('new_theme_name')
3- recreate window
4 - start window read

sometimes restarting the window consume time plus it would be more elegant to change theme and see the results instantly

i was playing with pysimplegui and tkinter code, and i came up with this result so far, it is horrible because i never used tkinter before

below is an example, where you can select theme and it will iterate over all window elements and change colors according to the selected theme, however it doesn't work as expected
the window background doesn't fill completely around other elements, i think i am missing something,

please have a look, as you are the expert here who can come up with an acceptable implementation


import PySimpleGUI as sg

themes = sg.ListOfLookAndFeelValues()
selected_theme = 'Reds'
current_them = sg.LOOK_AND_FEEL_TABLE[selected_theme]
sg.ChangeLookAndFeel(selected_theme)

layout = [
        [sg.T('User Setting:')],
        [sg.Text('Select Theme:'), 
        sg.Combo(values=themes, default_value=selected_theme, size=(15, 1), enable_events=True, key='select_theme')],
        [sg.I('this is input')], 
        [sg.B('Hello'), sg.Button(' about ', key='about')]
]

window = sg.Window('', layout=layout)

while True:
    e, v= window()
    if e is None:
        break
       
    elif e == 'select_theme':
        
            selected_theme = v['select_theme']
            print(selected_theme)
            current_them = sg.LOOK_AND_FEEL_TABLE[selected_theme]

            try:
                window_bkg = current_them.get('BACKGROUND')
                window.TKroot.config(background=window_bkg)
            except Exception as e:
                    print(e)

             # iterate over all widgets:
            for v, element in window.AllKeysDict.items():
            # for child in window.TKroot.frame.children.values():
                
                try:
                    color = current_them.get(element.Type.upper())
                    if color:
                        if element.Type == 'button':
                            element.Widget.config(foreground=color[0], background=color[1])
                        else:
                            element.Widget.config(background=color)

                        element.update()
                except Exception as e:
                    print(e)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions