mirror of
https://github.com/TomSchimansky/CustomTkinter.git
synced 2023-08-10 21:13:13 +03:00
removed bg and background argument support from CTk and CTkToplevel
This commit is contained in:
0
customtkinter/utility/__init__.py
Normal file
0
customtkinter/utility/__init__.py
Normal file
94
customtkinter/utility/ctk_font.py
Normal file
94
customtkinter/utility/ctk_font.py
Normal file
@@ -0,0 +1,94 @@
|
||||
from tkinter.font import Font
|
||||
import copy
|
||||
import sys
|
||||
|
||||
from ..scaling_tracker import ScalingTracker
|
||||
from ..theme_manager import ThemeManager
|
||||
|
||||
|
||||
class CTkFont(Font):
|
||||
"""
|
||||
Font object with size in pixel independent of scaling.
|
||||
"""
|
||||
def __init__(self,
|
||||
family: str = "default_theme",
|
||||
size: int = "default_theme",
|
||||
weight: str = "normal",
|
||||
slant: str = "roman",
|
||||
underline: bool = False,
|
||||
overstrike: bool = False):
|
||||
|
||||
# unscaled font size in px
|
||||
self._size = ThemeManager.theme["text"]["size"] if size == "default_theme" else size
|
||||
|
||||
if self._size < 0:
|
||||
sys.stderr.write(f"Warning: size {self._size} of CTkFont don't has to be negative, it's measured in pixel by default\n")
|
||||
|
||||
super().__init__(family=ThemeManager.theme["text"]["font"] if family == "default_theme" else family,
|
||||
size=self._size,
|
||||
weight=weight,
|
||||
slant=slant,
|
||||
underline=underline,
|
||||
overstrike=overstrike)
|
||||
|
||||
def _set_scaling(self, new_widget_scaling, new_spacing_scaling, new_window_scaling):
|
||||
self._widget_scaling = new_widget_scaling
|
||||
super().configure(size=round(self._apply_widget_scaling(self._size)))
|
||||
|
||||
def _apply_widget_scaling(self, value: int) -> int:
|
||||
if isinstance(value, int):
|
||||
return round(value * self._widget_scaling)
|
||||
else:
|
||||
raise ValueError(f"CTkFont can not scale size of type {type(value)}, only int allowed")
|
||||
|
||||
def _reverse_widget_scaling(self, value: int) -> int:
|
||||
if isinstance(value, int):
|
||||
return round(value / self._widget_scaling)
|
||||
else:
|
||||
raise ValueError(f"CTkFont can not scale size of type {type(value)}, only int allowed")
|
||||
|
||||
def config(self, *args, **kwargs):
|
||||
raise AttributeError("'config' is not implemented for CTk widgets. For consistency, always use 'configure' instead.")
|
||||
|
||||
def configure(self, **kwargs):
|
||||
if "size" in kwargs:
|
||||
self._size = kwargs.pop("size")
|
||||
super().configure(size=self._apply_widget_scaling(self._size))
|
||||
|
||||
super().configure(**kwargs)
|
||||
|
||||
def cget(self, attribute_name) -> any:
|
||||
if attribute_name == "size":
|
||||
return self._size
|
||||
else:
|
||||
super().cget(attribute_name)
|
||||
|
||||
def copy(self) -> "CTkFont":
|
||||
return copy.deepcopy(self)
|
||||
|
||||
def measure(self, text, displayof=None) -> int:
|
||||
""" measure width of text in px independent of scaling """
|
||||
return self._reverse_widget_scaling(super().measure(text, displayof=displayof))
|
||||
|
||||
def metrics(self, *options: any, **kw: any) -> dict:
|
||||
""" metrics of font, all values independent of scaling """
|
||||
metrics_dict = super().metrics(*options, **kw)
|
||||
|
||||
if "ascent" in metrics_dict:
|
||||
metrics_dict["ascent"] = self._reverse_widget_scaling(metrics_dict["ascent"])
|
||||
if "descent" in metrics_dict:
|
||||
metrics_dict["descent"] = self._reverse_widget_scaling(metrics_dict["descent"])
|
||||
if "linespace" in metrics_dict:
|
||||
metrics_dict["linespace"] = self._reverse_widget_scaling(metrics_dict["linespace"])
|
||||
|
||||
return metrics_dict
|
||||
|
||||
def actual(self, option: any = None, displayof: any = None) -> dict:
|
||||
""" get back a dictionary of the font's actual attributes, which may differ from the ones you requested, size independent of scaling """
|
||||
actual_dict = super().actual(option, displayof)
|
||||
|
||||
if "size" in actual_dict:
|
||||
actual_dict["size"] = self._reverse_widget_scaling(actual_dict["size"])
|
||||
|
||||
return actual_dict
|
||||
|
||||
23
customtkinter/utility/utility_functions.py
Normal file
23
customtkinter/utility/utility_functions.py
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user