From 7cdf3fd98d8a023a8d258faf29eacafd6832f163 Mon Sep 17 00:00:00 2001 From: TomSchimansky Date: Sat, 9 Apr 2022 21:23:35 +0200 Subject: [PATCH] new detect_color_of_master function on all widgets --- customtkinter/customtkinter_button.py | 14 ++---- customtkinter/customtkinter_checkbox.py | 21 +++++++-- customtkinter/customtkinter_entry.py | 22 +++++++-- customtkinter/customtkinter_frame.py | 21 +++++++-- customtkinter/customtkinter_label.py | 22 +++++++-- customtkinter/customtkinter_progressbar.py | 21 +++++++-- customtkinter/customtkinter_radiobutton.py | 21 +++++++-- customtkinter/customtkinter_slider.py | 21 +++++++-- customtkinter/customtkinter_switch.py | 21 +++++++-- test/test_button_antialiasing.py | 5 +- test/test_ttk_frames.py | 53 ---------------------- 11 files changed, 149 insertions(+), 93 deletions(-) diff --git a/customtkinter/customtkinter_button.py b/customtkinter/customtkinter_button.py index f4a403e..7fa5c52 100644 --- a/customtkinter/customtkinter_button.py +++ b/customtkinter/customtkinter_button.py @@ -149,27 +149,19 @@ class CTkButton(tkinter.Frame): if isinstance(self.master, CTkFrame): # master is CTkFrame return self.master.fg_color - elif isinstance(self.master, ttk.Frame): # master is ttk widget + elif isinstance(self.master, (ttk.Frame, ttk.LabelFrame, ttk.Notebook)): # 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" + return "#FFFFFF", "#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" + return "#FFFFFF", "#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_checkbox.py b/customtkinter/customtkinter_checkbox.py index 434a918..b413ad3 100644 --- a/customtkinter/customtkinter_checkbox.py +++ b/customtkinter/customtkinter_checkbox.py @@ -1,4 +1,5 @@ import tkinter +import tkinter.ttk as ttk import sys from customtkinter.customtkinter_tk import CTk @@ -140,10 +141,24 @@ class CTkCheckBox(tkinter.Frame): super().destroy() 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, ttk.LabelFrame, ttk.Notebook)): # 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 "#FFFFFF", "#000000" + + else: # master is normal tkinter widget + try: + return self.master.cget("bg") # try to get bg color by .cget() method + except Exception: + return "#FFFFFF", "#000000" def draw(self): 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_entry.py b/customtkinter/customtkinter_entry.py index e7878ac..8fb7c4c 100644 --- a/customtkinter/customtkinter_entry.py +++ b/customtkinter/customtkinter_entry.py @@ -1,4 +1,5 @@ import tkinter +import tkinter.ttk as ttk from .customtkinter_tk import CTk from .customtkinter_frame import CTkFrame @@ -111,13 +112,24 @@ class CTkEntry(tkinter.Frame): self.grid_columnconfigure(0, weight=1) 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: + + elif isinstance(self.master, (ttk.Frame, ttk.LabelFrame, ttk.Notebook)): # master is ttk widget + print("button on", self.master.winfo_class()) try: - return self.master.cget("bg") - except: - pass + ttk_style = ttk.Style() + return ttk_style.lookup(self.master.winfo_class(), 'background') + except Exception: + return "#FFFFFF", "#000000" + + else: # master is normal tkinter widget + try: + return self.master.cget("bg") # try to get bg color by .cget() method + except Exception: + return "#FFFFFF", "#000000" def update_dimensions(self, event): # only redraw if dimensions changed (for performance) diff --git a/customtkinter/customtkinter_frame.py b/customtkinter/customtkinter_frame.py index 5fc0fbf..9348ea8 100644 --- a/customtkinter/customtkinter_frame.py +++ b/customtkinter/customtkinter_frame.py @@ -1,4 +1,5 @@ import tkinter +import tkinter.ttk as ttk from .customtkinter_tk import CTk from .appearance_mode_tracker import AppearanceModeTracker @@ -94,10 +95,24 @@ class CTkFrame(tkinter.Frame): super().destroy() 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, ttk.LabelFrame, ttk.Notebook)): # 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 "#FFFFFF", "#000000" + + else: # master is normal tkinter widget + try: + return self.master.cget("bg") # try to get bg color by .cget() method + except Exception: + return "#FFFFFF", "#000000" def update_dimensions(self, event): # only redraw if dimensions changed (for performance) diff --git a/customtkinter/customtkinter_label.py b/customtkinter/customtkinter_label.py index 443b1aa..fdbba67 100644 --- a/customtkinter/customtkinter_label.py +++ b/customtkinter/customtkinter_label.py @@ -1,5 +1,5 @@ import tkinter -import sys +import tkinter.ttk as ttk from .customtkinter_tk import CTk from .customtkinter_frame import CTkFrame @@ -99,10 +99,24 @@ class CTkLabel(tkinter.Frame): super().destroy() 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, ttk.LabelFrame, ttk.Notebook)): # 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 "#FFFFFF", "#000000" + + else: # master is normal tkinter widget + try: + return self.master.cget("bg") # try to get bg color by .cget() method + except Exception: + return "#FFFFFF", "#000000" def update_dimensions(self, event): # only redraw if dimensions changed (for performance) diff --git a/customtkinter/customtkinter_progressbar.py b/customtkinter/customtkinter_progressbar.py index a19f4d0..45ff428 100644 --- a/customtkinter/customtkinter_progressbar.py +++ b/customtkinter/customtkinter_progressbar.py @@ -1,5 +1,6 @@ import sys import tkinter +import tkinter.ttk as ttk from .customtkinter_tk import CTk from .customtkinter_frame import CTkFrame @@ -96,10 +97,24 @@ class CTkProgressBar(tkinter.Frame): super().destroy() 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, ttk.LabelFrame, ttk.Notebook)): # 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 "#FFFFFF", "#000000" + + else: # master is normal tkinter widget + try: + return self.master.cget("bg") # try to get bg color by .cget() method + except Exception: + return "#FFFFFF", "#000000" @staticmethod def calc_optimal_height(user_height): diff --git a/customtkinter/customtkinter_radiobutton.py b/customtkinter/customtkinter_radiobutton.py index 7a42c83..e81887d 100644 --- a/customtkinter/customtkinter_radiobutton.py +++ b/customtkinter/customtkinter_radiobutton.py @@ -1,4 +1,5 @@ import tkinter +import tkinter.ttk as ttk import sys from customtkinter.customtkinter_tk import CTk @@ -135,10 +136,24 @@ class CTkRadioButton(tkinter.Frame): super().destroy() 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, ttk.LabelFrame, ttk.Notebook)): # 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 "#FFFFFF", "#000000" + + else: # master is normal tkinter widget + try: + return self.master.cget("bg") # try to get bg color by .cget() method + except Exception: + return "#FFFFFF", "#000000" def draw(self): 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_slider.py b/customtkinter/customtkinter_slider.py index 1a33ba1..ea7d1f1 100644 --- a/customtkinter/customtkinter_slider.py +++ b/customtkinter/customtkinter_slider.py @@ -1,4 +1,5 @@ import tkinter +import tkinter.ttk as ttk import sys from .customtkinter_tk import CTk @@ -134,10 +135,24 @@ class CTkSlider(tkinter.Frame): self.grid_columnconfigure(0, weight=1) 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, ttk.LabelFrame, ttk.Notebook)): # 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 "#FFFFFF", "#000000" + + else: # master is normal tkinter widget + try: + return self.master.cget("bg") # try to get bg color by .cget() method + except Exception: + return "#FFFFFF", "#000000" @staticmethod def calc_optimal_height(user_height): diff --git a/customtkinter/customtkinter_switch.py b/customtkinter/customtkinter_switch.py index deb3331..5f30aad 100644 --- a/customtkinter/customtkinter_switch.py +++ b/customtkinter/customtkinter_switch.py @@ -1,4 +1,5 @@ import tkinter +import tkinter.ttk as ttk import sys from .customtkinter_tk import CTk @@ -134,10 +135,24 @@ class CTkSwitch(tkinter.Frame): super().destroy() 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, ttk.LabelFrame, ttk.Notebook)): # 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 "#FFFFFF", "#000000" + + else: # master is normal tkinter widget + try: + return self.master.cget("bg") # try to get bg color by .cget() method + except Exception: + return "#FFFFFF", "#000000" def draw(self, color_updates=True): diff --git a/test/test_button_antialiasing.py b/test/test_button_antialiasing.py index a421c86..a18c8d9 100644 --- a/test/test_button_antialiasing.py +++ b/test/test_button_antialiasing.py @@ -29,8 +29,9 @@ f4.grid(row=0, column=3, rowspan=1, columnspan=1, sticky="nsew") f4.grid_columnconfigure(0, weight=1) for i in range(0, 21, 1): - b = customtkinter.CTkButton(f1, corner_radius=i, height=34, border_width=2, text=f"{i} {i-2}", - border_color="white", fg_color=None, text_color="white") + #b = customtkinter.CTkButton(f1, corner_radius=i, height=34, border_width=2, text=f"{i} {i-2}", + # border_color="white", fg_color=None, text_color="white") + b = tkinter.Button(f1, text=f"{i} {i-2}", width=20) b.grid(row=i, column=0, pady=5, padx=15, sticky="nsew") b = customtkinter.CTkButton(f2, corner_radius=i, height=34, border_width=0, text=f"{i}", diff --git a/test/test_ttk_frames.py b/test/test_ttk_frames.py index ea00f4d..35be654 100644 --- a/test/test_ttk_frames.py +++ b/test/test_ttk_frames.py @@ -41,57 +41,4 @@ 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()