fixed bug in fg_color detection of master

This commit is contained in:
Tom Schimansky 2022-05-23 22:35:38 +02:00
parent 3a5d34cef6
commit 4e155aedd6

View File

@ -135,22 +135,30 @@ class CTkBaseClass(tkinter.Frame):
self.draw(no_color_updates=True) # faster drawing without color changes self.draw(no_color_updates=True) # faster drawing without color changes
def detect_color_of_master(self): def detect_color_of_master(self, master_widget=None):
""" detect color of self.master widget to set correct bg_color """ """ detect color of self.master widget to set correct bg_color """
if isinstance(self.master, CTkBaseClass) and hasattr(self.master, "fg_color"): # master is CTkFrame if master_widget is None:
return self.master.fg_color master_widget = self.master
elif isinstance(self.master, (ttk.Frame, ttk.LabelFrame, ttk.Notebook)): # master is ttk widget if isinstance(master_widget, CTkBaseClass) and hasattr(master_widget, "fg_color"): # master is CTkFrame
if master_widget.fg_color is not None:
return master_widget.fg_color
# if fg_color of master is None, try to retrieve fg_color from master of master
elif hasattr(master_widget.master, "master"):
return self.detect_color_of_master(self.master.master)
elif isinstance(master_widget, (ttk.Frame, ttk.LabelFrame, ttk.Notebook)): # master is ttk widget
try: try:
ttk_style = ttk.Style() ttk_style = ttk.Style()
return ttk_style.lookup(self.master.winfo_class(), 'background') return ttk_style.lookup(master_widget.winfo_class(), 'background')
except Exception: except Exception:
return "#FFFFFF", "#000000" return "#FFFFFF", "#000000"
else: # master is normal tkinter widget else: # master is normal tkinter widget
try: try:
return self.master.cget("bg") # try to get bg color by .cget() method return master_widget.cget("bg") # try to get bg color by .cget() method
except Exception: except Exception:
return "#FFFFFF", "#000000" return "#FFFFFF", "#000000"