rdbende / Forest-ttk-theme

A beautiful modern theme for ttk, inspired by MS Excel's look 🌲🌳
MIT License
382 stars 97 forks source link

Changing between light and dark mode #1

Closed Dhierin closed 3 years ago

Dhierin commented 3 years ago

Hi, first of all, I'd like to thank you for the nice theme. I like it a lot, I am currently trying to make a button to switch from one theme to the other. I have my default set at the dark forest theme. Using the code attached I am trying to change the themes. I am able to change from dark to light mode. However, when trying to change from light back to dark, I get the following error: in change_theme root.tk.call('source', tclfile) _tkinter.TclError: Theme forest-dark already exists. What am I missing?

tclfile='forest-dark.tcl'
themename='forest-dark'
root.tk.call('source', tclfile)
ttk.Style().theme_use(themename)

def change_theme():
    global themename, tclfile
    if themename == 'forest-dark':
        tclfile = 'forest-light.tcl'
        themename = 'forest-light'
        root.tk.call('source', tclfile)
        ttk.Style().theme_use(themename)
        print(themename)
    elif themename=='forest-light':
        themename = 'forest-dark'
        tclfile = 'forest-dark.tcl'

        root.tk.call('source', tclfile)
        ttk.Style().theme_use(themename)
rdbende commented 3 years ago

For my Azure and Sun Valley themes I implemented an easy way to switch between themes, without any problem.

You get the error, because the theme is already exists, and it cannot be created again. Below you can find the solution on how to change the theme with this theme, but unfortunately some colors won't change. In the future, I'll create a method for this topic to switch themes without any problems, but I won’t deal with that yet.

themename='forest-dark'
root.tk.call('source', "forest-light.tcl")
root.tk.call('source', "forest-dark.tcl")
ttk.Style().theme_use(themename)

def change_theme():
    global themename
    if themename == 'forest-dark':
        themename = 'forest-light'
        ttk.Style().theme_use(themename)
        print(themename)
    elif themename=='forest-light':
        themename = 'forest-dark'
        ttk.Style().theme_use(themename)