From 3516912b275ff7b8989b38b8f1a4b95b4660b73f Mon Sep 17 00:00:00 2001 From: Youssef Essalhi <76540840+YoussefEssalhi@users.noreply.github.com> Date: Sun, 9 Jul 2023 06:56:26 +0100 Subject: [PATCH] Update CTkEntry --- customtkinter/windows/widgets/ctk_entry.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/customtkinter/windows/widgets/ctk_entry.py b/customtkinter/windows/widgets/ctk_entry.py index db54386..38f6301 100644 --- a/customtkinter/windows/widgets/ctk_entry.py +++ b/customtkinter/windows/widgets/ctk_entry.py @@ -28,6 +28,7 @@ class CTkEntry(CTkBaseClass): height: int = 28, corner_radius: Optional[int] = None, border_width: Optional[int] = None, + max_length: Optional[int] = None, bg_color: Union[str, Tuple[str, str]] = "transparent", fg_color: Optional[Union[str, Tuple[str, str]]] = None, @@ -65,6 +66,7 @@ class CTkEntry(CTkBaseClass): self._pre_placeholder_arguments = {} # some set arguments of the entry will be changed for placeholder and then set back self._textvariable = textvariable self._state = state + self._max_length = max_length self._textvariable_callback_name: str = "" # font @@ -89,7 +91,10 @@ class CTkEntry(CTkBaseClass): state=self._state, textvariable=self._textvariable, **pop_from_dict_by_set(kwargs, self._valid_tk_entry_attributes)) - + + if self._max_length != None: + self._entry.bind("", self._enforce_max_length) + check_kwargs_empty(kwargs, raise_error=True) self._create_grid() @@ -195,6 +200,9 @@ class CTkEntry(CTkBaseClass): if "state" in kwargs: self._state = kwargs.pop("state") self._entry.configure(state=self._state) + + if "max_length" in kwargs: + self._max_length = kwargs.pop("max_length") if "fg_color" in kwargs: self._fg_color = self._check_color_type(kwargs.pop("fg_color")) @@ -324,6 +332,13 @@ class CTkEntry(CTkBaseClass): self._deactivate_placeholder() self._is_focused = True + def _enforce_max_length(self, event=None): + entry_text = self.get() + if len(entry_text) >= self._max_length: + entry_text = entry_text[:self._max_length] + self.delete(0, "end") + self.insert(0, entry_text) + def delete(self, first_index, last_index=None): self._entry.delete(first_index, last_index)