From 23e2a7ff0718ee1dcf6cfc0d7eac6eae14d0df38 Mon Sep 17 00:00:00 2001 From: bfedie518 Date: Sat, 1 Apr 2023 16:29:03 -0500 Subject: [PATCH] Make button focusable --- customtkinter/windows/widgets/ctk_button.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/customtkinter/windows/widgets/ctk_button.py b/customtkinter/windows/widgets/ctk_button.py index d79a944..b4298ba 100644 --- a/customtkinter/windows/widgets/ctk_button.py +++ b/customtkinter/windows/widgets/ctk_button.py @@ -46,6 +46,8 @@ class CTkButton(CTkBaseClass): command: Union[Callable[[], None], None] = None, compound: str = "left", anchor: str = "center", + + takefocus: bool = True, **kwargs): # transfer basic functionality (bg_color, size, appearance_mode, scaling) to CTkBaseClass @@ -93,6 +95,7 @@ class CTkButton(CTkBaseClass): # canvas and draw engine self._canvas = CTkCanvas(master=self, + takefocus=takefocus, highlightthickness=0, width=self._apply_widget_scaling(self._desired_width), height=self._apply_widget_scaling(self._desired_height)) @@ -132,6 +135,16 @@ class CTkButton(CTkBaseClass): if self._image_label is not None: self._image_label.bind("", self._clicked) + if sequence is None or sequence == "": + self._canvas.bind("", self._on_enter) + + if sequence is None or sequence == "": + self._canvas.bind("", self._on_leave) + + if sequence is None or sequence == "": + self._canvas.bind("", self._clicked) + + def _set_scaling(self, *args, **kwargs): super()._set_scaling(*args, **kwargs) @@ -584,10 +597,10 @@ class CTkButton(CTkBaseClass): self._create_bindings(sequence=sequence) # restore internal callbacks for sequence def focus(self): - return self._text_label.focus() + return self._canvas.focus() def focus_set(self): - return self._text_label.focus_set() + return self._canvas.focus_set() def focus_force(self): - return self._text_label.focus_force() + return self._canvas.focus_force()