This commit is contained in:
Youssef Essalhi 2023-07-27 16:41:19 +01:00 committed by GitHub
commit 8249ad6085
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 8 deletions

View File

@ -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("<KeyPress>", 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)

View File

@ -30,19 +30,21 @@ class CTkScrollableFrame(tkinter.Frame, CTkAppearanceModeBaseClass, CTkScalingBa
scrollbar_fg_color: Optional[Union[str, Tuple[str, str]]] = None,
scrollbar_button_color: Optional[Union[str, Tuple[str, str]]] = None,
scrollbar_button_hover_color: Optional[Union[str, Tuple[str, str]]] = None,
scrollbar_corner_radius: Optional[Union[int, str]] = None,
label_fg_color: Optional[Union[str, Tuple[str, str]]] = None,
label_text_color: Optional[Union[str, Tuple[str, str]]] = None,
label_text: str = "",
label_font: Optional[Union[tuple, CTkFont]] = None,
label_anchor: str = "center",
orientation: Literal["vertical", "horizontal"] = "vertical"):
orientation: Literal["vertical", "horizontal"] = "vertical",
scrollbar_cursor: str = "hand2"):
self._orientation = orientation
# dimensions independent of scaling
self._desired_width = width # _desired_width and _desired_height, represent desired size set by width and height
self._desired_height = height
self._desired_width = width # _desired_width represent desired size set by width
self._desired_height = height # _desired_height represent desired size set by height
self._parent_frame = CTkFrame(master=master, width=0, height=0, corner_radius=corner_radius,
border_width=border_width, bg_color=bg_color, fg_color=fg_color, border_color=border_color)
@ -50,12 +52,12 @@ class CTkScrollableFrame(tkinter.Frame, CTkAppearanceModeBaseClass, CTkScalingBa
self._set_scroll_increments()
if self._orientation == "horizontal":
self._scrollbar = CTkScrollbar(master=self._parent_frame, orientation="horizontal", command=self._parent_canvas.xview,
fg_color=scrollbar_fg_color, button_color=scrollbar_button_color, button_hover_color=scrollbar_button_hover_color)
self._scrollbar = CTkScrollbar(master=self._parent_frame, orientation="horizontal", command=self._parent_canvas.xview, width=self._desired_width, cursor=scrollbar_cursor,
corner_radius=scrollbar_corner_radius, fg_color=scrollbar_fg_color, button_color=scrollbar_button_color, button_hover_color=scrollbar_button_hover_color)
self._parent_canvas.configure(xscrollcommand=self._scrollbar.set)
elif self._orientation == "vertical":
self._scrollbar = CTkScrollbar(master=self._parent_frame, orientation="vertical", command=self._parent_canvas.yview,
fg_color=scrollbar_fg_color, button_color=scrollbar_button_color, button_hover_color=scrollbar_button_hover_color)
self._scrollbar = CTkScrollbar(master=self._parent_frame, orientation="vertical", command=self._parent_canvas.yview, height=self._desired_height, cursor=scrollbar_cursor,
corner_radius=scrollbar_corner_radius, fg_color=scrollbar_fg_color, button_color=scrollbar_button_color, button_hover_color=scrollbar_button_hover_color)
self._parent_canvas.configure(yscrollcommand=self._scrollbar.set)
self._label_text = label_text