Replies: 1 comment 3 replies
-
Good point. Right now, once the widget is created the only way to change a particular component theme is by accessing its inner state with configure and changing every aspect of it. What you can do is to verify what attribute colors you want to change: The code below is an atempt to help other people and maybe it can help to customize an element faster: #python std
import sys
sys.path.append("/home/user/Repositories/CustomTkinter/")
import os
import json
import tkinter
#third-party
import customtkinter
from customtkinter import ThemeManager, CTkButton
# theme initialization
THEME = "sweetkind"
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme(THEME)
# get theme by name
class ThemeUtl:
@staticmethod
def get_theme_by_name(theme_dir,theme_name : str):
with open(os.path.join(theme_dir, f"{theme_name}.json"), "r") as f:
return json.load(f)
class CTkButtonStyle:
@staticmethod
def customize(element : CTkButton, color_theme : str):
theme_json = ThemeUtl.get_theme_by_name("/home/user/Repositories/CustomTkinter/assets/themes/", color_theme)
element.configure(bg_color = theme_json["color"]["button"])
# do the same for each button property
# .........
# element.configure(fg_color="default_theme")
# element.configure(hover_color="default_theme")
# element.configure(bg_active="default_theme")
# element.configure(border_color="default_theme")
# element.configure(border_width="default_theme")
# element.configure(command=None)
# element.configure(textvariable=None)
# element.configure(width=140)
# element.configure(height=28)
# element.configure(corner_radius="default_theme")
# element.configure(text_font="default_theme")
# element.configure(text_color="default_theme")
# element.configure(text_color_disabled="default_theme")
class App(customtkinter.CTk):
theme = THEME
def toggle_color(self):
if App.theme == "sweetkind":
App.theme = "blue"
else:
App.theme = "sweetkind"
def alter_theme(self, event=None):
self.toggle_color()
CTkButtonStyle.customize(self.bt, App.theme)
def __init__(self):
super().__init__()
self.title("Change Theme")
self.geometry("800x600")
self.bt = customtkinter.CTkButton(self, width=100, height=50, text="Change Theme", command=self.alter_theme)
self.bt.place(anchor="center", relx="0.5", rely="0.5")
a = App()
a.mainloop() Tom, what do you think about adding a new method inside the widget_base class, something like set_style ? def set_style(self, style_state):
pass And then implementing inside each widget how to override the style for that individual component ? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How to change default theme with a ctk.CTkSlider because it doesn't changes the theme when doing so example: ctk.set_default_color_theme("sweetkind") or ctk.set_default_color_theme("green")
def green_sweet_slider_():
get=green_sweet_slider.get()
if get==1:
ctk.set_default_color_theme("green")
else:
ctk.set_default_color_theme('sweetkind')
green_sweet_slider=ctk.CTkSwitch(frame_bar,text='green',command=lambda:[green_sweet_slider_()])
green_sweet_slider.place(x=265,y=660)
green_sweet_slider.select()
Beta Was this translation helpful? Give feedback.
All reactions