new detect_color_of_master function on all widgets

This commit is contained in:
TomSchimansky 2022-04-09 21:23:35 +02:00
parent cdbf2382ef
commit 7cdf3fd98d
11 changed files with 149 additions and 93 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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):

View File

@ -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)

View File

@ -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):

View File

@ -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):

View File

@ -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}",

View File

@ -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()