removed bg and background argument support from CTk and CTkToplevel

This commit is contained in:
Tom Schimansky
2022-10-16 20:13:19 +02:00
parent 53b0d04e4b
commit 1ae794272b
16 changed files with 184 additions and 138 deletions

View File

@ -7,6 +7,7 @@ from ..theme_manager import ThemeManager
from ..settings import Settings
from ..draw_engine import DrawEngine
from .widget_base_class import CTkBaseClass
from ..utility.ctk_font import CTkFont
class CTkButton(CTkBaseClass):
@ -34,7 +35,7 @@ class CTkButton(CTkBaseClass):
round_height_to_even_numbers: bool = True,
text: str = "CTkButton",
font: any = "default_theme",
font: Union[tuple, CTkFont] = "default_theme",
textvariable: tkinter.Variable = None,
image: tkinter.PhotoImage = None,
state: str = "normal",
@ -67,7 +68,7 @@ class CTkButton(CTkBaseClass):
self._image_label: Union[tkinter.Label, None] = None
self._text = text
self._text_label: Union[tkinter.Label, None] = None
self._font = (ThemeManager.theme["text"]["font"], ThemeManager.theme["text"]["size"]) if font == "default_theme" else font
self._font = CTkFont() if font == "default_theme" else self._check_font_type_and_values(font)
# callback and hover functionality
self._command = command

View File

@ -9,8 +9,6 @@ from ..settings import Settings
from ..draw_engine import DrawEngine
from .widget_base_class import CTkBaseClass
from .widget_helper_functions import filter_dict_by_set
class CTkComboBox(CTkBaseClass):
"""

View File

@ -6,7 +6,7 @@ from ..theme_manager import ThemeManager
from ..draw_engine import DrawEngine
from .widget_base_class import CTkBaseClass
from .widget_helper_functions import pop_from_dict_by_set
from customtkinter.utility.utility_functions import pop_from_dict_by_set, check_kwargs_empty
class CTkEntry(CTkBaseClass):
@ -95,7 +95,7 @@ class CTkEntry(CTkBaseClass):
padx=self._apply_widget_scaling(self._minimum_x_padding),
pady=(self._apply_widget_scaling(self._border_width), self._apply_widget_scaling(self._border_width + 1)))
self._check_kwargs_empty(kwargs, raise_error=True)
check_kwargs_empty(kwargs, raise_error=True)
self._entry.bind('<FocusOut>', self._entry_focus_out)
self._entry.bind('<FocusIn>', self._entry_focus_in)

View File

@ -6,7 +6,7 @@ from ..theme_manager import ThemeManager
from ..draw_engine import DrawEngine
from .widget_base_class import CTkBaseClass
from .widget_helper_functions import pop_from_dict_by_set
from customtkinter.utility.utility_functions import pop_from_dict_by_set, check_kwargs_empty
class CTkLabel(CTkBaseClass):
@ -74,7 +74,7 @@ class CTkLabel(CTkBaseClass):
self._text_label.grid(row=0, column=0, sticky=text_label_grid_sticky,
padx=self._apply_widget_scaling(min(self._corner_radius, round(self._current_height/2))))
self._check_kwargs_empty(kwargs, raise_error=True)
check_kwargs_empty(kwargs, raise_error=True)
self._draw()

View File

@ -7,7 +7,7 @@ from ..theme_manager import ThemeManager
from ..draw_engine import DrawEngine
from .widget_base_class import CTkBaseClass
from .widget_helper_functions import pop_from_dict_by_set
from customtkinter.utility.utility_functions import pop_from_dict_by_set, check_kwargs_empty
class CTkTextbox(CTkBaseClass):
@ -87,7 +87,7 @@ class CTkTextbox(CTkBaseClass):
bg=ThemeManager.single_color(self._fg_color, self._appearance_mode),
**pop_from_dict_by_set(kwargs, self._valid_tk_text_attributes))
self._check_kwargs_empty(kwargs, raise_error=True)
check_kwargs_empty(kwargs, raise_error=True)
# scrollbars
self._scrollbars_activated = activate_scrollbars

View File

@ -1,7 +1,6 @@
import tkinter
import tkinter.ttk as ttk
import copy
import re
from typing import Union, Callable, Tuple
try:
@ -15,7 +14,9 @@ from ..appearance_mode_tracker import AppearanceModeTracker
from ..scaling_tracker import ScalingTracker
from ..theme_manager import ThemeManager
from .widget_helper_functions import pop_from_dict_by_set
from ..utility.ctk_font import CTkFont
from ..utility.utility_functions import pop_from_dict_by_set, check_kwargs_empty
class CTkBaseClass(tkinter.Frame):
@ -36,7 +37,7 @@ class CTkBaseClass(tkinter.Frame):
super().__init__(master=master, width=width, height=height, **pop_from_dict_by_set(kwargs, self._valid_tk_frame_attributes))
# check if kwargs is empty, if not raise error for unsupported arguments
self._check_kwargs_empty(kwargs, raise_error=True)
check_kwargs_empty(kwargs, raise_error=True)
# dimensions
self._current_width = width # _current_width and _current_height in pixel, represent current size of the widget
@ -138,16 +139,10 @@ class CTkBaseClass(tkinter.Frame):
raise ValueError(f"'{attribute_name}' is not a supported argument. Look at the documentation for supported arguments.")
@staticmethod
def _check_kwargs_empty(kwargs_dict, raise_error=False) -> bool:
""" returns True if kwargs are empty, False otherwise, raises error if not empty """
if len(kwargs_dict) > 0:
if raise_error:
raise ValueError(f"{list(kwargs_dict.keys())} are not supported arguments. Look at the documentation for supported arguments.")
else:
return True
else:
return False
def _check_font_type_and_values(font: any):
if not isinstance(font, CTkFont):
raise ValueError(f"\nFor consistency, Customtkinter requires the font argument {font} to be an instance of CTkFont.\n" +
f"\nUsage example: font=customtkinter.CTkFont(family='name', size='size in px')\n(other arguments in the documentation)\n")
def _update_dimensions_event(self, event):
# only redraw if dimensions changed (for performance), independent of scaling
@ -224,26 +219,14 @@ class CTkBaseClass(tkinter.Frame):
else:
return value
def _apply_font_scaling(self, font):
if type(font) == tuple or type(font) == list:
def _apply_font_scaling(self, font: Union[tuple, CTkFont]) -> Union[tuple, CTkFont]:
if type(font) == tuple:
font_list = list(font)
for i in range(len(font_list)):
if (type(font_list[i]) == int or type(font_list[i]) == float) and font_list[i] < 0:
font_list[i] = int(font_list[i] * self._widget_scaling)
if len(font_list) >= 2 and type(font_list[1]) == int:
font_list[1] = int(font_list[1] * self._widget_scaling)
return tuple(font_list)
elif type(font) == str:
for negative_number in re.findall(r" -\d* ", font):
font = font.replace(negative_number, f" {int(int(negative_number) * self._widget_scaling)} ")
return font
elif isinstance(font, tkinter.font.Font):
new_font_object = copy.copy(font)
if font.cget("size") < 0:
new_font_object.config(size=int(font.cget("size") * self._widget_scaling))
return new_font_object
else:
elif isinstance(font, CTkFont):
return font
def _apply_argument_scaling(self, kwargs: dict) -> dict:

View File

@ -1,22 +0,0 @@
def filter_dict_by_set(dictionary: dict, valid_keys: set):
""" create new dict with key value pairs of dictionary, where key is in valid_keys """
new_dictionary = {}
for key, value in dictionary.items():
if key in valid_keys:
new_dictionary[key] = value
return new_dictionary
def pop_from_dict_by_set(dictionary: dict, valid_keys: set):
""" remove and create new dict with key value pairs of dictionary, where key is in valid_keys """
new_dictionary = {}
for key in list(dictionary.keys()):
if key in valid_keys:
new_dictionary[key] = dictionary.pop(key)
return new_dictionary