From cdbf2382eff956a49db545d75f75cba355b2cd24 Mon Sep 17 00:00:00 2001 From: Tom Schimansky Date: Sat, 9 Apr 2022 19:37:07 +0200 Subject: [PATCH] progress on test_ttk_frames.py --- customtkinter/__init__.py | 2 +- customtkinter/customtkinter_base_class.py | 5 + customtkinter/customtkinter_button.py | 29 +++++- customtkinter/customtkinter_theme_manager.py | 9 +- test/test_simple_font_circle.py | 12 +-- test/test_ttk_frames.py | 97 ++++++++++++++++++++ 6 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 customtkinter/customtkinter_base_class.py create mode 100644 test/test_ttk_frames.py diff --git a/customtkinter/__init__.py b/customtkinter/__init__.py index ad81396..95b20a7 100644 --- a/customtkinter/__init__.py +++ b/customtkinter/__init__.py @@ -83,7 +83,7 @@ if sys.platform.startswith("win"): pathbuf = create_unicode_buffer(fontpath) AddFontResourceEx = windll.gdi32.AddFontResourceExW else: - raise TypeError('fontpath must be of type str or unicode') + raise TypeError('fontpath must be of type bytes or str') flags = (FR_PRIVATE if private else 0) | (FR_NOT_ENUM if not enumerable else 0) num_fonts_added = AddFontResourceEx(byref(pathbuf), flags, 0) diff --git a/customtkinter/customtkinter_base_class.py b/customtkinter/customtkinter_base_class.py new file mode 100644 index 0000000..06d9fc1 --- /dev/null +++ b/customtkinter/customtkinter_base_class.py @@ -0,0 +1,5 @@ +import tkinter +import tkinter.ttk as ttk + + +class CTkWidgetBaseClass: diff --git a/customtkinter/customtkinter_button.py b/customtkinter/customtkinter_button.py index 4d45041..f4a403e 100644 --- a/customtkinter/customtkinter_button.py +++ b/customtkinter/customtkinter_button.py @@ -1,4 +1,5 @@ import tkinter +import tkinter.ttk as ttk import sys from .customtkinter_tk import CTk @@ -143,10 +144,32 @@ class CTkButton(tkinter.Frame): self.draw(no_color_updates=True) # fast drawing without color changes def detect_color_of_master(self): - if isinstance(self.master, CTkFrame): + """ detect color of self.master widget to set correct bg_color """ + + if isinstance(self.master, CTkFrame): # master is CTkFrame return self.master.fg_color - else: - return self.master.cget("bg") + + elif isinstance(self.master, ttk.Frame): # master is ttk widget + print("button on", self.master.winfo_class()) + try: + ttk_style = ttk.Style() + return ttk_style.lookup(self.master.winfo_class(), 'background') + except Exception: + return "#000000" + + elif isinstance(self.master, ttk.LabelFrame): + print("button on", self.master.winfo_class()) + try: + ttk_style = ttk.Style() + return ttk_style.lookup(self.master.winfo_class(), 'foreground') + except Exception: + return "#000000" + + else: # master is normal tkinter widget + try: + return self.master.cget("bg") # try to get bg color by .cget() method + except Exception: + return "#000000" def draw(self, no_color_updates=False): requires_recoloring = self.draw_engine.draw_rounded_rect_with_border(self.width, self.height, self.corner_radius, self.border_width) diff --git a/customtkinter/customtkinter_theme_manager.py b/customtkinter/customtkinter_theme_manager.py index 5bdb7e5..cebba58 100644 --- a/customtkinter/customtkinter_theme_manager.py +++ b/customtkinter/customtkinter_theme_manager.py @@ -62,13 +62,6 @@ class CTkThemeManager: return cls.rgb2hex(new_rgb) - @classmethod - def disabled_color(cls, hex_color: str): - if appearance_mode == 0: # Light mode - return cls.theme["color"]["text_disabled"] - else: - return cls.multiply_hex_color(hex_color, 0.8) - @classmethod def multiply_hex_color(cls, hex_color: str, factor: float = None) -> str: if factor is None: @@ -90,4 +83,4 @@ class CTkThemeManager: cls.MAIN_HOVER_COLOR = main_color_hover -CTkThemeManager.load_theme("blue") +CTkThemeManager.load_theme("blue") # standard theme diff --git a/test/test_simple_font_circle.py b/test/test_simple_font_circle.py index 7d86de5..a617cad 100644 --- a/test/test_simple_font_circle.py +++ b/test/test_simple_font_circle.py @@ -7,10 +7,10 @@ app.geometry("600x500") canvas = tkinter.Canvas(master=app, highlightthickness=0, bg="gray30") canvas.pack(expand=True, fill="both") -text_1 = canvas.create_text(100, 100, text="⬤", font=('Helvetica','2','bold')) -text_2 = canvas.create_text(100, 200, text="⬤", font=('Helvetica','4','bold')) -text_3 = canvas.create_text(100, 300, text="⬤", font=('Helvetica','6','bold')) -text_4 = canvas.create_text(100, 400, text="⬤", font=('Helvetica',8,'bold')) -text_4 = canvas.create_text(400, 400, text="⬤", font=('Helvetica',80,'bold')) +text_1 = canvas.create_text(100, 100, text="⬤", font=('Helvetica', -4, 'bold')) +text_2 = canvas.create_text(100, 200, text="⬤", font=('Helvetica', -8, 'bold')) +text_3 = canvas.create_text(100, 300, text="⬤", font=('Helvetica', -12, 'bold')) +text_4 = canvas.create_text(100, 400, text="⬤", font=('Helvetica', -20, 'bold')) +text_5 = canvas.create_text(400, 400, text="⬤", font=('Helvetica', -100, 'bold')) -app.mainloop() \ No newline at end of file +app.mainloop() diff --git a/test/test_ttk_frames.py b/test/test_ttk_frames.py new file mode 100644 index 0000000..ea00f4d --- /dev/null +++ b/test/test_ttk_frames.py @@ -0,0 +1,97 @@ +import tkinter +import tkinter.ttk as ttk +import customtkinter + +root_tk = customtkinter.CTk() +root_tk.geometry("1400x480") +root_tk.title("CustomTkinter TTk Compatibility Test") + +root_tk.grid_rowconfigure(0, weight=1) +root_tk.grid_columnconfigure((0, 1, 2, 3, 5), weight=1) + + +button_0 = customtkinter.CTkButton(root_tk) +button_0.grid(padx=20, pady=20, row=0, column=0) + +frame_1 = tkinter.Frame(master=root_tk) +frame_1.grid(padx=20, pady=20, row=0, column=1, sticky="nsew") +button_1 = customtkinter.CTkButton(frame_1, text="tkinter.Frame") +button_1.pack(pady=20, padx=20) + +frame_2 = tkinter.LabelFrame(master=root_tk, text="Tkinter LabelFrame") +frame_2.grid(padx=20, pady=20, row=0, column=2, sticky="nsew") +button_2 = customtkinter.CTkButton(frame_2, text="tkinter.LabelFrame") +button_2.pack(pady=20, padx=20) + +frame_3 = ttk.Frame(master=root_tk) +frame_3.grid(padx=20, pady=20, row=0, column=3, sticky="nsew") +button_3 = customtkinter.CTkButton(frame_3, text="ttk.Frame") +button_3.pack(pady=20, padx=20) + +frame_4 = ttk.LabelFrame(master=root_tk, text="TTk LabelFrame") +frame_4.grid(padx=20, pady=20, row=0, column=4, sticky="nsew") +button_4 = customtkinter.CTkButton(frame_4) +button_4.pack(pady=20, padx=20) + +frame_5 = ttk.Notebook(master=root_tk) +frame_5.grid(padx=20, pady=20, row=0, column=5, sticky="nsew") +button_5 = customtkinter.CTkButton(frame_5, text="ttk.Notebook") +button_5.pack(pady=20, padx=20) + +ttk_style = ttk.Style() +ttk_style.configure(frame_3.winfo_class(), background='red') + + +def iter_layout(layout, tab_amnt=0, elements=[]): + """Recursively prints the layout children.""" + el_tabs = ' '*tab_amnt + val_tabs = ' '*(tab_amnt + 1) + + for element, child in layout: + elements.append(element) + print(el_tabs+ '\'{}\': {}'.format(element, '{')) + for key, value in child.items(): + if type(value) == str: + print(val_tabs + '\'{}\' : \'{}\','.format(key, value)) + else: + print(val_tabs + '\'{}\' : [('.format(key)) + iter_layout(value, tab_amnt=tab_amnt+3) + print(val_tabs + ')]') + + print(el_tabs + '{}{}'.format('} // ', element)) + + return elements + +def stylename_elements_options(stylename, widget): + """Function to expose the options of every element associated to a widget stylename.""" + + try: + # Get widget elements + style = ttk.Style() + layout = style.layout(stylename) + config = widget.configure() + + print('{:*^50}\n'.format(f'Style = {stylename}')) + + print('{:*^50}'.format('Config')) + for key, value in config.items(): + print('{:<15}{:^10}{}'.format(key, '=>', value)) + + print('\n{:*^50}'.format('Layout')) + elements = iter_layout(layout) + + # Get options of widget elements + print('\n{:*^50}'.format('element options')) + for element in elements: + print('{0:30} options: {1}'.format( + element, style.element_options(element))) + + except tk.TclError: + print('_tkinter.TclError: "{0}" in function' + 'widget_elements_options({0}) is not a regonised stylename.' + .format(stylename)) + + +stylename_elements_options(frame_3.winfo_class(), frame_3) + +root_tk.mainloop()