pythontkintertkinter-layoutttkthemes

Layouts from used theme not available in class App(tk.Tk) but works in plain tk.Tk


I use a theme called "forest-light", implemented as suggested from its author on github and in general it's working great until I tried to use several layouts for other widgets:

import tkinter as tk
from tkinter import ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        ttk.Checkbutton(self, text="Checkbutton", style="ToggleButton").pack()


root = App()

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

root.mainloop()

I always get the Error:

_tkinter.TclError: Layout ToggleButton not found

If I do the same thing without a class, it works as should:

import tkinter as tk
from tkinter import ttk


root = tk.Tk()

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

ttk.Checkbutton(root, text="Checkbutton", style="ToggleButton").pack()

root.mainloop()

Any insight or help would be highly appreciated.


Solution

  • In the first case you're trying to use the ToggleButton style before you import the style and use it:

    root = App()   # this is where you use the style
    root.tk.call('source', 'forest-light.tcl')  # this is where you import it.
    

    In the second case, you're importing the style before you create the ttk.Checkbutton.

    root.tk.call('source', 'forest-light.tcl')  # this is where you import it
    ttk.Checkbutton(..., style="ToggleButton")  # this is where you use it.