Setting a custom colour theme in customtkinter

In a project, I am trying to change the colour template of my project from a list of pre-loaded .json themes saved to a folder user_themes. The themes are Anthracite.json, Cobalt.json and Blue.json. A drop-down menu is used to select the color and the template should load.

I tried this:

from customtkinter import CTkLabel, CTkButton, CTkOptionMenu, set_default_color_theme, CTk
root = CTk()
root.geometry("400x400")


def click():
    CTkLabel(root, text="Here is some text").pack(pady=20)


def new_color_theme(nct: str):
    print(nct)
    set_default_color_theme(f"user_themes/{nct}.json")


btn = CTkButton(root, text="Click me", command=click).pack(pady=20)
lst = ["Anthracite", "Cobalt", "Blue"]
menu = CTkOptionMenu(root, values=lst, command=new_color_theme).pack(pady=20)
root.mainloop()

But this is not working. The theme is getting printed but is not reflected in the window. What could be the reason for this and any suggestions to correct it?

Upvotes: 0

Views: 859

Answers (1)

Mike 1212
Mike 1212

Reputation: 11

Ok, the problem here is that the function is customtkinter's. So this is your script:

set_default_color_theme(f"user_themes/{nct}.json")

It doesn't work, instead use:

import customtkinter
customtkinter.set_default_color_theme(f"user_themes/{nct}.json")

Upvotes: 0

Related Questions